mscorlib(4.0.0.0) API with additions
DllImportAttribute.cs
1 using System.Reflection;
2 using System.Security;
3 
5 {
7  [AttributeUsage(AttributeTargets.Method, Inherited = false)]
8  [ComVisible(true)]
9  [__DynamicallyInvokable]
10  public sealed class DllImportAttribute : Attribute
11  {
12  internal string _val;
13 
15  [__DynamicallyInvokable]
16  public string EntryPoint;
17 
19  [__DynamicallyInvokable]
20  public CharSet CharSet;
21 
23  [__DynamicallyInvokable]
24  public bool SetLastError;
25 
27  [__DynamicallyInvokable]
28  public bool ExactSpelling;
29 
31  [__DynamicallyInvokable]
32  public bool PreserveSig;
33 
35  [__DynamicallyInvokable]
37 
39  [__DynamicallyInvokable]
40  public bool BestFitMapping;
41 
43  [__DynamicallyInvokable]
44  public bool ThrowOnUnmappableChar;
45 
48  [__DynamicallyInvokable]
49  public string Value
50  {
51  [__DynamicallyInvokable]
52  get
53  {
54  return _val;
55  }
56  }
57 
58  [SecurityCritical]
59  internal static Attribute GetCustomAttribute(RuntimeMethodInfo method)
60  {
61  if ((method.Attributes & MethodAttributes.PinvokeImpl) == MethodAttributes.PrivateScope)
62  {
63  return null;
64  }
65  MetadataImport metadataImport = ModuleHandle.GetMetadataImport(method.Module.ModuleHandle.GetRuntimeModule());
66  string importDll = null;
67  int metadataToken = method.MetadataToken;
68  PInvokeAttributes attributes = PInvokeAttributes.CharSetNotSpec;
69  metadataImport.GetPInvokeMap(metadataToken, out attributes, out string importName, out importDll);
70  CharSet charSet = CharSet.None;
71  switch (attributes & PInvokeAttributes.CharSetMask)
72  {
73  case PInvokeAttributes.CharSetNotSpec:
74  charSet = CharSet.None;
75  break;
76  case PInvokeAttributes.CharSetAnsi:
77  charSet = CharSet.Ansi;
78  break;
79  case PInvokeAttributes.CharSetUnicode:
80  charSet = CharSet.Unicode;
81  break;
82  case PInvokeAttributes.CharSetMask:
83  charSet = CharSet.Auto;
84  break;
85  }
86  CallingConvention callingConvention = CallingConvention.Cdecl;
87  switch (attributes & PInvokeAttributes.CallConvMask)
88  {
89  case PInvokeAttributes.CallConvWinapi:
90  callingConvention = CallingConvention.Winapi;
91  break;
92  case PInvokeAttributes.CallConvCdecl:
93  callingConvention = CallingConvention.Cdecl;
94  break;
95  case PInvokeAttributes.CallConvStdcall:
96  callingConvention = CallingConvention.StdCall;
97  break;
98  case PInvokeAttributes.CallConvThiscall:
99  callingConvention = CallingConvention.ThisCall;
100  break;
101  case PInvokeAttributes.CallConvFastcall:
102  callingConvention = CallingConvention.FastCall;
103  break;
104  }
105  bool exactSpelling = (attributes & PInvokeAttributes.NoMangle) != PInvokeAttributes.CharSetNotSpec;
106  bool setLastError = (attributes & PInvokeAttributes.SupportsLastError) != PInvokeAttributes.CharSetNotSpec;
107  bool bestFitMapping = (attributes & PInvokeAttributes.BestFitMask) == PInvokeAttributes.BestFitEnabled;
108  bool throwOnUnmappableChar = (attributes & PInvokeAttributes.ThrowOnUnmappableCharMask) == PInvokeAttributes.ThrowOnUnmappableCharEnabled;
109  bool preserveSig = (method.GetMethodImplementationFlags() & MethodImplAttributes.PreserveSig) != MethodImplAttributes.IL;
110  return new DllImportAttribute(importDll, importName, charSet, exactSpelling, setLastError, preserveSig, callingConvention, bestFitMapping, throwOnUnmappableChar);
111  }
112 
113  internal static bool IsDefined(RuntimeMethodInfo method)
114  {
115  return (method.Attributes & MethodAttributes.PinvokeImpl) != MethodAttributes.PrivateScope;
116  }
117 
118  internal DllImportAttribute(string dllName, string entryPoint, CharSet charSet, bool exactSpelling, bool setLastError, bool preserveSig, CallingConvention callingConvention, bool bestFitMapping, bool throwOnUnmappableChar)
119  {
120  _val = dllName;
121  EntryPoint = entryPoint;
122  CharSet = charSet;
123  ExactSpelling = exactSpelling;
124  SetLastError = setLastError;
125  PreserveSig = preserveSig;
126  CallingConvention = callingConvention;
127  BestFitMapping = bestFitMapping;
128  ThrowOnUnmappableChar = throwOnUnmappableChar;
129  }
130 
133  [__DynamicallyInvokable]
134  public DllImportAttribute(string dllName)
135  {
136  _val = dllName;
137  }
138  }
139 }
MethodAttributes
Specifies flags for method attributes. These flags are defined in the corhdr.h file.
bool PreserveSig
Indicates whether unmanaged methods that have HRESULT or retval return values are directly translated...
bool BestFitMapping
Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.
bool ThrowOnUnmappableChar
Enables or disables the throwing of an exception on an unmappable Unicode character that is converted...
DllImportAttribute(string dllName)
Initializes a new instance of the T:System.Runtime.InteropServices.DllImportAttribute class with the ...
MethodImplAttributes
Specifies flags for the attributes of a method implementation.
CallingConvention CallingConvention
Indicates the calling convention of an entry point.
Represents the base class for custom attributes.
Definition: Attribute.cs:15
Definition: __Canon.cs:3
Represents a runtime handle for a module.
Definition: ModuleHandle.cs:12
bool SetLastError
Indicates whether the callee calls the SetLastError Win32 API function before returning from the attr...
CallingConvention
Specifies the calling convention required to call methods implemented in unmanaged code.
Indicates that the attributed method is exposed by an unmanaged dynamic-link library (DLL) as a stati...
CharSet
Dictates which character set marshaled strings should use.
Definition: CharSet.cs:7
string Value
Gets the name of the DLL file that contains the entry point.
AttributeTargets
Specifies the application elements on which it is valid to apply an attribute.
bool ExactSpelling
Controls whether the F:System.Runtime.InteropServices.DllImportAttribute.CharSet field causes the com...
CharSet CharSet
Indicates how to marshal string parameters to the method and controls name mangling.
string EntryPoint
Indicates the name or ordinal of the DLL entry point to be called.