-
Notifications
You must be signed in to change notification settings - Fork 4.5k
/
BestFitMappingNative.cpp
476 lines (407 loc) · 12.9 KB
/
BestFitMappingNative.cpp
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include <xplatform.h>
#include <platformdefines.h>
#pragma warning( push )
#pragma warning( disable : 4996)
static int fails = 0; //record the fail numbers
// Overload methods for reportfailure
static int ReportFailure(const char* s)
{
printf(" === Fail:%s\n", s);
return (++fails);
}
extern "C" DLL_EXPORT int __cdecl GetResult()
{
return fails;
}
//This method is used on Windows Only
extern "C" DLL_EXPORT char __cdecl GetByteForWideChar()
{
#ifdef WINDOWS
char * p = new char[3];
WideCharToMultiByte(CP_ACP, 0, W("\x263c"), -1, p, 2, NULL, NULL);
p[1] = '\0';
char breturn = p[0];
delete p;
return breturn;
#else
return 0; //It wont be called MAC
#endif
}
//x86: Managed(Encoding: utf8)---->Marshaler(Encoding:ASCII)---->Native(Encoding:utf8)
//MAC(x64):Managed(Encoding:utf8)----->Marshaler(Encoding:utf8)---->Native(Encoding:utf8)
//Now both side(Managed Side and native side) takes the utf8 encoding when comparing string
bool CheckInput(LPSTR str)
{
//int WideCharToMultiByte(
// UINT CodePage,
// DWORD dwFlags,
// LPCWSTR lpWideCharStr,
// int cchWideChar,
// LPSTR lpMultiByteStr,
// int cbMultiByte,
// LPCSTR lpDefaultChar,
// LPBOOL lpUsedDefaultChar
//);
#ifdef WINDOWS
char * p = new char[3];
WideCharToMultiByte(CP_ACP, 0, W("\x263c"), -1, p, 2, NULL, NULL);
p[1] = '\0';
#else
char* p = new char[4]; //00bc98e2,the utf8 code of "\263c",we can get these char value through the following code with C#
p[0] = (char)0xe2; //Encoding enc = Encoding.Default;//UTF8 Encoding
p[1] = (char)0x98; //Byte[] by = enc.GetBytes("\x263c");
p[2] = (char)0xbc;
p[3] = (char)0;
#endif
if (0 != strncmp(str, p, 4))
{
printf("CheckInput:Expected:%s,Actual:%d\n", p, str[0]);
delete[]p;
return false;
}
delete[]p;
return true;
}
//C Call,In attribute,LPstr
extern "C" DLL_EXPORT LPSTR __cdecl CLPStr_In(LPSTR pStr)
{
//Check the Input
if (!CheckInput(pStr))
{
ReportFailure("CLPStr_In:Native Side");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(pStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strcpy(pBack, pStr);
return pBack;
}
extern "C" DLL_EXPORT LPSTR __cdecl CLPStr_Out(LPSTR pStr)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strncpy(pBack, pTemp, strlen(pTemp) + 1);
strncpy(pStr, pTemp, strlen(pTemp) + 1);
return pBack;
}
extern "C" DLL_EXPORT LPSTR __cdecl CLPStr_InOut(LPSTR pStr)
{
//Check the Input
if (!CheckInput(pStr))
{
ReportFailure("CLPStr_InOut:Native Side");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(pStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, pStr);
return pBack;
}
extern "C" DLL_EXPORT LPSTR __cdecl CLPStr_InByRef(LPSTR* ppStr)
{
//Check the Input
if (!CheckInput(*ppStr))
{
ReportFailure("CLPStr_InByRef:Native Side");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(*ppStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, *ppStr);
return pBack;
}
extern "C" DLL_EXPORT LPSTR __cdecl CLPStr_OutByRef(LPSTR* ppStr)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strncpy(pBack, pTemp, strlen(pTemp) + 1);
*ppStr = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strncpy(*ppStr, pTemp, strlen(pTemp) + 1);
return pBack;
}
extern "C" DLL_EXPORT LPSTR __cdecl CLPStr_InOutByRef(LPSTR* ppStr)
{
//Check the Input
if (!CheckInput(*ppStr))
{
ReportFailure("CLPStr_InOutByRef:Native Side");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(*ppStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, *ppStr);
return pBack;
}
typedef LPSTR (__cdecl* delegate_cdecl)(LPSTR* ppstr);
extern "C" DLL_EXPORT delegate_cdecl __cdecl CLPStr_DelegatePInvoke()
{
return CLPStr_InOutByRef;
}
//stdcall
extern "C" DLL_EXPORT LPSTR STDMETHODCALLTYPE SLPStr_In(LPSTR pStr)
{
//Check the Input
if (!CheckInput(pStr))
{
ReportFailure("SLPStr_In:NativeSide");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(pStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, pStr);
return pBack;
}
extern "C" DLL_EXPORT LPSTR STDMETHODCALLTYPE SLPStr_Out(LPSTR pStr)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strncpy(pBack, pTemp, strlen(pTemp) + 1);
strncpy(pStr, pTemp, strlen(pTemp) + 1);
return pBack;
}
extern "C" DLL_EXPORT LPSTR STDMETHODCALLTYPE SLPStr_InOut(LPSTR pStr)
{
//Check the Input
if (!CheckInput(pStr))
{
ReportFailure("SLPStr_InOut:NativeSide");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(pStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, pStr);
return pBack;
}
extern "C" DLL_EXPORT LPSTR STDMETHODCALLTYPE SLPStr_InByRef(LPSTR* ppStr)
{
//Check the Input
if (!CheckInput(*ppStr))
{
ReportFailure("SLPStr_InByRef:NativeSide");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(*ppStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, *ppStr);
return pBack;
}
extern "C" DLL_EXPORT LPSTR STDMETHODCALLTYPE SLPStr_OutByRef(LPSTR* ppStr)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strncpy(pBack, pTemp, strlen(pTemp) + 1);
*ppStr = (LPSTR)CoreClrAlloc(sizeof(char) * len);
strncpy(*ppStr, pTemp, strlen(pTemp) + 1);
return pBack;
}
extern "C" DLL_EXPORT LPSTR STDMETHODCALLTYPE SLPStr_InOutByRef(LPSTR* ppStr)
{
//Check the Input
if (!CheckInput(*ppStr))
{
ReportFailure("SLPStr_InOutByRef:NativeSide");
}
//alloc,copy, since we cannot depend the Marshaler's activity.
size_t len = strlen(*ppStr) + 1; //+1, Include the NULL Character.
LPSTR pBack = (LPSTR)CoreClrAlloc(len);
strcpy(pBack, *ppStr);
return pBack;
}
typedef LPSTR (STDMETHODCALLTYPE *delegate_stdcall)(LPSTR* ppstr);
extern "C" DLL_EXPORT delegate_stdcall SLPStr_DelegatePInvoke()
{
return SLPStr_InOutByRef;
}
///Cdecl, Reverse PInvoke
typedef LPSTR (__cdecl *CCallBackIn)(LPSTR pstr);
extern "C" DLL_EXPORT void __cdecl DoCCallBack_LPSTR_In(CCallBackIn callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp)+1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr,pTemp,len);
if(!CheckInput(callback(pStr)))
{
ReportFailure("DoCCallBack_LPSTR_In:NativeSide");
}
CoreClrFree(pStr);
}
typedef LPSTR (__cdecl *CCallBackOut)(LPSTR pstr);
extern "C" DLL_EXPORT void __cdecl DoCCallBack_LPSTR_Out(CCallBackOut callback)
{
size_t len = 10;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
//Check the return value
if (!CheckInput(callback(pStr)))
{
ReportFailure("DoCCallBack_LPSTR_Out:NativeSide,the first check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoCCallBack_LPSTR_Out:NativeSide,the Second Check");
}
CoreClrFree(pStr);
}
typedef LPSTR (__cdecl *CCallBackInOut)(LPSTR pstr);
extern "C" DLL_EXPORT void __cdecl DoCCallBack_LPSTR_InOut(CCallBackInOut callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(pStr)))
{
ReportFailure("DoCCallBack_LPSTR_InOut:NativeSide,the first check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoCCallBack_LPSTR_InOut:NativeSide,the Second Check");
}
CoreClrFree(pStr);
}
typedef LPSTR (__cdecl *CallBackInByRef)(LPSTR* pstr);
extern "C" DLL_EXPORT void __cdecl DoCCallBack_LPSTR_InByRef(CallBackInByRef callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(&pStr)))
{
ReportFailure("DoCCallBack_LPSTR_InByRef:NativeSide");
}
CoreClrFree(pStr);
}
typedef LPSTR (__cdecl *CCallBackOutByRef)(LPSTR* pstr);
extern "C" DLL_EXPORT void __cdecl DoCCallBack_LPSTR_OutByRef(CCallBackOutByRef callback)
{
size_t len = 10;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
if (!CheckInput(callback(&pStr)))
{
ReportFailure("DoCCallBack_LPSTR_OutByRef:NativeSide,the first Check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoCCallBack_LPSTR_OutByRef:NativeSide,the Second Check");
}
CoreClrFree(pStr);
}
typedef LPSTR (__cdecl *CCallBackInOutByRef)(LPSTR* pstr);
extern "C" DLL_EXPORT void __cdecl DoCCallBack_LPSTR_InOutByRef(CCallBackInOutByRef callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(&pStr)))
{
ReportFailure("DoCCallBack_LPSTR_InOutByRef:NativeSide");
}
if (!CheckInput(pStr))
{
ReportFailure("DoCCallBack_LPSTR_InOutByRef:NativeSide,the Second Check");
}
CoreClrFree(pStr);
}
///STDCALL Reverse PInvoke
typedef LPSTR (STDMETHODCALLTYPE *SCallBackIn)(LPSTR pstr);
extern "C" DLL_EXPORT void __cdecl DoSCallBack_LPSTR_In(SCallBackIn callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(pStr)))
{
ReportFailure("DoSCallBack_LPSTR_In:NativeSide");
}
CoreClrFree(pStr);
}
typedef LPSTR (STDMETHODCALLTYPE *SCallBackOut)(LPSTR pstr);
extern "C" DLL_EXPORT void __cdecl DoSCallBack_LPSTR_Out(SCallBackOut callback)
{
size_t len = 10;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
if (!CheckInput(callback(pStr)))
{
ReportFailure("DoSCallBack_LPSTR_Out:NativeSide,the first check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoSCallBack_LPSTR_Out:NativeSide,the Second Check");
}
CoreClrFree(pStr);
}
typedef LPSTR (STDMETHODCALLTYPE *SCallBackInOut)(LPSTR pstr);
extern "C" DLL_EXPORT void __cdecl DoSCallBack_LPSTR_InOut(SCallBackInOut callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(pStr)))
{
ReportFailure("DoSCallBack_LPSTR_InOut:NativeSide,the first check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoSCallBack_LPSTR_InOut:NativeSide,the second Check");
}
CoreClrFree(pStr);
}
typedef LPSTR (STDMETHODCALLTYPE *SCallBackInByRef)(LPSTR* pstr);
extern "C" DLL_EXPORT void __cdecl DoSCallBack_LPSTR_InByRef(SCallBackInByRef callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(&pStr)))
{
ReportFailure("DoSCallBack_LPSTR_InByRef:NativeSide");
}
CoreClrFree(pStr);
}
typedef LPSTR (STDMETHODCALLTYPE *SCallBackOutByRef)(LPSTR* pstr);
extern "C" DLL_EXPORT void __cdecl DoSCallBack_LPSTR_OutByRef(SCallBackOutByRef callback)
{
size_t len = 10;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
if (!CheckInput(callback(&pStr)))
{
ReportFailure("DoSCallBack_LPSTR_OutByRef:NativeSide,the first check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoSCallBack_LPSTR_OutByRef:NativeSide,the second Check");
}
CoreClrFree(pStr);
}
typedef LPSTR (STDMETHODCALLTYPE *SCallBackInOutByRef)(LPSTR* pstr);
extern "C" DLL_EXPORT void __cdecl DoSCallBack_LPSTR_InOutByRef(SCallBackInOutByRef callback)
{
const char* pTemp = "AAAA";
size_t len = strlen(pTemp) + 1;
LPSTR pStr = (LPSTR)CoreClrAlloc(len);
strncpy(pStr, pTemp, len);
if (!CheckInput(callback(&pStr)))
{
ReportFailure("DoSCallBack_LPSTR_InOutByRef:NativeSide,the first check");
}
if (!CheckInput(pStr))
{
ReportFailure("DoSCallBack_LPSTR_InOutByRef:NativeSide,the second Check");
}
CoreClrFree(pStr);
}
#pragma warning( pop )