2 #define FEATURE_COMINTEROP
6 // Copyright (c) Microsoft Corporation. All rights reserved.
9 ////////////////////////////////////////////////////////////////////////////////
10 ////////////////////////////////////////////////////////////////////////////////
11 namespace System.Runtime.InteropServices{
14 using System.Reflection;
15 using System.Diagnostics.Contracts;
17 [AttributeUsage(AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
18 [System.Runtime.InteropServices.ComVisible(true)]
19 public sealed class UnmanagedFunctionPointerAttribute : Attribute
21 CallingConvention m_callingConvention;
23 public UnmanagedFunctionPointerAttribute(CallingConvention callingConvention) { m_callingConvention = callingConvention; }
25 public CallingConvention CallingConvention { get { return m_callingConvention; } }
27 public CharSet CharSet;
28 public bool BestFitMapping;
29 public bool ThrowOnUnmappableChar;
31 // This field is ignored and marshaling behaves as if it was true (for historical reasons).
32 public bool SetLastError;
34 // P/Invoke via delegate always preserves signature, HRESULT swapping is not supported.
35 //public bool PreserveSig;
38 [AttributeUsage(AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, AllowMultiple = false, Inherited = false)]
39 [System.Runtime.InteropServices.ComVisible(false)]
40 public sealed class TypeIdentifierAttribute : Attribute
42 public TypeIdentifierAttribute() { }
43 public TypeIdentifierAttribute(string scope, string identifier) { Scope_ = scope; Identifier_ = identifier; }
45 public String Scope { get { return Scope_; } }
46 public String Identifier { get { return Identifier_; } }
48 internal String Scope_;
49 internal String Identifier_;
52 // To be used on methods that sink reverse P/Invoke calls.
53 // This attribute is a CoreCLR-only security measure, currently ignored by the desktop CLR.
54 [AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = false)]
55 public sealed class AllowReversePInvokeCallsAttribute : Attribute
57 public AllowReversePInvokeCallsAttribute()
62 [AttributeUsage(AttributeTargets.Method | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Event, Inherited = false)]
63 [System.Runtime.InteropServices.ComVisible(true)]
64 public sealed class DispIdAttribute : Attribute
67 public DispIdAttribute(int dispId)
71 public int Value { get { return _val; } }
75 [System.Runtime.InteropServices.ComVisible(true)]
76 public enum ComInterfaceType
79 InterfaceIsIUnknown = 1,
80 InterfaceIsIDispatch = 2,
82 [System.Runtime.InteropServices.ComVisible(false)]
83 InterfaceIsIInspectable = 3,
86 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
87 [System.Runtime.InteropServices.ComVisible(true)]
88 public sealed class InterfaceTypeAttribute : Attribute
90 internal ComInterfaceType _val;
91 public InterfaceTypeAttribute(ComInterfaceType interfaceType)
95 public InterfaceTypeAttribute(short interfaceType)
97 _val = (ComInterfaceType)interfaceType;
99 public ComInterfaceType Value { get { return _val; } }
102 [AttributeUsage(AttributeTargets.Class, Inherited = false)]
103 [System.Runtime.InteropServices.ComVisible(true)]
104 public sealed class ComDefaultInterfaceAttribute : Attribute
108 public ComDefaultInterfaceAttribute(Type defaultInterface)
110 _val = defaultInterface;
113 public Type Value { get { return _val; } }
117 [System.Runtime.InteropServices.ComVisible(true)]
118 public enum ClassInterfaceType
125 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, Inherited = false)]
126 [System.Runtime.InteropServices.ComVisible(true)]
127 public sealed class ClassInterfaceAttribute : Attribute
129 internal ClassInterfaceType _val;
130 public ClassInterfaceAttribute(ClassInterfaceType classInterfaceType)
132 _val = classInterfaceType;
135 public ClassInterfaceAttribute(short classInterfaceType)
137 _val = (ClassInterfaceType)classInterfaceType;
139 public ClassInterfaceType Value { get { return _val; } }
142 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Delegate | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
143 [System.Runtime.InteropServices.ComVisible(true)]
144 public sealed class ComVisibleAttribute : Attribute
147 public ComVisibleAttribute(bool visibility)
151 public bool Value { get { return _val; } }
154 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
155 [System.Runtime.InteropServices.ComVisible(true)]
156 public sealed class TypeLibImportClassAttribute : Attribute
158 internal String _importClassName;
159 public TypeLibImportClassAttribute(Type importClass)
161 _importClassName = importClass.ToString();
163 public String Value { get { return _importClassName; } }
166 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
167 [System.Runtime.InteropServices.ComVisible(true)]
168 public sealed class LCIDConversionAttribute : Attribute
171 public LCIDConversionAttribute(int lcid)
175 public int Value { get {return _val;} }
178 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
179 [System.Runtime.InteropServices.ComVisible(true)]
180 public sealed class ComRegisterFunctionAttribute : Attribute
182 public ComRegisterFunctionAttribute()
187 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
188 [System.Runtime.InteropServices.ComVisible(true)]
189 public sealed class ComUnregisterFunctionAttribute : Attribute
191 public ComUnregisterFunctionAttribute()
196 [AttributeUsage(AttributeTargets.Class, Inherited = false)]
197 [System.Runtime.InteropServices.ComVisible(true)]
198 public sealed class ProgIdAttribute : Attribute
200 internal String _val;
201 public ProgIdAttribute(String progId)
205 public String Value { get {return _val;} }
208 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
209 [System.Runtime.InteropServices.ComVisible(true)]
210 public sealed class ImportedFromTypeLibAttribute : Attribute
212 internal String _val;
213 public ImportedFromTypeLibAttribute(String tlbFile)
217 public String Value { get {return _val;} }
220 [Obsolete("The IDispatchImplAttribute is deprecated.", false)]
222 [System.Runtime.InteropServices.ComVisible(true)]
223 public enum IDispatchImplType
225 SystemDefinedImpl = 0,
230 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false)]
231 [Obsolete("This attribute is deprecated and will be removed in a future version.", false)]
232 [System.Runtime.InteropServices.ComVisible(true)]
233 public sealed class IDispatchImplAttribute : Attribute
235 internal IDispatchImplType _val;
236 public IDispatchImplAttribute(IDispatchImplType implType)
240 public IDispatchImplAttribute(short implType)
242 _val = (IDispatchImplType)implType;
244 public IDispatchImplType Value { get {return _val;} }
247 [AttributeUsage(AttributeTargets.Class, Inherited = true)]
248 [System.Runtime.InteropServices.ComVisible(true)]
249 public sealed class ComSourceInterfacesAttribute : Attribute
251 internal String _val;
252 public ComSourceInterfacesAttribute(String sourceInterfaces)
254 _val = sourceInterfaces;
256 public ComSourceInterfacesAttribute(Type sourceInterface)
258 _val = sourceInterface.FullName;
260 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2)
262 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName;
264 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3)
266 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName;
268 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3, Type sourceInterface4)
270 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName + "\0" + sourceInterface4.FullName;
272 public String Value { get {return _val;} }
275 #if FEATURE_COMINTEROP || MOBILE_LEGACY
276 [AttributeUsage(AttributeTargets.All, Inherited = false)]
277 [System.Runtime.InteropServices.ComVisible(true)]
278 public sealed class ComConversionLossAttribute : Attribute
280 public ComConversionLossAttribute()
285 #if FEATURE_COMINTEROP || MOBILE_LEGACY
288 [System.Runtime.InteropServices.ComVisible(true)]
289 public enum TypeLibTypeFlags
298 FNonExtensible = 0x0080,
299 FOleAutomation = 0x0100,
300 FRestricted = 0x0200,
301 FAggregatable = 0x0400,
302 FReplaceable = 0x0800,
303 FDispatchable = 0x1000,
304 FReverseBind = 0x2000,
309 [System.Runtime.InteropServices.ComVisible(true)]
310 public enum TypeLibFuncFlags
312 FRestricted = 0x0001,
315 FRequestEdit = 0x0008,
316 FDisplayBind = 0x0010,
317 FDefaultBind = 0x0020,
319 FUsesGetLastError = 0x0080,
320 FDefaultCollelem = 0x0100,
322 FNonBrowsable = 0x0400,
323 FReplaceable = 0x0800,
324 FImmediateBind = 0x1000,
329 [System.Runtime.InteropServices.ComVisible(true)]
330 public enum TypeLibVarFlags
335 FRequestEdit = 0x0008,
336 FDisplayBind = 0x0010,
337 FDefaultBind = 0x0020,
339 FRestricted = 0x0080,
340 FDefaultCollelem = 0x0100,
342 FNonBrowsable = 0x0400,
343 FReplaceable = 0x0800,
344 FImmediateBind = 0x1000,
347 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct, Inherited = false)]
348 [System.Runtime.InteropServices.ComVisible(true)]
349 public sealed class TypeLibTypeAttribute : Attribute
351 internal TypeLibTypeFlags _val;
352 public TypeLibTypeAttribute(TypeLibTypeFlags flags)
356 public TypeLibTypeAttribute(short flags)
358 _val = (TypeLibTypeFlags)flags;
360 public TypeLibTypeFlags Value { get {return _val;} }
363 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
364 [System.Runtime.InteropServices.ComVisible(true)]
365 public sealed class TypeLibFuncAttribute : Attribute
367 internal TypeLibFuncFlags _val;
368 public TypeLibFuncAttribute(TypeLibFuncFlags flags)
372 public TypeLibFuncAttribute(short flags)
374 _val = (TypeLibFuncFlags)flags;
376 public TypeLibFuncFlags Value { get {return _val;} }
379 [AttributeUsage(AttributeTargets.Field, Inherited = false)]
380 [System.Runtime.InteropServices.ComVisible(true)]
381 public sealed class TypeLibVarAttribute : Attribute
383 internal TypeLibVarFlags _val;
384 public TypeLibVarAttribute(TypeLibVarFlags flags)
388 public TypeLibVarAttribute(short flags)
390 _val = (TypeLibVarFlags)flags;
392 public TypeLibVarFlags Value { get {return _val;} }
396 [System.Runtime.InteropServices.ComVisible(true)]
435 VT_STREAMED_OBJECT = 68,
436 VT_STORED_OBJECT = 69,
446 [System.Runtime.InteropServices.ComVisible(true)]
447 // Note that this enum should remain in-[....] with the CorNativeType enum in corhdr.h
448 public enum UnmanagedType
450 Bool = 0x2, // 4 byte boolean value (true != 0, false == 0)
452 I1 = 0x3, // 1 byte signed value
454 U1 = 0x4, // 1 byte unsigned value
456 I2 = 0x5, // 2 byte signed value
458 U2 = 0x6, // 2 byte unsigned value
460 I4 = 0x7, // 4 byte signed value
462 U4 = 0x8, // 4 byte unsigned value
464 I8 = 0x9, // 8 byte signed value
466 U8 = 0xa, // 8 byte unsigned value
468 R4 = 0xb, // 4 byte floating point
470 R8 = 0xc, // 8 byte floating point
472 Currency = 0xf, // A currency
474 BStr = 0x13, // OLE Unicode BSTR
476 LPStr = 0x14, // Ptr to SBCS string
478 LPWStr = 0x15, // Ptr to Unicode string
480 LPTStr = 0x16, // Ptr to OS preferred (SBCS/Unicode) string
482 ByValTStr = 0x17, // OS preferred (SBCS/Unicode) inline string (only valid in structs)
484 IUnknown = 0x19, // COM IUnknown pointer.
486 IDispatch = 0x1a, // COM IDispatch pointer
488 Struct = 0x1b, // Structure
490 Interface = 0x1c, // COM interface
492 SafeArray = 0x1d, // OLE SafeArray
494 ByValArray = 0x1e, // Array of fixed size (only valid in structs)
496 SysInt = 0x1f, // Hardware natural sized signed integer
502 AnsiBStr = 0x23, // OLE BSTR containing SBCS characters
504 TBStr = 0x24, // Ptr to OS preferred (SBCS/Unicode) BSTR
506 VariantBool = 0x25, // OLE defined BOOLEAN (2 bytes, true == -1, false == 0)
508 FunctionPtr = 0x26, // Function pointer
510 AsAny = 0x28, // Paired with Object type and does runtime marshalling determination
512 LPArray = 0x2a, // C style array
514 LPStruct = 0x2b, // Pointer to a structure
516 CustomMarshaler = 0x2c,
520 [System.Runtime.InteropServices.ComVisible(false)]
523 [System.Runtime.InteropServices.ComVisible(false)]
524 HString = 0x2f, // Windows Runtime HSTRING
526 [System.Runtime.InteropServices.ComVisible(false)]
527 LPUTF8Str = 0x30, // UTF8 string
531 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.ReturnValue, Inherited = false)]
532 [System.Runtime.InteropServices.ComVisible(true)]
533 public unsafe sealed class MarshalAsAttribute : Attribute
536 internal MarshalAsAttribute Copy ()
538 return (MarshalAsAttribute)this.MemberwiseClone ();
541 [System.Security.SecurityCritical] // auto-generated
542 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
544 return GetCustomAttribute(parameter.MetadataToken, parameter.GetRuntimeModule());
547 [System.Security.SecurityCritical] // auto-generated
548 internal static bool IsDefined(RuntimeParameterInfo parameter)
550 return GetCustomAttribute(parameter) != null;
553 [System.Security.SecurityCritical] // auto-generated
554 internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
556 return GetCustomAttribute(field.MetadataToken, field.GetRuntimeModule()); ;
559 [System.Security.SecurityCritical] // auto-generated
560 internal static bool IsDefined(RuntimeFieldInfo field)
562 return GetCustomAttribute(field) != null;
565 [System.Security.SecurityCritical] // auto-generated
566 internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
568 UnmanagedType unmanagedType, arraySubType;
569 VarEnum safeArraySubType;
570 int sizeParamIndex = 0, sizeConst = 0;
571 string marshalTypeName = null, marshalCookie = null, safeArrayUserDefinedTypeName = null;
572 int iidParamIndex = 0;
573 ConstArray nativeType = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);
575 if (nativeType.Length == 0)
578 MetadataImport.GetMarshalAs(nativeType,
579 out unmanagedType, out safeArraySubType, out safeArrayUserDefinedTypeName, out arraySubType, out sizeParamIndex,
580 out sizeConst, out marshalTypeName, out marshalCookie, out iidParamIndex);
582 RuntimeType safeArrayUserDefinedType = safeArrayUserDefinedTypeName == null || safeArrayUserDefinedTypeName.Length == 0 ? null :
583 RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedTypeName, scope);
584 RuntimeType marshalTypeRef = null;
588 marshalTypeRef = marshalTypeName == null ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalTypeName, scope);
590 catch (System.TypeLoadException)
592 // The user may have supplied a bad type name string causing this TypeLoadException
593 // Regardless, we return the bad type name
594 Contract.Assert(marshalTypeName != null);
597 return new MarshalAsAttribute(
598 unmanagedType, safeArraySubType, safeArrayUserDefinedType, arraySubType,
599 (short)sizeParamIndex, sizeConst, marshalTypeName, marshalTypeRef, marshalCookie, iidParamIndex);
602 internal MarshalAsAttribute(UnmanagedType val, VarEnum safeArraySubType, RuntimeType safeArrayUserDefinedSubType, UnmanagedType arraySubType,
603 short sizeParamIndex, int sizeConst, string marshalType, RuntimeType marshalTypeRef, string marshalCookie, int iidParamIndex)
606 SafeArraySubType = safeArraySubType;
607 SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType;
608 IidParameterIndex = iidParamIndex;
609 ArraySubType = arraySubType;
610 SizeParamIndex = sizeParamIndex;
611 SizeConst = sizeConst;
612 MarshalType = marshalType;
613 MarshalTypeRef = marshalTypeRef;
614 MarshalCookie = marshalCookie;
617 internal UnmanagedType _val;
618 public MarshalAsAttribute(UnmanagedType unmanagedType)
620 _val = unmanagedType;
622 public MarshalAsAttribute(short unmanagedType)
624 _val = (UnmanagedType)unmanagedType;
626 public UnmanagedType Value { get { return _val; } }
628 // Fields used with SubType = SafeArray.
629 public VarEnum SafeArraySubType;
630 public Type SafeArrayUserDefinedSubType;
632 // Field used with iid_is attribute (interface pointers).
633 public int IidParameterIndex;
635 // Fields used with SubType = ByValArray and LPArray.
636 // Array size = parameter(PI) * PM + C
637 public UnmanagedType ArraySubType;
638 public short SizeParamIndex; // param index PI
639 public int SizeConst; // constant C
641 // Fields used with SubType = CustomMarshaler
642 [System.Runtime.InteropServices.ComVisible(true)]
643 public String MarshalType; // Name of marshaler class
644 [System.Runtime.InteropServices.ComVisible(true)]
645 public Type MarshalTypeRef; // Type of marshaler class
646 public String MarshalCookie; // cookie to pass to marshaler
649 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
650 [System.Runtime.InteropServices.ComVisible(true)]
651 public sealed class ComImportAttribute : Attribute
653 internal static Attribute GetCustomAttribute(RuntimeType type)
655 if ((type.Attributes & TypeAttributes.Import) == 0)
658 return new ComImportAttribute();
661 internal static bool IsDefined(RuntimeType type)
663 return (type.Attributes & TypeAttributes.Import) != 0;
666 public ComImportAttribute()
671 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, Inherited = false)]
672 [System.Runtime.InteropServices.ComVisible(true)]
673 public sealed class GuidAttribute : Attribute
675 internal String _val;
676 public GuidAttribute(String guid)
680 public String Value { get { return _val; } }
683 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
684 [System.Runtime.InteropServices.ComVisible(true)]
685 public sealed class PreserveSigAttribute : Attribute
687 internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
689 if ((method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) == 0)
692 return new PreserveSigAttribute();
695 internal static bool IsDefined(RuntimeMethodInfo method)
697 return (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
700 public PreserveSigAttribute()
705 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
706 [System.Runtime.InteropServices.ComVisible(true)]
707 public sealed class InAttribute : Attribute
709 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
711 return parameter.IsIn ? new InAttribute() : null;
713 internal static bool IsDefined(RuntimeParameterInfo parameter)
715 return parameter.IsIn;
723 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
724 [System.Runtime.InteropServices.ComVisible(true)]
725 public sealed class OutAttribute : Attribute
727 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
729 return parameter.IsOut ? new OutAttribute() : null;
731 internal static bool IsDefined(RuntimeParameterInfo parameter)
733 return parameter.IsOut;
736 public OutAttribute()
741 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
742 [System.Runtime.InteropServices.ComVisible(true)]
743 public sealed class OptionalAttribute : Attribute
745 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
747 return parameter.IsOptional ? new OptionalAttribute() : null;
749 internal static bool IsDefined(RuntimeParameterInfo parameter)
751 return parameter.IsOptional;
754 public OptionalAttribute()
760 public enum DllImportSearchPath
762 UseDllDirectoryForDependencies = 0x100,
763 ApplicationDirectory = 0x200,
764 UserDirectories = 0x400,
766 SafeDirectories = 0x1000,
767 AssemblyDirectory = 0x2,
771 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method, AllowMultiple = false)]
772 [System.Runtime.InteropServices.ComVisible(false)]
773 public sealed class DefaultDllImportSearchPathsAttribute : Attribute
775 internal DllImportSearchPath _paths;
776 public DefaultDllImportSearchPathsAttribute(DllImportSearchPath paths)
781 public DllImportSearchPath Paths { get { return _paths; } }
784 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
785 [System.Runtime.InteropServices.ComVisible(true)]
786 public unsafe sealed class DllImportAttribute : Attribute
788 [System.Security.SecurityCritical] // auto-generated
789 internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
791 if ((method.Attributes & MethodAttributes.PinvokeImpl) == 0)
795 MetadataImport scope = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
797 string entryPoint, dllName = null;
798 int token = method.MetadataToken;
799 PInvokeAttributes flags = 0;
802 ((MonoMethod)method).GetPInvoke(out flags, out entryPoint, out dllName);
804 scope.GetPInvokeMap(token, out flags, out entryPoint, out dllName);
807 CharSet charSet = CharSet.None;
809 switch (flags & PInvokeAttributes.CharSetMask)
811 case PInvokeAttributes.CharSetNotSpec: charSet = CharSet.None; break;
812 case PInvokeAttributes.CharSetAnsi: charSet = CharSet.Ansi; break;
813 case PInvokeAttributes.CharSetUnicode: charSet = CharSet.Unicode; break;
814 case PInvokeAttributes.CharSetAuto: charSet = CharSet.Auto; break;
816 // Invalid: default to CharSet.None
820 CallingConvention callingConvention = CallingConvention.Cdecl;
822 switch (flags & PInvokeAttributes.CallConvMask)
824 case PInvokeAttributes.CallConvWinapi: callingConvention = CallingConvention.Winapi; break;
825 case PInvokeAttributes.CallConvCdecl: callingConvention = CallingConvention.Cdecl; break;
826 case PInvokeAttributes.CallConvStdcall: callingConvention = CallingConvention.StdCall; break;
827 case PInvokeAttributes.CallConvThiscall: callingConvention = CallingConvention.ThisCall; break;
828 case PInvokeAttributes.CallConvFastcall: callingConvention = CallingConvention.FastCall; break;
830 // Invalid: default to CallingConvention.Cdecl
834 bool exactSpelling = (flags & PInvokeAttributes.NoMangle) != 0;
835 bool setLastError = (flags & PInvokeAttributes.SupportsLastError) != 0;
836 bool bestFitMapping = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
837 bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
838 bool preserveSig = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
840 return new DllImportAttribute(
841 dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig,
842 callingConvention, bestFitMapping, throwOnUnmappableChar);
845 internal static bool IsDefined(RuntimeMethodInfo method)
847 return (method.Attributes & MethodAttributes.PinvokeImpl) != 0;
851 internal DllImportAttribute(
852 string dllName, string entryPoint, CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig,
853 CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
856 EntryPoint = entryPoint;
858 ExactSpelling = exactSpelling;
859 SetLastError = setLastError;
860 PreserveSig = preserveSig;
861 CallingConvention = callingConvention;
862 BestFitMapping = bestFitMapping;
863 ThrowOnUnmappableChar = throwOnUnmappableChar;
866 internal String _val;
868 public DllImportAttribute(String dllName)
872 public String Value { get { return _val; } }
874 public String EntryPoint;
875 public CharSet CharSet;
876 public bool SetLastError;
877 public bool ExactSpelling;
878 public bool PreserveSig;
879 public CallingConvention CallingConvention;
880 public bool BestFitMapping;
881 public bool ThrowOnUnmappableChar;
885 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
886 [System.Runtime.InteropServices.ComVisible(true)]
887 public unsafe sealed class StructLayoutAttribute : Attribute
889 private const int DEFAULT_PACKING_SIZE = 8;
891 [System.Security.SecurityCritical] // auto-generated
892 internal static StructLayoutAttribute GetCustomAttribute(RuntimeType type)
894 if (!IsDefined(type))
897 int pack = 0, size = 0;
898 LayoutKind layoutKind = LayoutKind.Auto;
899 switch (type.Attributes & TypeAttributes.LayoutMask)
901 case TypeAttributes.ExplicitLayout: layoutKind = LayoutKind.Explicit; break;
902 case TypeAttributes.AutoLayout: layoutKind = LayoutKind.Auto; break;
903 case TypeAttributes.SequentialLayout: layoutKind = LayoutKind.Sequential; break;
904 default: Contract.Assume(false); break;
907 CharSet charSet = CharSet.None;
908 switch (type.Attributes & TypeAttributes.StringFormatMask)
910 case TypeAttributes.AnsiClass: charSet = CharSet.Ansi; break;
911 case TypeAttributes.AutoClass: charSet = CharSet.Auto; break;
912 case TypeAttributes.UnicodeClass: charSet = CharSet.Unicode; break;
913 default: Contract.Assume(false); break;
917 type.GetPacking (out pack, out size);
919 type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out pack, out size);
922 // Metadata parameter checking should not have allowed 0 for packing size.
923 // The runtime later converts a packing size of 0 to 8 so do the same here
924 // because it's more useful from a user perspective.
926 pack = DEFAULT_PACKING_SIZE;
928 return new StructLayoutAttribute(layoutKind, pack, size, charSet);
931 internal static bool IsDefined(RuntimeType type)
933 if (type.IsInterface || type.HasElementType || type.IsGenericParameter)
939 internal LayoutKind _val;
941 internal StructLayoutAttribute(LayoutKind layoutKind, int pack, int size, CharSet charSet)
949 public StructLayoutAttribute(LayoutKind layoutKind)
953 public StructLayoutAttribute(short layoutKind)
955 _val = (LayoutKind)layoutKind;
957 public LayoutKind Value { get { return _val; } }
960 public CharSet CharSet;
963 [AttributeUsage(AttributeTargets.Field, Inherited = false)]
964 [System.Runtime.InteropServices.ComVisible(true)]
965 public unsafe sealed class FieldOffsetAttribute : Attribute
967 [System.Security.SecurityCritical] // auto-generated
968 internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
972 if (field.DeclaringType != null &&
974 (fieldOffset = field.GetFieldOffset ()) >= 0)
976 field.GetRuntimeModule().MetadataImport.GetFieldOffset(field.DeclaringType.MetadataToken, field.MetadataToken, out fieldOffset))
978 return new FieldOffsetAttribute(fieldOffset);
983 [System.Security.SecurityCritical] // auto-generated
984 internal static bool IsDefined(RuntimeFieldInfo field)
986 return GetCustomAttribute(field) != null;
990 public FieldOffsetAttribute(int offset)
994 public int Value { get { return _val; } }
997 #if FEATURE_COMINTEROP || MOBILE_LEGACY
998 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
999 [System.Runtime.InteropServices.ComVisible(true)]
1000 public sealed class ComAliasNameAttribute : Attribute
1002 internal String _val;
1003 public ComAliasNameAttribute(String alias)
1007 public String Value { get {return _val;} }
1010 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
1011 [System.Runtime.InteropServices.ComVisible(true)]
1012 public sealed class AutomationProxyAttribute : Attribute
1015 public AutomationProxyAttribute(bool val)
1019 public bool Value { get {return _val;} }
1022 [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
1023 [System.Runtime.InteropServices.ComVisible(true)]
1024 public sealed class PrimaryInteropAssemblyAttribute : Attribute
1026 internal int _major;
1027 internal int _minor;
1029 public PrimaryInteropAssemblyAttribute(int major, int minor)
1035 public int MajorVersion { get {return _major;} }
1036 public int MinorVersion { get {return _minor;} }
1039 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1040 [System.Runtime.InteropServices.ComVisible(true)]
1041 public sealed class CoClassAttribute : Attribute
1043 internal Type _CoClass;
1045 public CoClassAttribute(Type coClass)
1050 public Type CoClass { get { return _CoClass; } }
1053 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1054 [System.Runtime.InteropServices.ComVisible(true)]
1055 public sealed class ComEventInterfaceAttribute : Attribute
1057 internal Type _SourceInterface;
1058 internal Type _EventProvider;
1060 public ComEventInterfaceAttribute(Type SourceInterface, Type EventProvider)
1062 _SourceInterface = SourceInterface;
1063 _EventProvider = EventProvider;
1066 public Type SourceInterface { get {return _SourceInterface;} }
1067 public Type EventProvider { get {return _EventProvider;} }
1070 #if FEATURE_COMINTEROP || MOBILE_LEGACY
1071 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1072 [System.Runtime.InteropServices.ComVisible(true)]
1073 public sealed class TypeLibVersionAttribute : Attribute
1075 internal int _major;
1076 internal int _minor;
1078 public TypeLibVersionAttribute(int major, int minor)
1084 public int MajorVersion { get {return _major;} }
1085 public int MinorVersion { get {return _minor;} }
1088 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1089 [System.Runtime.InteropServices.ComVisible(true)]
1090 public sealed class ComCompatibleVersionAttribute : Attribute
1092 internal int _major;
1093 internal int _minor;
1094 internal int _build;
1095 internal int _revision;
1097 public ComCompatibleVersionAttribute(int major, int minor, int build, int revision)
1102 _revision = revision;
1105 public int MajorVersion { get {return _major;} }
1106 public int MinorVersion { get {return _minor;} }
1107 public int BuildNumber { get {return _build;} }
1108 public int RevisionNumber { get {return _revision;} }
1111 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
1112 [System.Runtime.InteropServices.ComVisible(true)]
1113 public sealed class BestFitMappingAttribute : Attribute
1115 internal bool _bestFitMapping;
1117 public BestFitMappingAttribute(bool BestFitMapping)
1119 _bestFitMapping = BestFitMapping;
1122 public bool BestFitMapping { get { return _bestFitMapping; } }
1123 public bool ThrowOnUnmappableChar;
1126 [AttributeUsage(AttributeTargets.Module, Inherited = false)]
1127 [System.Runtime.InteropServices.ComVisible(true)]
1128 public sealed class DefaultCharSetAttribute : Attribute
1130 internal CharSet _CharSet;
1132 public DefaultCharSetAttribute(CharSet charSet)
1137 public CharSet CharSet { get { return _CharSet; } }
1140 [Obsolete("This attribute has been deprecated. Application Domains no longer respect Activation Context boundaries in IDispatch calls.", false)]
1141 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1142 [System.Runtime.InteropServices.ComVisible(true)]
1143 public sealed class SetWin32ContextInIDispatchAttribute : Attribute
1145 public SetWin32ContextInIDispatchAttribute()
1150 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
1151 [System.Runtime.InteropServices.ComVisible(false)]
1152 public sealed class ManagedToNativeComInteropStubAttribute : Attribute
1154 internal Type _classType;
1155 internal String _methodName;
1157 public ManagedToNativeComInteropStubAttribute(Type classType, String methodName)
1159 _classType = classType;
1160 _methodName = methodName;
1163 public Type ClassType { get { return _classType; } }
1164 public String MethodName { get { return _methodName; } }