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 #if FEATURE_COMINTEROP
83 [System.Runtime.InteropServices.ComVisible(false)]
84 InterfaceIsIInspectable = 3,
85 #endif // FEATURE_COMINTEROP
88 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
89 [System.Runtime.InteropServices.ComVisible(true)]
90 public sealed class InterfaceTypeAttribute : Attribute
92 internal ComInterfaceType _val;
93 public InterfaceTypeAttribute(ComInterfaceType interfaceType)
97 public InterfaceTypeAttribute(short interfaceType)
99 _val = (ComInterfaceType)interfaceType;
101 public ComInterfaceType Value { get { return _val; } }
104 [AttributeUsage(AttributeTargets.Class, Inherited = false)]
105 [System.Runtime.InteropServices.ComVisible(true)]
106 public sealed class ComDefaultInterfaceAttribute : Attribute
110 public ComDefaultInterfaceAttribute(Type defaultInterface)
112 _val = defaultInterface;
115 public Type Value { get { return _val; } }
119 [System.Runtime.InteropServices.ComVisible(true)]
120 public enum ClassInterfaceType
127 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class, Inherited = false)]
128 [System.Runtime.InteropServices.ComVisible(true)]
129 public sealed class ClassInterfaceAttribute : Attribute
131 internal ClassInterfaceType _val;
132 public ClassInterfaceAttribute(ClassInterfaceType classInterfaceType)
134 _val = classInterfaceType;
137 public ClassInterfaceAttribute(short classInterfaceType)
139 _val = (ClassInterfaceType)classInterfaceType;
141 public ClassInterfaceType Value { get { return _val; } }
144 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Delegate | AttributeTargets.Enum | AttributeTargets.Field | AttributeTargets.Method | AttributeTargets.Property, Inherited = false)]
145 [System.Runtime.InteropServices.ComVisible(true)]
146 public sealed class ComVisibleAttribute : Attribute
149 public ComVisibleAttribute(bool visibility)
153 public bool Value { get { return _val; } }
156 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
157 [System.Runtime.InteropServices.ComVisible(true)]
158 public sealed class TypeLibImportClassAttribute : Attribute
160 internal String _importClassName;
161 public TypeLibImportClassAttribute(Type importClass)
163 _importClassName = importClass.ToString();
165 public String Value { get { return _importClassName; } }
168 #if FEATURE_COMINTEROP
169 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
170 [System.Runtime.InteropServices.ComVisible(true)]
171 public sealed class LCIDConversionAttribute : Attribute
174 public LCIDConversionAttribute(int lcid)
178 public int Value { get {return _val;} }
181 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
182 [System.Runtime.InteropServices.ComVisible(true)]
183 public sealed class ComRegisterFunctionAttribute : Attribute
185 public ComRegisterFunctionAttribute()
190 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
191 [System.Runtime.InteropServices.ComVisible(true)]
192 public sealed class ComUnregisterFunctionAttribute : Attribute
194 public ComUnregisterFunctionAttribute()
199 [AttributeUsage(AttributeTargets.Class, Inherited = false)]
200 [System.Runtime.InteropServices.ComVisible(true)]
201 public sealed class ProgIdAttribute : Attribute
203 internal String _val;
204 public ProgIdAttribute(String progId)
208 public String Value { get {return _val;} }
211 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
212 [System.Runtime.InteropServices.ComVisible(true)]
213 public sealed class ImportedFromTypeLibAttribute : Attribute
215 internal String _val;
216 public ImportedFromTypeLibAttribute(String tlbFile)
220 public String Value { get {return _val;} }
223 [Obsolete("The IDispatchImplAttribute is deprecated.", false)]
225 [System.Runtime.InteropServices.ComVisible(true)]
226 public enum IDispatchImplType
228 SystemDefinedImpl = 0,
233 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Assembly, Inherited = false)]
234 [Obsolete("This attribute is deprecated and will be removed in a future version.", false)]
235 [System.Runtime.InteropServices.ComVisible(true)]
236 public sealed class IDispatchImplAttribute : Attribute
238 internal IDispatchImplType _val;
239 public IDispatchImplAttribute(IDispatchImplType implType)
243 public IDispatchImplAttribute(short implType)
245 _val = (IDispatchImplType)implType;
247 public IDispatchImplType Value { get {return _val;} }
251 [AttributeUsage(AttributeTargets.Class, Inherited = true)]
252 [System.Runtime.InteropServices.ComVisible(true)]
253 public sealed class ComSourceInterfacesAttribute : Attribute
255 internal String _val;
256 public ComSourceInterfacesAttribute(String sourceInterfaces)
258 _val = sourceInterfaces;
260 public ComSourceInterfacesAttribute(Type sourceInterface)
262 _val = sourceInterface.FullName;
264 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2)
266 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName;
268 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3)
270 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName;
272 public ComSourceInterfacesAttribute(Type sourceInterface1, Type sourceInterface2, Type sourceInterface3, Type sourceInterface4)
274 _val = sourceInterface1.FullName + "\0" + sourceInterface2.FullName + "\0" + sourceInterface3.FullName + "\0" + sourceInterface4.FullName;
276 public String Value { get {return _val;} }
279 #if FEATURE_COMINTEROP
280 [AttributeUsage(AttributeTargets.All, Inherited = false)]
281 [System.Runtime.InteropServices.ComVisible(true)]
282 public sealed class ComConversionLossAttribute : Attribute
284 public ComConversionLossAttribute()
291 [System.Runtime.InteropServices.ComVisible(true)]
292 public enum TypeLibTypeFlags
301 FNonExtensible = 0x0080,
302 FOleAutomation = 0x0100,
303 FRestricted = 0x0200,
304 FAggregatable = 0x0400,
305 FReplaceable = 0x0800,
306 FDispatchable = 0x1000,
307 FReverseBind = 0x2000,
312 [System.Runtime.InteropServices.ComVisible(true)]
313 public enum TypeLibFuncFlags
315 FRestricted = 0x0001,
318 FRequestEdit = 0x0008,
319 FDisplayBind = 0x0010,
320 FDefaultBind = 0x0020,
322 FUsesGetLastError = 0x0080,
323 FDefaultCollelem = 0x0100,
325 FNonBrowsable = 0x0400,
326 FReplaceable = 0x0800,
327 FImmediateBind = 0x1000,
332 [System.Runtime.InteropServices.ComVisible(true)]
333 public enum TypeLibVarFlags
338 FRequestEdit = 0x0008,
339 FDisplayBind = 0x0010,
340 FDefaultBind = 0x0020,
342 FRestricted = 0x0080,
343 FDefaultCollelem = 0x0100,
345 FNonBrowsable = 0x0400,
346 FReplaceable = 0x0800,
347 FImmediateBind = 0x1000,
350 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface | AttributeTargets.Enum | AttributeTargets.Struct, Inherited = false)]
351 [System.Runtime.InteropServices.ComVisible(true)]
352 public sealed class TypeLibTypeAttribute : Attribute
354 internal TypeLibTypeFlags _val;
355 public TypeLibTypeAttribute(TypeLibTypeFlags flags)
359 public TypeLibTypeAttribute(short flags)
361 _val = (TypeLibTypeFlags)flags;
363 public TypeLibTypeFlags Value { get {return _val;} }
366 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
367 [System.Runtime.InteropServices.ComVisible(true)]
368 public sealed class TypeLibFuncAttribute : Attribute
370 internal TypeLibFuncFlags _val;
371 public TypeLibFuncAttribute(TypeLibFuncFlags flags)
375 public TypeLibFuncAttribute(short flags)
377 _val = (TypeLibFuncFlags)flags;
379 public TypeLibFuncFlags Value { get {return _val;} }
382 [AttributeUsage(AttributeTargets.Field, Inherited = false)]
383 [System.Runtime.InteropServices.ComVisible(true)]
384 public sealed class TypeLibVarAttribute : Attribute
386 internal TypeLibVarFlags _val;
387 public TypeLibVarAttribute(TypeLibVarFlags flags)
391 public TypeLibVarAttribute(short flags)
393 _val = (TypeLibVarFlags)flags;
395 public TypeLibVarFlags Value { get {return _val;} }
397 #endif //FEATURE_COMINTEROP
400 [System.Runtime.InteropServices.ComVisible(true)]
412 #if FEATURE_COMINTEROP
414 #endif // FEATURE_COMINTEROP
441 VT_STREAMED_OBJECT = 68,
442 VT_STORED_OBJECT = 69,
452 [System.Runtime.InteropServices.ComVisible(true)]
453 // Note that this enum should remain in-[....] with the CorNativeType enum in corhdr.h
454 public enum UnmanagedType
456 Bool = 0x2, // 4 byte boolean value (true != 0, false == 0)
458 I1 = 0x3, // 1 byte signed value
460 U1 = 0x4, // 1 byte unsigned value
462 I2 = 0x5, // 2 byte signed value
464 U2 = 0x6, // 2 byte unsigned value
466 I4 = 0x7, // 4 byte signed value
468 U4 = 0x8, // 4 byte unsigned value
470 I8 = 0x9, // 8 byte signed value
472 U8 = 0xa, // 8 byte unsigned value
474 R4 = 0xb, // 4 byte floating point
476 R8 = 0xc, // 8 byte floating point
478 Currency = 0xf, // A currency
480 #if FEATURE_COMINTEROP || FEATURE_LEGACYNETCF || MONO
481 BStr = 0x13, // OLE Unicode BSTR
482 #endif //FEATURE_COMINTEROP || FEATURE_LEGACYNETCF
484 LPStr = 0x14, // Ptr to SBCS string
486 LPWStr = 0x15, // Ptr to Unicode string
488 LPTStr = 0x16, // Ptr to OS preferred (SBCS/Unicode) string
490 ByValTStr = 0x17, // OS preferred (SBCS/Unicode) inline string (only valid in structs)
492 IUnknown = 0x19, // COM IUnknown pointer.
494 #if FEATURE_COMINTEROP || FEATURE_LEGACYNETCF || MONO
495 IDispatch = 0x1a, // COM IDispatch pointer
496 #endif //FEATURE_COMINTEROP || FEATURE_LEGACYNETCF
498 Struct = 0x1b, // Structure
500 #if FEATURE_COMINTEROP || FEATURE_LEGACYNETCF || MONO
501 Interface = 0x1c, // COM interface
503 SafeArray = 0x1d, // OLE SafeArray
504 #endif //FEATURE_COMINTEROP || FEATURE_LEGACYNETCF
506 ByValArray = 0x1e, // Array of fixed size (only valid in structs)
508 SysInt = 0x1f, // Hardware natural sized signed integer
512 #if FEATURE_COMINTEROP || FEATURE_LEGACYNETCF || MONO
515 AnsiBStr = 0x23, // OLE BSTR containing SBCS characters
517 TBStr = 0x24, // Ptr to OS preferred (SBCS/Unicode) BSTR
519 VariantBool = 0x25, // OLE defined BOOLEAN (2 bytes, true == -1, false == 0)
520 #endif //FEATURE_COMINTEROP || FEATURE_LEGACYNETCF
522 FunctionPtr = 0x26, // Function pointer
524 AsAny = 0x28, // Paired with Object type and does runtime marshalling determination
526 LPArray = 0x2a, // C style array
528 LPStruct = 0x2b, // Pointer to a structure
530 #if FEATURE_COMINTEROP || FEATURE_LEGACYNETCF || MONO
531 CustomMarshaler = 0x2c,
532 #endif //FEATURE_COMINTEROP || FEATURE_LEGACYNETCF
536 #if FEATURE_COMINTEROP
537 [System.Runtime.InteropServices.ComVisible(false)]
540 [System.Runtime.InteropServices.ComVisible(false)]
541 HString = 0x2f, // Windows Runtime HSTRING
543 #endif //FEATURE_COMINTEROP
547 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.ReturnValue, Inherited = false)]
548 [System.Runtime.InteropServices.ComVisible(true)]
549 public unsafe sealed class MarshalAsAttribute : Attribute
552 internal MarshalAsAttribute Copy ()
554 return (MarshalAsAttribute)this.MemberwiseClone ();
557 [System.Security.SecurityCritical] // auto-generated
558 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
560 return GetCustomAttribute(parameter.MetadataToken, parameter.GetRuntimeModule());
563 [System.Security.SecurityCritical] // auto-generated
564 internal static bool IsDefined(RuntimeParameterInfo parameter)
566 return GetCustomAttribute(parameter) != null;
569 [System.Security.SecurityCritical] // auto-generated
570 internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
572 return GetCustomAttribute(field.MetadataToken, field.GetRuntimeModule()); ;
575 [System.Security.SecurityCritical] // auto-generated
576 internal static bool IsDefined(RuntimeFieldInfo field)
578 return GetCustomAttribute(field) != null;
581 [System.Security.SecurityCritical] // auto-generated
582 internal static Attribute GetCustomAttribute(int token, RuntimeModule scope)
584 UnmanagedType unmanagedType, arraySubType;
585 VarEnum safeArraySubType;
586 int sizeParamIndex = 0, sizeConst = 0;
587 string marshalTypeName = null, marshalCookie = null, safeArrayUserDefinedTypeName = null;
588 int iidParamIndex = 0;
589 ConstArray nativeType = ModuleHandle.GetMetadataImport(scope.GetNativeHandle()).GetFieldMarshal(token);
591 if (nativeType.Length == 0)
594 MetadataImport.GetMarshalAs(nativeType,
595 out unmanagedType, out safeArraySubType, out safeArrayUserDefinedTypeName, out arraySubType, out sizeParamIndex,
596 out sizeConst, out marshalTypeName, out marshalCookie, out iidParamIndex);
598 RuntimeType safeArrayUserDefinedType = safeArrayUserDefinedTypeName == null || safeArrayUserDefinedTypeName.Length == 0 ? null :
599 RuntimeTypeHandle.GetTypeByNameUsingCARules(safeArrayUserDefinedTypeName, scope);
600 RuntimeType marshalTypeRef = null;
604 marshalTypeRef = marshalTypeName == null ? null : RuntimeTypeHandle.GetTypeByNameUsingCARules(marshalTypeName, scope);
606 catch (System.TypeLoadException)
608 // The user may have supplied a bad type name string causing this TypeLoadException
609 // Regardless, we return the bad type name
610 Contract.Assert(marshalTypeName != null);
613 return new MarshalAsAttribute(
614 unmanagedType, safeArraySubType, safeArrayUserDefinedType, arraySubType,
615 (short)sizeParamIndex, sizeConst, marshalTypeName, marshalTypeRef, marshalCookie, iidParamIndex);
618 internal MarshalAsAttribute(UnmanagedType val, VarEnum safeArraySubType, RuntimeType safeArrayUserDefinedSubType, UnmanagedType arraySubType,
619 short sizeParamIndex, int sizeConst, string marshalType, RuntimeType marshalTypeRef, string marshalCookie, int iidParamIndex)
622 #if FEATURE_COMINTEROP
623 SafeArraySubType = safeArraySubType;
624 SafeArrayUserDefinedSubType = safeArrayUserDefinedSubType;
625 IidParameterIndex = iidParamIndex;
626 #endif // FEATURE_COMINTEROP
627 ArraySubType = arraySubType;
628 SizeParamIndex = sizeParamIndex;
629 SizeConst = sizeConst;
630 MarshalType = marshalType;
631 MarshalTypeRef = marshalTypeRef;
632 MarshalCookie = marshalCookie;
635 internal UnmanagedType _val;
636 public MarshalAsAttribute(UnmanagedType unmanagedType)
638 _val = unmanagedType;
640 public MarshalAsAttribute(short unmanagedType)
642 _val = (UnmanagedType)unmanagedType;
644 public UnmanagedType Value { get { return _val; } }
646 #if FEATURE_COMINTEROP
647 // Fields used with SubType = SafeArray.
648 public VarEnum SafeArraySubType;
649 public Type SafeArrayUserDefinedSubType;
651 // Field used with iid_is attribute (interface pointers).
652 public int IidParameterIndex;
653 #endif // FEATURE_COMINTEROP
655 // Fields used with SubType = ByValArray and LPArray.
656 // Array size = parameter(PI) * PM + C
657 public UnmanagedType ArraySubType;
658 public short SizeParamIndex; // param index PI
659 public int SizeConst; // constant C
661 // Fields used with SubType = CustomMarshaler
662 [System.Runtime.InteropServices.ComVisible(true)]
663 public String MarshalType; // Name of marshaler class
664 [System.Runtime.InteropServices.ComVisible(true)]
665 public Type MarshalTypeRef; // Type of marshaler class
666 public String MarshalCookie; // cookie to pass to marshaler
669 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
670 [System.Runtime.InteropServices.ComVisible(true)]
674 sealed class ComImportAttribute : Attribute
676 internal static Attribute GetCustomAttribute(RuntimeType type)
678 if ((type.Attributes & TypeAttributes.Import) == 0)
681 return new ComImportAttribute();
684 internal static bool IsDefined(RuntimeType type)
686 return (type.Attributes & TypeAttributes.Import) != 0;
689 public ComImportAttribute()
694 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Enum | AttributeTargets.Struct | AttributeTargets.Delegate, Inherited = false)]
695 [System.Runtime.InteropServices.ComVisible(true)]
696 public sealed class GuidAttribute : Attribute
698 internal String _val;
699 public GuidAttribute(String guid)
703 public String Value { get { return _val; } }
706 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
707 [System.Runtime.InteropServices.ComVisible(true)]
708 public sealed class PreserveSigAttribute : Attribute
710 internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
712 if ((method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) == 0)
715 return new PreserveSigAttribute();
718 internal static bool IsDefined(RuntimeMethodInfo method)
720 return (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
723 public PreserveSigAttribute()
728 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
729 [System.Runtime.InteropServices.ComVisible(true)]
730 public sealed class InAttribute : Attribute
732 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
734 return parameter.IsIn ? new InAttribute() : null;
736 internal static bool IsDefined(RuntimeParameterInfo parameter)
738 return parameter.IsIn;
746 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
747 [System.Runtime.InteropServices.ComVisible(true)]
748 public sealed class OutAttribute : Attribute
750 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
752 return parameter.IsOut ? new OutAttribute() : null;
754 internal static bool IsDefined(RuntimeParameterInfo parameter)
756 return parameter.IsOut;
759 public OutAttribute()
764 [AttributeUsage(AttributeTargets.Parameter, Inherited = false)]
765 [System.Runtime.InteropServices.ComVisible(true)]
766 public sealed class OptionalAttribute : Attribute
768 internal static Attribute GetCustomAttribute(RuntimeParameterInfo parameter)
770 return parameter.IsOptional ? new OptionalAttribute() : null;
772 internal static bool IsDefined(RuntimeParameterInfo parameter)
774 return parameter.IsOptional;
777 public OptionalAttribute()
783 public enum DllImportSearchPath
785 UseDllDirectoryForDependencies = 0x100,
786 ApplicationDirectory = 0x200,
787 UserDirectories = 0x400,
789 SafeDirectories = 0x1000,
790 AssemblyDirectory = 0x2,
794 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Method, AllowMultiple = false)]
795 [System.Runtime.InteropServices.ComVisible(false)]
796 public sealed class DefaultDllImportSearchPathsAttribute : Attribute
798 internal DllImportSearchPath _paths;
799 public DefaultDllImportSearchPathsAttribute(DllImportSearchPath paths)
804 public DllImportSearchPath Paths { get { return _paths; } }
807 [AttributeUsage(AttributeTargets.Method, Inherited = false)]
808 [System.Runtime.InteropServices.ComVisible(true)]
809 public unsafe sealed class DllImportAttribute : Attribute
811 [System.Security.SecurityCritical] // auto-generated
812 internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
814 if ((method.Attributes & MethodAttributes.PinvokeImpl) == 0)
818 MetadataImport scope = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
820 string entryPoint, dllName = null;
821 int token = method.MetadataToken;
822 PInvokeAttributes flags = 0;
825 ((MonoMethod)method).GetPInvoke(out flags, out entryPoint, out dllName);
827 scope.GetPInvokeMap(token, out flags, out entryPoint, out dllName);
830 CharSet charSet = CharSet.None;
832 switch (flags & PInvokeAttributes.CharSetMask)
834 case PInvokeAttributes.CharSetNotSpec: charSet = CharSet.None; break;
835 case PInvokeAttributes.CharSetAnsi: charSet = CharSet.Ansi; break;
836 case PInvokeAttributes.CharSetUnicode: charSet = CharSet.Unicode; break;
837 case PInvokeAttributes.CharSetAuto: charSet = CharSet.Auto; break;
839 // Invalid: default to CharSet.None
843 CallingConvention callingConvention = CallingConvention.Cdecl;
845 switch (flags & PInvokeAttributes.CallConvMask)
847 case PInvokeAttributes.CallConvWinapi: callingConvention = CallingConvention.Winapi; break;
848 case PInvokeAttributes.CallConvCdecl: callingConvention = CallingConvention.Cdecl; break;
849 case PInvokeAttributes.CallConvStdcall: callingConvention = CallingConvention.StdCall; break;
850 case PInvokeAttributes.CallConvThiscall: callingConvention = CallingConvention.ThisCall; break;
851 case PInvokeAttributes.CallConvFastcall: callingConvention = CallingConvention.FastCall; break;
853 // Invalid: default to CallingConvention.Cdecl
857 bool exactSpelling = (flags & PInvokeAttributes.NoMangle) != 0;
858 bool setLastError = (flags & PInvokeAttributes.SupportsLastError) != 0;
859 bool bestFitMapping = (flags & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
860 bool throwOnUnmappableChar = (flags & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
861 bool preserveSig = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != 0;
863 return new DllImportAttribute(
864 dllName, entryPoint, charSet, exactSpelling, setLastError, preserveSig,
865 callingConvention, bestFitMapping, throwOnUnmappableChar);
868 internal static bool IsDefined(RuntimeMethodInfo method)
870 return (method.Attributes & MethodAttributes.PinvokeImpl) != 0;
874 internal DllImportAttribute(
875 string dllName, string entryPoint, CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig,
876 CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
879 EntryPoint = entryPoint;
881 ExactSpelling = exactSpelling;
882 SetLastError = setLastError;
883 PreserveSig = preserveSig;
884 CallingConvention = callingConvention;
885 BestFitMapping = bestFitMapping;
886 ThrowOnUnmappableChar = throwOnUnmappableChar;
889 internal String _val;
891 public DllImportAttribute(String dllName)
895 public String Value { get { return _val; } }
897 public String EntryPoint;
898 public CharSet CharSet;
899 public bool SetLastError;
900 public bool ExactSpelling;
901 public bool PreserveSig;
902 public CallingConvention CallingConvention;
903 public bool BestFitMapping;
904 public bool ThrowOnUnmappableChar;
908 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
909 [System.Runtime.InteropServices.ComVisible(true)]
910 public unsafe sealed class StructLayoutAttribute : Attribute
912 private const int DEFAULT_PACKING_SIZE = 8;
914 [System.Security.SecurityCritical] // auto-generated
915 internal static StructLayoutAttribute GetCustomAttribute(RuntimeType type)
917 if (!IsDefined(type))
920 int pack = 0, size = 0;
921 LayoutKind layoutKind = LayoutKind.Auto;
922 switch (type.Attributes & TypeAttributes.LayoutMask)
924 case TypeAttributes.ExplicitLayout: layoutKind = LayoutKind.Explicit; break;
925 case TypeAttributes.AutoLayout: layoutKind = LayoutKind.Auto; break;
926 case TypeAttributes.SequentialLayout: layoutKind = LayoutKind.Sequential; break;
927 default: Contract.Assume(false); break;
930 CharSet charSet = CharSet.None;
931 switch (type.Attributes & TypeAttributes.StringFormatMask)
933 case TypeAttributes.AnsiClass: charSet = CharSet.Ansi; break;
934 case TypeAttributes.AutoClass: charSet = CharSet.Auto; break;
935 case TypeAttributes.UnicodeClass: charSet = CharSet.Unicode; break;
936 default: Contract.Assume(false); break;
940 type.GetPacking (out pack, out size);
942 type.GetRuntimeModule().MetadataImport.GetClassLayout(type.MetadataToken, out pack, out size);
945 // Metadata parameter checking should not have allowed 0 for packing size.
946 // The runtime later converts a packing size of 0 to 8 so do the same here
947 // because it's more useful from a user perspective.
949 pack = DEFAULT_PACKING_SIZE;
951 return new StructLayoutAttribute(layoutKind, pack, size, charSet);
954 internal static bool IsDefined(RuntimeType type)
956 if (type.IsInterface || type.HasElementType || type.IsGenericParameter)
962 internal LayoutKind _val;
964 internal StructLayoutAttribute(LayoutKind layoutKind, int pack, int size, CharSet charSet)
972 public StructLayoutAttribute(LayoutKind layoutKind)
976 public StructLayoutAttribute(short layoutKind)
978 _val = (LayoutKind)layoutKind;
980 public LayoutKind Value { get { return _val; } }
983 public CharSet CharSet;
986 [AttributeUsage(AttributeTargets.Field, Inherited = false)]
987 [System.Runtime.InteropServices.ComVisible(true)]
988 public unsafe sealed class FieldOffsetAttribute : Attribute
990 [System.Security.SecurityCritical] // auto-generated
991 internal static Attribute GetCustomAttribute(RuntimeFieldInfo field)
995 if (field.DeclaringType != null &&
997 (fieldOffset = field.GetFieldOffset ()) >= 0)
999 field.GetRuntimeModule().MetadataImport.GetFieldOffset(field.DeclaringType.MetadataToken, field.MetadataToken, out fieldOffset))
1001 return new FieldOffsetAttribute(fieldOffset);
1006 [System.Security.SecurityCritical] // auto-generated
1007 internal static bool IsDefined(RuntimeFieldInfo field)
1009 return GetCustomAttribute(field) != null;
1013 public FieldOffsetAttribute(int offset)
1017 public int Value { get { return _val; } }
1020 #if FEATURE_COMINTEROP
1021 [AttributeUsage(AttributeTargets.Parameter | AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.ReturnValue, Inherited = false)]
1022 [System.Runtime.InteropServices.ComVisible(true)]
1023 public sealed class ComAliasNameAttribute : Attribute
1025 internal String _val;
1026 public ComAliasNameAttribute(String alias)
1030 public String Value { get {return _val;} }
1033 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Class | AttributeTargets.Interface, Inherited = false)]
1034 [System.Runtime.InteropServices.ComVisible(true)]
1035 public sealed class AutomationProxyAttribute : Attribute
1038 public AutomationProxyAttribute(bool val)
1042 public bool Value { get {return _val;} }
1045 [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
1046 [System.Runtime.InteropServices.ComVisible(true)]
1047 public sealed class PrimaryInteropAssemblyAttribute : Attribute
1049 internal int _major;
1050 internal int _minor;
1052 public PrimaryInteropAssemblyAttribute(int major, int minor)
1058 public int MajorVersion { get {return _major;} }
1059 public int MinorVersion { get {return _minor;} }
1061 #endif //FEATURE_COMINTEROP
1063 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1064 [System.Runtime.InteropServices.ComVisible(true)]
1065 public sealed class CoClassAttribute : Attribute
1067 internal Type _CoClass;
1069 public CoClassAttribute(Type coClass)
1074 public Type CoClass { get { return _CoClass; } }
1077 #if FEATURE_COMINTEROP || MONO
1079 [AttributeUsage(AttributeTargets.Interface, Inherited = false)]
1080 [System.Runtime.InteropServices.ComVisible(true)]
1081 public sealed class ComEventInterfaceAttribute : Attribute
1083 internal Type _SourceInterface;
1084 internal Type _EventProvider;
1086 public ComEventInterfaceAttribute(Type SourceInterface, Type EventProvider)
1088 _SourceInterface = SourceInterface;
1089 _EventProvider = EventProvider;
1092 public Type SourceInterface { get {return _SourceInterface;} }
1093 public Type EventProvider { get {return _EventProvider;} }
1096 #if FEATURE_COMINTEROP
1097 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1098 [System.Runtime.InteropServices.ComVisible(true)]
1099 public sealed class TypeLibVersionAttribute : Attribute
1101 internal int _major;
1102 internal int _minor;
1104 public TypeLibVersionAttribute(int major, int minor)
1110 public int MajorVersion { get {return _major;} }
1111 public int MinorVersion { get {return _minor;} }
1114 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1115 [System.Runtime.InteropServices.ComVisible(true)]
1116 public sealed class ComCompatibleVersionAttribute : Attribute
1118 internal int _major;
1119 internal int _minor;
1120 internal int _build;
1121 internal int _revision;
1123 public ComCompatibleVersionAttribute(int major, int minor, int build, int revision)
1128 _revision = revision;
1131 public int MajorVersion { get {return _major;} }
1132 public int MinorVersion { get {return _minor;} }
1133 public int BuildNumber { get {return _build;} }
1134 public int RevisionNumber { get {return _revision;} }
1137 #endif //FEATURE_COMINTEROP
1139 [AttributeUsage(AttributeTargets.Assembly | AttributeTargets.Interface | AttributeTargets.Class | AttributeTargets.Struct, Inherited = false)]
1140 [System.Runtime.InteropServices.ComVisible(true)]
1141 public sealed class BestFitMappingAttribute : Attribute
1143 internal bool _bestFitMapping;
1145 public BestFitMappingAttribute(bool BestFitMapping)
1147 _bestFitMapping = BestFitMapping;
1150 public bool BestFitMapping { get { return _bestFitMapping; } }
1151 public bool ThrowOnUnmappableChar;
1154 [AttributeUsage(AttributeTargets.Module, Inherited = false)]
1155 [System.Runtime.InteropServices.ComVisible(true)]
1156 public sealed class DefaultCharSetAttribute : Attribute
1158 internal CharSet _CharSet;
1160 public DefaultCharSetAttribute(CharSet charSet)
1165 public CharSet CharSet { get { return _CharSet; } }
1168 [Obsolete("This attribute has been deprecated. Application Domains no longer respect Activation Context boundaries in IDispatch calls.", false)]
1169 [AttributeUsage(AttributeTargets.Assembly, Inherited = false)]
1170 [System.Runtime.InteropServices.ComVisible(true)]
1171 public sealed class SetWin32ContextInIDispatchAttribute : Attribute
1173 public SetWin32ContextInIDispatchAttribute()
1178 #if FEATURE_COMINTEROP
1179 [AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
1180 [System.Runtime.InteropServices.ComVisible(false)]
1181 public sealed class ManagedToNativeComInteropStubAttribute : Attribute
1183 internal Type _classType;
1184 internal String _methodName;
1186 public ManagedToNativeComInteropStubAttribute(Type classType, String methodName)
1188 _classType = classType;
1189 _methodName = methodName;
1192 public Type ClassType { get { return _classType; } }
1193 public String MethodName { get { return _methodName; } }
1195 #endif // FEATURE_COMINTEROP