mwe*_*ber 5 c# pinvoke unsafe function-pointers
我在DLL中有一个函数:
char __usercall MyUserCallFunction<al>(int arg1<esi>)
Run Code Online (Sandbox Code Playgroud)
因为我讨厌自己,所以我想使用P/Invoke在C#中调用它.
通常这可以通过获取函数委托来完成,la:
[UnmanagedFunctionPointer(CallingConvention.ThisCall, CharSet = CharSet.Auto, SetLastError = true)]
public delegate char DMyUserCallFunction(IntPtr a1);
Run Code Online (Sandbox Code Playgroud)
然后通过执行以下操作来调用它:
var MyFunctionPointer = (DMyUserCallFunction)Marshal.GetDelegateForFunctionPointer(AddressOfFunction, typeof(DMyUserCallFunction));
MyFunctionPointer(IntPtr.Zero);
Run Code Online (Sandbox Code Playgroud)
但是,对于自定义用户调用约定,这将导致程序崩溃.有没有某种方法我可以使用不安全的代码或某种将代理放在适当位置的包装函数来做到这一点,但是不强迫我编写C++ DLL?
谢谢!
编辑:
正如dtb所建议的,我创建了一个非常小的C++ DLL,我通过P/Invoke来调用该函数.对于任何好奇的人来说,C++中的函数最终看起来像:
extern "C" __declspec(dllexport) int __stdcall callUsercallFunction(int functionPointer, int arg1 )
{
int retVal;
_asm
{
mov esi, arg1
call functionPointer
mov retVal, eax
}
//Fake returning al, the lower byte of eax
return retVal & 0x000000FF;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2870 次 |
| 最近记录: |