[fix] fixed PInvokeAnalyzer bug in computing PInvoke function calling conventions.

main
walon 2025-08-25 19:23:29 +08:00
parent 0642726469
commit e36bbb9022
3 changed files with 22 additions and 2 deletions

View File

@ -5,5 +5,7 @@ namespace HybridCLR.Editor.MethodBridge
public class CallNativeMethodSignatureInfo public class CallNativeMethodSignatureInfo
{ {
public MethodSig MethodSig { get; set; } public MethodSig MethodSig { get; set; }
public CallingConvention? Callvention { get; set; }
} }
} }

View File

@ -638,7 +638,7 @@ namespace HybridCLR.Editor.MethodBridge
sharedMethod.Init(); sharedMethod.Init();
sharedMethod = ToIsomorphicMethod(sharedMethod); sharedMethod = ToIsomorphicMethod(sharedMethod);
CallingConvention callingConv = (CallingConvention)((int)(method.MethodSig.CallingConvention & dnlib.DotNet.CallingConvention.Mask) + 1); CallingConvention callingConv = (CallingConvention)((int)((method.Callvention ?? method.MethodSig.CallingConvention) & dnlib.DotNet.CallingConvention.Mask) + 1);
string signature = MakeCalliSignature(sharedMethod, callingConv); string signature = MakeCalliSignature(sharedMethod, callingConv);
if (!methodsBySig.TryGetValue(signature, out var arm)) if (!methodsBySig.TryGetValue(signature, out var arm))

View File

@ -3,6 +3,7 @@ using HybridCLR.Editor.Meta;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
using UnityEngine; using UnityEngine;
@ -26,6 +27,19 @@ namespace HybridCLR.Editor.MethodBridge
} }
} }
private CallingConvention GetCallingConvention(MethodDef method)
{
switch (method.ImplMap.CallConv)
{
case PInvokeAttributes.CallConvWinapi: return CallingConvention.Default;
case PInvokeAttributes.CallConvCdecl: return CallingConvention.C;
case PInvokeAttributes.CallConvStdCall: return CallingConvention.StdCall;
case PInvokeAttributes.CallConvThiscall: return CallingConvention.ThisCall;
case PInvokeAttributes.CallConvFastcall: return CallingConvention.FastCall;
default: return CallingConvention.Default;
}
}
public void Run() public void Run()
{ {
foreach (var mod in _rootModules) foreach (var mod in _rootModules)
@ -40,7 +54,11 @@ namespace HybridCLR.Editor.MethodBridge
{ {
Debug.LogError($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature."); Debug.LogError($"PInvoke method {method.FullName} has unsupported parameter or return type. Please check the method signature.");
} }
_pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo { MethodSig = method.MethodSig }); _pinvokeMethodSignatures.Add(new CallNativeMethodSignatureInfo
{
MethodSig = method.MethodSig,
Callvention = method.HasImplMap? GetCallingConvention(method) : (CallingConvention?)null,
});
} }
} }
} }