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
528 [AttributeUsage(AttributeTargets
.Parameter
| AttributeTargets
.Field
| AttributeTargets
.ReturnValue
, Inherited
= false)]
529 [System
.Runtime
.InteropServices
.ComVisible(true)]
530 public unsafe sealed class MarshalAsAttribute
: Attribute
533 internal MarshalAsAttribute
Copy ()
535 return (MarshalAsAttribute
)this.MemberwiseClone ();
538 [System
.Security
.SecurityCritical
] // auto-generated
539 internal static Attribute
GetCustomAttribute(RuntimeParameterInfo parameter
)
541 return GetCustomAttribute(parameter
.MetadataToken
, parameter
.GetRuntimeModule());
544 [System
.Security
.SecurityCritical
] // auto-generated
545 internal static bool IsDefined(RuntimeParameterInfo parameter
)
547 return GetCustomAttribute(parameter
) != null;
550 [System
.Security
.SecurityCritical
] // auto-generated
551 internal static Attribute
GetCustomAttribute(RuntimeFieldInfo field
)
553 return GetCustomAttribute(field
.MetadataToken
, field
.GetRuntimeModule()); ;
556 [System
.Security
.SecurityCritical
] // auto-generated
557 internal static bool IsDefined(RuntimeFieldInfo field
)
559 return GetCustomAttribute(field
) != null;
562 [System
.Security
.SecurityCritical
] // auto-generated
563 internal static Attribute
GetCustomAttribute(int token
, RuntimeModule scope
)
565 UnmanagedType unmanagedType
, arraySubType
;
566 VarEnum safeArraySubType
;
567 int sizeParamIndex
= 0, sizeConst
= 0;
568 string marshalTypeName
= null, marshalCookie
= null, safeArrayUserDefinedTypeName
= null;
569 int iidParamIndex
= 0;
570 ConstArray nativeType
= ModuleHandle
.GetMetadataImport(scope
.GetNativeHandle()).GetFieldMarshal(token
);
572 if (nativeType
.Length
== 0)
575 MetadataImport
.GetMarshalAs(nativeType
,
576 out unmanagedType
, out safeArraySubType
, out safeArrayUserDefinedTypeName
, out arraySubType
, out sizeParamIndex
,
577 out sizeConst
, out marshalTypeName
, out marshalCookie
, out iidParamIndex
);
579 RuntimeType safeArrayUserDefinedType
= safeArrayUserDefinedTypeName
== null || safeArrayUserDefinedTypeName
.Length
== 0 ? null :
580 RuntimeTypeHandle
.GetTypeByNameUsingCARules(safeArrayUserDefinedTypeName
, scope
);
581 RuntimeType marshalTypeRef
= null;
585 marshalTypeRef
= marshalTypeName
== null ? null : RuntimeTypeHandle
.GetTypeByNameUsingCARules(marshalTypeName
, scope
);
587 catch (System
.TypeLoadException
)
589 // The user may have supplied a bad type name string causing this TypeLoadException
590 // Regardless, we return the bad type name
591 Contract
.Assert(marshalTypeName
!= null);
594 return new MarshalAsAttribute(
595 unmanagedType
, safeArraySubType
, safeArrayUserDefinedType
, arraySubType
,
596 (short)sizeParamIndex
, sizeConst
, marshalTypeName
, marshalTypeRef
, marshalCookie
, iidParamIndex
);
599 internal MarshalAsAttribute(UnmanagedType val
, VarEnum safeArraySubType
, RuntimeType safeArrayUserDefinedSubType
, UnmanagedType arraySubType
,
600 short sizeParamIndex
, int sizeConst
, string marshalType
, RuntimeType marshalTypeRef
, string marshalCookie
, int iidParamIndex
)
603 SafeArraySubType
= safeArraySubType
;
604 SafeArrayUserDefinedSubType
= safeArrayUserDefinedSubType
;
605 IidParameterIndex
= iidParamIndex
;
606 ArraySubType
= arraySubType
;
607 SizeParamIndex
= sizeParamIndex
;
608 SizeConst
= sizeConst
;
609 MarshalType
= marshalType
;
610 MarshalTypeRef
= marshalTypeRef
;
611 MarshalCookie
= marshalCookie
;
614 internal UnmanagedType _val
;
615 public MarshalAsAttribute(UnmanagedType unmanagedType
)
617 _val
= unmanagedType
;
619 public MarshalAsAttribute(short unmanagedType
)
621 _val
= (UnmanagedType
)unmanagedType
;
623 public UnmanagedType Value { get { return _val; }
}
625 // Fields used with SubType = SafeArray.
626 public VarEnum SafeArraySubType
;
627 public Type SafeArrayUserDefinedSubType
;
629 // Field used with iid_is attribute (interface pointers).
630 public int IidParameterIndex
;
632 // Fields used with SubType = ByValArray and LPArray.
633 // Array size = parameter(PI) * PM + C
634 public UnmanagedType ArraySubType
;
635 public short SizeParamIndex
; // param index PI
636 public int SizeConst
; // constant C
638 // Fields used with SubType = CustomMarshaler
639 [System
.Runtime
.InteropServices
.ComVisible(true)]
640 public String MarshalType
; // Name of marshaler class
641 [System
.Runtime
.InteropServices
.ComVisible(true)]
642 public Type MarshalTypeRef
; // Type of marshaler class
643 public String MarshalCookie
; // cookie to pass to marshaler
646 [AttributeUsage(AttributeTargets
.Class
| AttributeTargets
.Interface
, Inherited
= false)]
647 [System
.Runtime
.InteropServices
.ComVisible(true)]
648 public sealed class ComImportAttribute
: Attribute
650 internal static Attribute
GetCustomAttribute(RuntimeType type
)
652 if ((type
.Attributes
& TypeAttributes
.Import
) == 0)
655 return new ComImportAttribute();
658 internal static bool IsDefined(RuntimeType type
)
660 return (type
.Attributes
& TypeAttributes
.Import
) != 0;
663 public ComImportAttribute()
668 [AttributeUsage(AttributeTargets
.Assembly
| AttributeTargets
.Interface
| AttributeTargets
.Class
| AttributeTargets
.Enum
| AttributeTargets
.Struct
| AttributeTargets
.Delegate
, Inherited
= false)]
669 [System
.Runtime
.InteropServices
.ComVisible(true)]
670 public sealed class GuidAttribute
: Attribute
672 internal String _val
;
673 public GuidAttribute(String guid
)
677 public String Value { get { return _val; }
}
680 [AttributeUsage(AttributeTargets
.Method
, Inherited
= false)]
681 [System
.Runtime
.InteropServices
.ComVisible(true)]
682 public sealed class PreserveSigAttribute
: Attribute
684 internal static Attribute
GetCustomAttribute(RuntimeMethodInfo method
)
686 if ((method
.GetMethodImplementationFlags() & MethodImplAttributes
.PreserveSig
) == 0)
689 return new PreserveSigAttribute();
692 internal static bool IsDefined(RuntimeMethodInfo method
)
694 return (method
.GetMethodImplementationFlags() & MethodImplAttributes
.PreserveSig
) != 0;
697 public PreserveSigAttribute()
702 [AttributeUsage(AttributeTargets
.Parameter
, Inherited
= false)]
703 [System
.Runtime
.InteropServices
.ComVisible(true)]
704 public sealed class InAttribute
: Attribute
706 internal static Attribute
GetCustomAttribute(RuntimeParameterInfo parameter
)
708 return parameter
.IsIn
? new InAttribute() : null;
710 internal static bool IsDefined(RuntimeParameterInfo parameter
)
712 return parameter
.IsIn
;
720 [AttributeUsage(AttributeTargets
.Parameter
, Inherited
= false)]
721 [System
.Runtime
.InteropServices
.ComVisible(true)]
722 public sealed class OutAttribute
: Attribute
724 internal static Attribute
GetCustomAttribute(RuntimeParameterInfo parameter
)
726 return parameter
.IsOut
? new OutAttribute() : null;
728 internal static bool IsDefined(RuntimeParameterInfo parameter
)
730 return parameter
.IsOut
;
733 public OutAttribute()
738 [AttributeUsage(AttributeTargets
.Parameter
, Inherited
= false)]
739 [System
.Runtime
.InteropServices
.ComVisible(true)]
740 public sealed class OptionalAttribute
: Attribute
742 internal static Attribute
GetCustomAttribute(RuntimeParameterInfo parameter
)
744 return parameter
.IsOptional
? new OptionalAttribute() : null;
746 internal static bool IsDefined(RuntimeParameterInfo parameter
)
748 return parameter
.IsOptional
;
751 public OptionalAttribute()
757 public enum DllImportSearchPath
759 UseDllDirectoryForDependencies
= 0x100,
760 ApplicationDirectory
= 0x200,
761 UserDirectories
= 0x400,
763 SafeDirectories
= 0x1000,
764 AssemblyDirectory
= 0x2,
768 [AttributeUsage(AttributeTargets
.Assembly
| AttributeTargets
.Method
, AllowMultiple
= false)]
769 [System
.Runtime
.InteropServices
.ComVisible(false)]
770 public sealed class DefaultDllImportSearchPathsAttribute
: Attribute
772 internal DllImportSearchPath _paths
;
773 public DefaultDllImportSearchPathsAttribute(DllImportSearchPath paths
)
778 public DllImportSearchPath Paths { get { return _paths; }
}
781 [AttributeUsage(AttributeTargets
.Method
, Inherited
= false)]
782 [System
.Runtime
.InteropServices
.ComVisible(true)]
783 public unsafe sealed class DllImportAttribute
: Attribute
785 [System
.Security
.SecurityCritical
] // auto-generated
786 internal static Attribute
GetCustomAttribute(RuntimeMethodInfo method
)
788 if ((method
.Attributes
& MethodAttributes
.PinvokeImpl
) == 0)
792 MetadataImport scope
= ModuleHandle
.GetMetadataImport(method
.Module
.ModuleHandle
.GetRuntimeModule());
794 string entryPoint
, dllName
= null;
795 int token
= method
.MetadataToken
;
796 PInvokeAttributes flags
= 0;
799 ((MonoMethod
)method
).GetPInvoke(out flags
, out entryPoint
, out dllName
);
801 scope
.GetPInvokeMap(token
, out flags
, out entryPoint
, out dllName
);
804 CharSet charSet
= CharSet
.None
;
806 switch (flags
& PInvokeAttributes
.CharSetMask
)
808 case PInvokeAttributes
.CharSetNotSpec
: charSet
= CharSet
.None
; break;
809 case PInvokeAttributes
.CharSetAnsi
: charSet
= CharSet
.Ansi
; break;
810 case PInvokeAttributes
.CharSetUnicode
: charSet
= CharSet
.Unicode
; break;
811 case PInvokeAttributes
.CharSetAuto
: charSet
= CharSet
.Auto
; break;
813 // Invalid: default to CharSet.None
817 CallingConvention callingConvention
= CallingConvention
.Cdecl
;
819 switch (flags
& PInvokeAttributes
.CallConvMask
)
821 case PInvokeAttributes
.CallConvWinapi
: callingConvention
= CallingConvention
.Winapi
; break;
822 case PInvokeAttributes
.CallConvCdecl
: callingConvention
= CallingConvention
.Cdecl
; break;
823 case PInvokeAttributes
.CallConvStdcall
: callingConvention
= CallingConvention
.StdCall
; break;
824 case PInvokeAttributes
.CallConvThiscall
: callingConvention
= CallingConvention
.ThisCall
; break;
825 case PInvokeAttributes
.CallConvFastcall
: callingConvention
= CallingConvention
.FastCall
; break;
827 // Invalid: default to CallingConvention.Cdecl
831 bool exactSpelling
= (flags
& PInvokeAttributes
.NoMangle
) != 0;
832 bool setLastError
= (flags
& PInvokeAttributes
.SupportsLastError
) != 0;
833 bool bestFitMapping
= (flags
& PInvokeAttributes
.BestFitMask
) == PInvokeAttributes
.BestFitEnabled
;
834 bool throwOnUnmappableChar
= (flags
& PInvokeAttributes
.ThrowOnUnmappableCharMask
) == PInvokeAttributes
.ThrowOnUnmappableCharEnabled
;
835 bool preserveSig
= (method
.GetMethodImplementationFlags() & MethodImplAttributes
.PreserveSig
) != 0;
837 return new DllImportAttribute(
838 dllName
, entryPoint
, charSet
, exactSpelling
, setLastError
, preserveSig
,
839 callingConvention
, bestFitMapping
, throwOnUnmappableChar
);
842 internal static bool IsDefined(RuntimeMethodInfo method
)
844 return (method
.Attributes
& MethodAttributes
.PinvokeImpl
) != 0;
848 internal DllImportAttribute(
849 string dllName
, string entryPoint
, CharSet charSet
, bool exactSpelling
, bool setLastError
, bool preserveSig
,
850 CallingConvention callingConvention
, bool bestFitMapping
, bool throwOnUnmappableChar
)
853 EntryPoint
= entryPoint
;
855 ExactSpelling
= exactSpelling
;
856 SetLastError
= setLastError
;
857 PreserveSig
= preserveSig
;
858 CallingConvention
= callingConvention
;
859 BestFitMapping
= bestFitMapping
;
860 ThrowOnUnmappableChar
= throwOnUnmappableChar
;
863 internal String _val
;
865 public DllImportAttribute(String dllName
)
869 public String Value { get { return _val; }
}
871 public String EntryPoint
;
872 public CharSet CharSet
;
873 public bool SetLastError
;
874 public bool ExactSpelling
;
875 public bool PreserveSig
;
876 public CallingConvention CallingConvention
;
877 public bool BestFitMapping
;
878 public bool ThrowOnUnmappableChar
;
882 [AttributeUsage(AttributeTargets
.Class
| AttributeTargets
.Struct
, Inherited
= false)]
883 [System
.Runtime
.InteropServices
.ComVisible(true)]
884 public unsafe sealed class StructLayoutAttribute
: Attribute
886 private const int DEFAULT_PACKING_SIZE
= 8;
888 [System
.Security
.SecurityCritical
] // auto-generated
889 internal static StructLayoutAttribute
GetCustomAttribute(RuntimeType type
)
891 if (!IsDefined(type
))
894 int pack
= 0, size
= 0;
895 LayoutKind layoutKind
= LayoutKind
.Auto
;
896 switch (type
.Attributes
& TypeAttributes
.LayoutMask
)
898 case TypeAttributes
.ExplicitLayout
: layoutKind
= LayoutKind
.Explicit
; break;
899 case TypeAttributes
.AutoLayout
: layoutKind
= LayoutKind
.Auto
; break;
900 case TypeAttributes
.SequentialLayout
: layoutKind
= LayoutKind
.Sequential
; break;
901 default: Contract
.Assume(false); break;
904 CharSet charSet
= CharSet
.None
;
905 switch (type
.Attributes
& TypeAttributes
.StringFormatMask
)
907 case TypeAttributes
.AnsiClass
: charSet
= CharSet
.Ansi
; break;
908 case TypeAttributes
.AutoClass
: charSet
= CharSet
.Auto
; break;
909 case TypeAttributes
.UnicodeClass
: charSet
= CharSet
.Unicode
; break;
910 default: Contract
.Assume(false); break;
914 type
.GetPacking (out pack
, out size
);
916 type
.GetRuntimeModule().MetadataImport
.GetClassLayout(type
.MetadataToken
, out pack
, out size
);
919 // Metadata parameter checking should not have allowed 0 for packing size.
920 // The runtime later converts a packing size of 0 to 8 so do the same here
921 // because it's more useful from a user perspective.
923 pack
= DEFAULT_PACKING_SIZE
;
925 return new StructLayoutAttribute(layoutKind
, pack
, size
, charSet
);
928 internal static bool IsDefined(RuntimeType type
)
930 if (type
.IsInterface
|| type
.HasElementType
|| type
.IsGenericParameter
)
936 internal LayoutKind _val
;
938 internal StructLayoutAttribute(LayoutKind layoutKind
, int pack
, int size
, CharSet charSet
)
946 public StructLayoutAttribute(LayoutKind layoutKind
)
950 public StructLayoutAttribute(short layoutKind
)
952 _val
= (LayoutKind
)layoutKind
;
954 public LayoutKind Value { get { return _val; }
}
957 public CharSet CharSet
;
960 [AttributeUsage(AttributeTargets
.Field
, Inherited
= false)]
961 [System
.Runtime
.InteropServices
.ComVisible(true)]
962 public unsafe sealed class FieldOffsetAttribute
: Attribute
964 [System
.Security
.SecurityCritical
] // auto-generated
965 internal static Attribute
GetCustomAttribute(RuntimeFieldInfo field
)
969 if (field
.DeclaringType
!= null &&
971 (fieldOffset
= field
.GetFieldOffset ()) >= 0)
973 field
.GetRuntimeModule().MetadataImport
.GetFieldOffset(field
.DeclaringType
.MetadataToken
, field
.MetadataToken
, out fieldOffset
))
975 return new FieldOffsetAttribute(fieldOffset
);
980 [System
.Security
.SecurityCritical
] // auto-generated
981 internal static bool IsDefined(RuntimeFieldInfo field
)
983 return GetCustomAttribute(field
) != null;
987 public FieldOffsetAttribute(int offset
)
991 public int Value { get { return _val; }
}
994 #if FEATURE_COMINTEROP || MOBILE_LEGACY
995 [AttributeUsage(AttributeTargets
.Parameter
| AttributeTargets
.Field
| AttributeTargets
.Property
| AttributeTargets
.ReturnValue
, Inherited
= false)]
996 [System
.Runtime
.InteropServices
.ComVisible(true)]
997 public sealed class ComAliasNameAttribute
: Attribute
999 internal String _val
;
1000 public ComAliasNameAttribute(String
alias)
1004 public String Value { get {return _val;}
}
1007 [AttributeUsage(AttributeTargets
.Assembly
| AttributeTargets
.Class
| AttributeTargets
.Interface
, Inherited
= false)]
1008 [System
.Runtime
.InteropServices
.ComVisible(true)]
1009 public sealed class AutomationProxyAttribute
: Attribute
1012 public AutomationProxyAttribute(bool val
)
1016 public bool Value { get {return _val;}
}
1019 [AttributeUsage(AttributeTargets
.Assembly
, Inherited
= false, AllowMultiple
= true)]
1020 [System
.Runtime
.InteropServices
.ComVisible(true)]
1021 public sealed class PrimaryInteropAssemblyAttribute
: Attribute
1023 internal int _major
;
1024 internal int _minor
;
1026 public PrimaryInteropAssemblyAttribute(int major
, int minor
)
1032 public int MajorVersion { get {return _major;}
}
1033 public int MinorVersion { get {return _minor;}
}
1036 [AttributeUsage(AttributeTargets
.Interface
, Inherited
= false)]
1037 [System
.Runtime
.InteropServices
.ComVisible(true)]
1038 public sealed class CoClassAttribute
: Attribute
1040 internal Type _CoClass
;
1042 public CoClassAttribute(Type coClass
)
1047 public Type CoClass { get { return _CoClass; }
}
1050 [AttributeUsage(AttributeTargets
.Interface
, Inherited
= false)]
1051 [System
.Runtime
.InteropServices
.ComVisible(true)]
1052 public sealed class ComEventInterfaceAttribute
: Attribute
1054 internal Type _SourceInterface
;
1055 internal Type _EventProvider
;
1057 public ComEventInterfaceAttribute(Type SourceInterface
, Type EventProvider
)
1059 _SourceInterface
= SourceInterface
;
1060 _EventProvider
= EventProvider
;
1063 public Type SourceInterface { get {return _SourceInterface;}
}
1064 public Type EventProvider { get {return _EventProvider;}
}
1067 #if FEATURE_COMINTEROP || MOBILE_LEGACY
1068 [AttributeUsage(AttributeTargets
.Assembly
, Inherited
= false)]
1069 [System
.Runtime
.InteropServices
.ComVisible(true)]
1070 public sealed class TypeLibVersionAttribute
: Attribute
1072 internal int _major
;
1073 internal int _minor
;
1075 public TypeLibVersionAttribute(int major
, int minor
)
1081 public int MajorVersion { get {return _major;}
}
1082 public int MinorVersion { get {return _minor;}
}
1085 [AttributeUsage(AttributeTargets
.Assembly
, Inherited
= false)]
1086 [System
.Runtime
.InteropServices
.ComVisible(true)]
1087 public sealed class ComCompatibleVersionAttribute
: Attribute
1089 internal int _major
;
1090 internal int _minor
;
1091 internal int _build
;
1092 internal int _revision
;
1094 public ComCompatibleVersionAttribute(int major
, int minor
, int build
, int revision
)
1099 _revision
= revision
;
1102 public int MajorVersion { get {return _major;}
}
1103 public int MinorVersion { get {return _minor;}
}
1104 public int BuildNumber { get {return _build;}
}
1105 public int RevisionNumber { get {return _revision;}
}
1108 [AttributeUsage(AttributeTargets
.Assembly
| AttributeTargets
.Interface
| AttributeTargets
.Class
| AttributeTargets
.Struct
, Inherited
= false)]
1109 [System
.Runtime
.InteropServices
.ComVisible(true)]
1110 public sealed class BestFitMappingAttribute
: Attribute
1112 internal bool _bestFitMapping
;
1114 public BestFitMappingAttribute(bool BestFitMapping
)
1116 _bestFitMapping
= BestFitMapping
;
1119 public bool BestFitMapping { get { return _bestFitMapping; }
}
1120 public bool ThrowOnUnmappableChar
;
1123 [AttributeUsage(AttributeTargets
.Module
, Inherited
= false)]
1124 [System
.Runtime
.InteropServices
.ComVisible(true)]
1125 public sealed class DefaultCharSetAttribute
: Attribute
1127 internal CharSet _CharSet
;
1129 public DefaultCharSetAttribute(CharSet charSet
)
1134 public CharSet CharSet { get { return _CharSet; }
}
1137 [Obsolete("This attribute has been deprecated. Application Domains no longer respect Activation Context boundaries in IDispatch calls.", false)]
1138 [AttributeUsage(AttributeTargets
.Assembly
, Inherited
= false)]
1139 [System
.Runtime
.InteropServices
.ComVisible(true)]
1140 public sealed class SetWin32ContextInIDispatchAttribute
: Attribute
1142 public SetWin32ContextInIDispatchAttribute()
1147 [AttributeUsage(AttributeTargets
.Method
, Inherited
= false, AllowMultiple
= false)]
1148 [System
.Runtime
.InteropServices
.ComVisible(false)]
1149 public sealed class ManagedToNativeComInteropStubAttribute
: Attribute
1151 internal Type _classType
;
1152 internal String _methodName
;
1154 public ManagedToNativeComInteropStubAttribute(Type classType
, String methodName
)
1156 _classType
= classType
;
1157 _methodName
= methodName
;
1160 public Type ClassType { get { return _classType; }
}
1161 public String MethodName { get { return _methodName; }
}