Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 MyFunctionPointer = (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;
}
like image 607
mweber Avatar asked Nov 05 '22 13:11

mweber


1 Answers

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.

like image 112
David Heffernan Avatar answered Nov 12 '22 23:11

David Heffernan