Calling an UnmanagedFunctionPointer in C# for custom calling conventions
I have a function in a DLL:
char __usercall MyUserCallFunction<al>(int arg1<esi>)
Because I hate myself I'd like to call this from within C# using P/Invoke.
Normally this can be done by getting a function delegate, a la:
[UnmanagedFunctionPointer(CallingConvention.ThisCall, CharSet = CharSet.Auto, SetLastError = true)]
public delegate char DMyUserCallFunction(IntPtr a1);
And then invoking it by doing:
var My开发者_如何转开发FunctionPointer = (DMyUserCallFunction)Marshal.GetDelegateForFunctionPointer(AddressOfFunction, typeof(DMyUserCallFunction));
MyFunctionPointer(IntPtr.Zero);
For custom user calling conventions, however, this will cause the program to crash. Is there some sort of way I can do this using unsafe code or some sort of wrapper function that puts the delegates in place, but doesn't force me to write a C++ DLL?
Thanks!
Edit:
As suggested by dtb, I created a very small C++ DLL which I use via P/Invoke to call the function. For anyone curious the function in C++ ends up looking like:
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;
}
There's no hope of implementing custom calling conventions in C#. You must do this in either a native DLL or a C++/CLI DLL.
精彩评论