-
Notifications
You must be signed in to change notification settings - Fork 4k
/
PlatformInvokeInformation.cs
246 lines (208 loc) · 8.18 KB
/
PlatformInvokeInformation.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Reflection;
using System.Runtime.InteropServices;
using Roslyn.Utilities;
namespace Microsoft.CodeAnalysis
{
/// <summary>
/// Information that describes how a method from the underlying Platform is to be invoked.
/// </summary>
public sealed class DllImportData : Cci.IPlatformInvokeInformation
{
private readonly string _moduleName;
private readonly string _entryPointName; // null if unspecified, the name of the target method should be used
private readonly MethodImportAttributes _flags;
internal DllImportData(string moduleName, string entryPointName, MethodImportAttributes flags)
{
_moduleName = moduleName;
_entryPointName = entryPointName;
_flags = flags;
}
/// <summary>
/// Module name. Null if value specified in the attribute is not valid.
/// </summary>
public string ModuleName
{
get { return _moduleName; }
}
/// <summary>
/// Name of the native entry point or null if not specified (the effective name is the same as the name of the target method).
/// </summary>
public string EntryPointName
{
get { return _entryPointName; }
}
MethodImportAttributes Cci.IPlatformInvokeInformation.Flags
{
get { return _flags; }
}
/// <summary>
/// Controls whether the <see cref="CharacterSet"/> field causes the common language runtime
/// to search an unmanaged DLL for entry-point names other than the one specified.
/// </summary>
public bool ExactSpelling
{
get
{
return (_flags & MethodImportAttributes.ExactSpelling) != 0;
}
}
/// <summary>
/// Indicates how to marshal string parameters and controls name mangling.
/// </summary>
public CharSet CharacterSet
{
get
{
switch (_flags & MethodImportAttributes.CharSetMask)
{
case MethodImportAttributes.CharSetAnsi:
return CharSet.Ansi;
case MethodImportAttributes.CharSetUnicode:
return CharSet.Unicode;
case MethodImportAttributes.CharSetAuto:
return Cci.Constants.CharSet_Auto;
case 0:
return Cci.Constants.CharSet_None;
}
throw ExceptionUtilities.UnexpectedValue(_flags);
}
}
/// <summary>
/// Indicates whether the callee calls the SetLastError Win32 API function before returning from the attributed method.
/// </summary>
public bool SetLastError
{
get
{
return (_flags & MethodImportAttributes.SetLastError) != 0;
}
}
/// <summary>
/// Indicates the calling convention of an entry point.
/// </summary>
public CallingConvention CallingConvention
{
get
{
switch (_flags & MethodImportAttributes.CallingConventionMask)
{
default:
return CallingConvention.Winapi;
case MethodImportAttributes.CallingConventionCDecl:
return CallingConvention.Cdecl;
case MethodImportAttributes.CallingConventionStdCall:
return CallingConvention.StdCall;
case MethodImportAttributes.CallingConventionThisCall:
return CallingConvention.ThisCall;
case MethodImportAttributes.CallingConventionFastCall:
return Cci.Constants.CallingConvention_FastCall;
}
}
}
/// <summary>
/// Enables or disables best-fit mapping behavior when converting Unicode characters to ANSI characters.
/// Null if not specified (the setting for the containing type or assembly should be used, <see cref="BestFitMappingAttribute"/>).
/// </summary>
public bool? BestFitMapping
{
get
{
switch (_flags & MethodImportAttributes.BestFitMappingMask)
{
case MethodImportAttributes.BestFitMappingEnable:
return true;
case MethodImportAttributes.BestFitMappingDisable:
return false;
default:
return null;
}
}
}
/// <summary>
/// Enables or disables the throwing of an exception on an unmappable Unicode character that is converted to an ANSI "?" character.
/// Null if not specified.
/// </summary>
public bool? ThrowOnUnmappableCharacter
{
get
{
switch (_flags & MethodImportAttributes.ThrowOnUnmappableCharMask)
{
case MethodImportAttributes.ThrowOnUnmappableCharEnable:
return true;
case MethodImportAttributes.ThrowOnUnmappableCharDisable:
return false;
default:
return null;
}
}
}
internal static MethodImportAttributes MakeFlags(bool exactSpelling, CharSet charSet, bool setLastError, CallingConvention callingConvention, bool? useBestFit, bool? throwOnUnmappable)
{
MethodImportAttributes result = 0;
if (exactSpelling)
{
result |= MethodImportAttributes.ExactSpelling;
}
switch (charSet)
{
case CharSet.Ansi:
result |= MethodImportAttributes.CharSetAnsi;
break;
case CharSet.Unicode:
result |= MethodImportAttributes.CharSetUnicode;
break;
case Cci.Constants.CharSet_Auto:
result |= MethodImportAttributes.CharSetAuto;
break;
// Dev10: use default without reporting an error
}
if (setLastError)
{
result |= MethodImportAttributes.SetLastError;
}
switch (callingConvention)
{
default: // Dev10: uses default without reporting an error
result |= MethodImportAttributes.CallingConventionWinApi;
break;
case CallingConvention.Cdecl:
result |= MethodImportAttributes.CallingConventionCDecl;
break;
case CallingConvention.StdCall:
result |= MethodImportAttributes.CallingConventionStdCall;
break;
case CallingConvention.ThisCall:
result |= MethodImportAttributes.CallingConventionThisCall;
break;
case Cci.Constants.CallingConvention_FastCall:
result |= MethodImportAttributes.CallingConventionFastCall;
break;
}
if (throwOnUnmappable.HasValue)
{
if (throwOnUnmappable.Value)
{
result |= MethodImportAttributes.ThrowOnUnmappableCharEnable;
}
else
{
result |= MethodImportAttributes.ThrowOnUnmappableCharDisable;
}
}
if (useBestFit.HasValue)
{
if (useBestFit.Value)
{
result |= MethodImportAttributes.BestFitMappingEnable;
}
else
{
result |= MethodImportAttributes.BestFitMappingDisable;
}
}
return result;
}
}
}