UnmanagedMarshal.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining
  5. // a copy of this software and associated documentation files (the
  6. // "Software"), to deal in the Software without restriction, including
  7. // without limitation the rights to use, copy, modify, merge, publish,
  8. // distribute, sublicense, and/or sell copies of the Software, and to
  9. // permit persons to whom the Software is furnished to do so, subject to
  10. // the following conditions:
  11. //
  12. // The above copyright notice and this permission notice shall be
  13. // included in all copies or substantial portions of the Software.
  14. //
  15. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  18. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  19. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  20. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  21. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. //
  23. //
  24. // System.Reflection.Emit/UnmanagedMarshal.cs
  25. //
  26. // Author:
  27. // Paolo Molaro ([email protected])
  28. //
  29. // (C) 2001-2002 Ximian, Inc. http://www.ximian.com
  30. //
  31. using System.Reflection.Emit;
  32. using System.Runtime.InteropServices;
  33. using System;
  34. namespace System.Reflection.Emit {
  35. [Obsolete ("An alternate API is available: Emit the MarshalAs custom attribute instead.")]
  36. [ComVisible (true)]
  37. [Serializable]
  38. public sealed class UnmanagedMarshal {
  39. #pragma warning disable 169, 414
  40. private int count;
  41. private UnmanagedType t;
  42. private UnmanagedType tbase;
  43. string guid;
  44. string mcookie;
  45. string marshaltype;
  46. Type marshaltyperef;
  47. private int param_num;
  48. private bool has_size;
  49. #pragma warning restore 169, 414
  50. private UnmanagedMarshal (UnmanagedType maint, int cnt) {
  51. count = cnt;
  52. t = maint;
  53. tbase = maint;
  54. }
  55. private UnmanagedMarshal (UnmanagedType maint, UnmanagedType elemt) {
  56. count = 0;
  57. t = maint;
  58. tbase = elemt;
  59. }
  60. public UnmanagedType BaseType {
  61. get {
  62. if (t == UnmanagedType.LPArray || t == UnmanagedType.SafeArray)
  63. throw new ArgumentException ();
  64. return tbase;
  65. }
  66. }
  67. public int ElementCount {
  68. get {return count;}
  69. }
  70. public UnmanagedType GetUnmanagedType {
  71. get {return t;}
  72. }
  73. public Guid IIDGuid {
  74. get {return new Guid (guid);}
  75. }
  76. public static UnmanagedMarshal DefineByValArray( int elemCount) {
  77. return new UnmanagedMarshal (UnmanagedType.ByValArray, elemCount);
  78. }
  79. public static UnmanagedMarshal DefineByValTStr( int elemCount) {
  80. return new UnmanagedMarshal (UnmanagedType.ByValTStr, elemCount);
  81. }
  82. public static UnmanagedMarshal DefineLPArray( UnmanagedType elemType) {
  83. return new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
  84. }
  85. public static UnmanagedMarshal DefineSafeArray( UnmanagedType elemType) {
  86. return new UnmanagedMarshal (UnmanagedType.SafeArray, elemType);
  87. }
  88. public static UnmanagedMarshal DefineUnmanagedMarshal( UnmanagedType unmanagedType) {
  89. return new UnmanagedMarshal (unmanagedType, unmanagedType);
  90. }
  91. /* this one is missing from the MS implementation */
  92. public static UnmanagedMarshal DefineCustom (Type typeref, string cookie, string mtype, Guid id) {
  93. UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.CustomMarshaler, UnmanagedType.CustomMarshaler);
  94. res.mcookie = cookie;
  95. res.marshaltype = mtype;
  96. res.marshaltyperef = typeref;
  97. if (id == Guid.Empty)
  98. res.guid = String.Empty;
  99. else
  100. res.guid = id.ToString ();
  101. return res;
  102. }
  103. // sizeConst and sizeParamIndex can be -1 meaning they are not specified
  104. internal static UnmanagedMarshal DefineLPArrayInternal (UnmanagedType elemType, int sizeConst, int sizeParamIndex) {
  105. UnmanagedMarshal res = new UnmanagedMarshal (UnmanagedType.LPArray, elemType);
  106. res.count = sizeConst;
  107. res.param_num = sizeParamIndex;
  108. res.has_size = true;
  109. return res;
  110. }
  111. internal MarshalAsAttribute ToMarshalAsAttribute () {
  112. MarshalAsAttribute attr = new MarshalAsAttribute (t);
  113. attr.ArraySubType = tbase;
  114. attr.MarshalCookie = mcookie;
  115. attr.MarshalType = marshaltype;
  116. attr.MarshalTypeRef = marshaltyperef;
  117. if (count == -1)
  118. attr.SizeConst = 0;
  119. else
  120. attr.SizeConst = count;
  121. if (param_num == -1)
  122. attr.SizeParamIndex = 0;
  123. else
  124. attr.SizeParamIndex = (short)param_num;
  125. return attr;
  126. }
  127. }
  128. }