Skip to content

Commit efcc949

Browse files
Use Cdecl calling convention for all [DllImport]
Add [UnmanagedFunctionPointer] to all binding function delegates with Cdecl calling convention
1 parent 7f61e75 commit efcc949

2 files changed

Lines changed: 80 additions & 13 deletions

File tree

Unity/Assets/NativeScript/Bindings.cs

Lines changed: 77 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -327,40 +327,46 @@ enum InitMode : byte
327327
// Handle to the C++ DLL
328328
static IntPtr libraryHandle;
329329

330+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
330331
delegate void InitDelegate(
331332
IntPtr memory,
332333
int memorySize,
333334
InitMode initMode);
334335

336+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
335337
public delegate void SetCsharpExceptionDelegate(int handle);
336338

337339
/*BEGIN CPP DELEGATES*/
340+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
338341
public delegate int NewBaseBallScriptDelegateType(int param0);
339342
public static NewBaseBallScriptDelegateType NewBaseBallScript;
340343

344+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
341345
public delegate void DestroyBaseBallScriptDelegateType(int param0);
342346
public static DestroyBaseBallScriptDelegateType DestroyBaseBallScript;
343347

348+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
344349
public delegate void MyGameAbstractBaseBallScriptUpdateDelegateType(int thisHandle);
345350
public static MyGameAbstractBaseBallScriptUpdateDelegateType MyGameAbstractBaseBallScriptUpdate;
346351

352+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
347353
public delegate void SetCsharpExceptionSystemNullReferenceExceptionDelegateType(int param0);
348354
public static SetCsharpExceptionSystemNullReferenceExceptionDelegateType SetCsharpExceptionSystemNullReferenceException;
349355
/*END CPP DELEGATES*/
350356
#endif
351357

352358
#if UNITY_EDITOR_OSX || UNITY_EDITOR_LINUX
353-
[DllImport("__Internal")]
359+
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
354360
static extern IntPtr dlopen(
355361
string path,
356362
int flag);
357363

358-
[DllImport("__Internal")]
364+
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
359365
static extern IntPtr dlsym(
360366
IntPtr handle,
361367
string symbolName);
362368

363-
[DllImport("__Internal")]
369+
[DllImport("__Internal", CallingConvention = CallingConvention.Cdecl)]
364370
static extern int dlclose(
365371
IntPtr handle);
366372

@@ -395,16 +401,16 @@ static T GetDelegate<T>(
395401
typeof(T)) as T;
396402
}
397403
#elif UNITY_EDITOR_WIN
398-
[DllImport("kernel32")]
404+
[DllImport("kernel32", SetLastError=true, CharSet = CharSet.Ansi)]
399405
static extern IntPtr LoadLibrary(
400406
string path);
401407

402-
[DllImport("kernel32")]
408+
[DllImport("kernel32", CharSet=CharSet.Ansi, ExactSpelling=true, SetLastError=true)]
403409
static extern IntPtr GetProcAddress(
404410
IntPtr libraryHandle,
405411
string symbolName);
406412

407-
[DllImport("kernel32")]
413+
[DllImport("kernel32.dll", SetLastError=true)]
408414
static extern bool FreeLibrary(
409415
IntPtr libraryHandle);
410416

@@ -437,86 +443,145 @@ static T GetDelegate<T>(
437443
typeof(T)) as T;
438444
}
439445
#else
440-
[DllImport(PLUGIN_NAME)]
446+
[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
441447
static extern void Init(
442448
IntPtr memory,
443449
int memorySize,
444450
InitMode initMode);
445451

446-
[DllImport(PLUGIN_NAME)]
452+
[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
447453
static extern void SetCsharpException(int handle);
448454

449455
/*BEGIN IMPORTS*/
450-
[DllImport(PLUGIN_NAME)]
456+
[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
451457
public static extern int NewBaseBallScript(int thisHandle);
452458

453-
[DllImport(PLUGIN_NAME)]
459+
[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
454460
public static extern void DestroyBaseBallScript(int thisHandle);
455461

456-
[DllImport(PLUGIN_NAME)]
462+
[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
457463
public static extern void MyGameAbstractBaseBallScriptUpdate(int thisHandle);
458464

459-
[DllImport(PLUGIN_NAME)]
465+
[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]
460466
public static extern void SetCsharpExceptionSystemNullReferenceException(int thisHandle);
461467
/*END IMPORTS*/
462468
#endif
463469

470+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
464471
delegate void ReleaseObjectDelegateType(int handle);
472+
473+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
465474
delegate int StringNewDelegateType(string chars);
475+
476+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
466477
delegate void SetExceptionDelegateType(int handle);
478+
479+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
467480
delegate int ArrayGetLengthDelegateType(int handle);
481+
482+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
468483
delegate int EnumerableGetEnumeratorDelegateType(int handle);
469484

470485
/*BEGIN DELEGATE TYPES*/
486+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
471487
delegate void ReleaseSystemDecimalDelegateType(int handle);
488+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
472489
delegate int SystemDecimalConstructorSystemDoubleDelegateType(double value);
490+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
473491
delegate int SystemDecimalConstructorSystemUInt64DelegateType(ulong value);
492+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
474493
delegate int BoxDecimalDelegateType(int valHandle);
494+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
475495
delegate int UnboxDecimalDelegateType(int valHandle);
496+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
476497
delegate UnityEngine.Vector3 UnityEngineVector3ConstructorSystemSingle_SystemSingle_SystemSingleDelegateType(float x, float y, float z);
498+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
477499
delegate UnityEngine.Vector3 UnityEngineVector3Methodop_AdditionUnityEngineVector3_UnityEngineVector3DelegateType(ref UnityEngine.Vector3 a, ref UnityEngine.Vector3 b);
500+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
478501
delegate int BoxVector3DelegateType(ref UnityEngine.Vector3 val);
502+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
479503
delegate UnityEngine.Vector3 UnboxVector3DelegateType(int valHandle);
504+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
480505
delegate int UnityEngineObjectPropertyGetNameDelegateType(int thisHandle);
506+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
481507
delegate void UnityEngineObjectPropertySetNameDelegateType(int thisHandle, int valueHandle);
508+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
482509
delegate int UnityEngineComponentPropertyGetTransformDelegateType(int thisHandle);
510+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
483511
delegate UnityEngine.Vector3 UnityEngineTransformPropertyGetPositionDelegateType(int thisHandle);
512+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
484513
delegate void UnityEngineTransformPropertySetPositionDelegateType(int thisHandle, ref UnityEngine.Vector3 value);
514+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
485515
delegate int SystemCollectionsIEnumeratorPropertyGetCurrentDelegateType(int thisHandle);
516+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
486517
delegate bool SystemCollectionsIEnumeratorMethodMoveNextDelegateType(int thisHandle);
518+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
487519
delegate int UnityEngineGameObjectMethodAddComponentMyGameBaseBallScriptDelegateType(int thisHandle);
520+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
488521
delegate int UnityEngineGameObjectMethodCreatePrimitiveUnityEnginePrimitiveTypeDelegateType(UnityEngine.PrimitiveType type);
522+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
489523
delegate void UnityEngineDebugMethodLogSystemObjectDelegateType(int messageHandle);
524+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
490525
delegate int UnityEngineMonoBehaviourPropertyGetTransformDelegateType(int thisHandle);
526+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
491527
delegate int SystemExceptionConstructorSystemStringDelegateType(int messageHandle);
528+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
492529
delegate int BoxPrimitiveTypeDelegateType(UnityEngine.PrimitiveType val);
530+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
493531
delegate UnityEngine.PrimitiveType UnboxPrimitiveTypeDelegateType(int valHandle);
532+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
494533
delegate float UnityEngineTimePropertyGetDeltaTimeDelegateType();
534+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
495535
delegate void BaseBallScriptConstructorDelegateType(int cppHandle, ref int handle);
536+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
496537
delegate void ReleaseBaseBallScriptDelegateType(int handle);
538+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
497539
delegate int BoxBooleanDelegateType(bool val);
540+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
498541
delegate bool UnboxBooleanDelegateType(int valHandle);
542+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
499543
delegate int BoxSByteDelegateType(sbyte val);
544+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
500545
delegate sbyte UnboxSByteDelegateType(int valHandle);
546+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
501547
delegate int BoxByteDelegateType(byte val);
548+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
502549
delegate byte UnboxByteDelegateType(int valHandle);
550+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
503551
delegate int BoxInt16DelegateType(short val);
552+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
504553
delegate short UnboxInt16DelegateType(int valHandle);
554+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
505555
delegate int BoxUInt16DelegateType(ushort val);
556+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
506557
delegate ushort UnboxUInt16DelegateType(int valHandle);
558+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
507559
delegate int BoxInt32DelegateType(int val);
560+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
508561
delegate int UnboxInt32DelegateType(int valHandle);
562+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
509563
delegate int BoxUInt32DelegateType(uint val);
564+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
510565
delegate uint UnboxUInt32DelegateType(int valHandle);
566+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
511567
delegate int BoxInt64DelegateType(long val);
568+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
512569
delegate long UnboxInt64DelegateType(int valHandle);
570+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
513571
delegate int BoxUInt64DelegateType(ulong val);
572+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
514573
delegate ulong UnboxUInt64DelegateType(int valHandle);
574+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
515575
delegate int BoxCharDelegateType(char val);
576+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
516577
delegate char UnboxCharDelegateType(int valHandle);
578+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
517579
delegate int BoxSingleDelegateType(float val);
580+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
518581
delegate float UnboxSingleDelegateType(int valHandle);
582+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
519583
delegate int BoxDoubleDelegateType(double val);
584+
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
520585
delegate double UnboxDoubleDelegateType(int valHandle);
521586
/*END DELEGATE TYPES*/
522587

Unity/Assets/NativeScript/Editor/GenerateBindings.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10397,6 +10397,7 @@ static void AppendCsharpDelegate(
1039710397
TypeKind returnTypeKind,
1039810398
StringBuilder output)
1039910399
{
10400+
output.Append("\t\t[UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n");
1040010401
output.Append("\t\tpublic delegate ");
1040110402
if (returnType == typeof(void))
1040210403
{
@@ -10527,7 +10528,7 @@ static void AppendCsharpImport(
1052710528
StringBuilder output
1052810529
)
1052910530
{
10530-
output.Append("\t\t[DllImport(PLUGIN_NAME)]\n");
10531+
output.Append("\t\t[DllImport(PLUGIN_NAME, CallingConvention = CallingConvention.Cdecl)]\n");
1053110532
output.Append("\t\tpublic static extern ");
1053210533
AppendCsharpTypeFullName(returnType, output);
1053310534
output.Append(' ');
@@ -12044,6 +12045,7 @@ static void AppendCsharpDelegateType(
1204412045
ParameterInfo[] parameters,
1204512046
StringBuilder output)
1204612047
{
12048+
output.Append("\t\t[UnmanagedFunctionPointer(CallingConvention.Cdecl)]\n");
1204712049
output.Append("\t\tdelegate ");
1204812050

1204912051
// Return type

0 commit comments

Comments
 (0)