Call UnmanagedFunctionPointer in C # for custom calls

I have a function in a DLL:

char __usercall MyUserCallFunction<al>(int arg1<esi>)

Since I hate myself, I would call it from within C # using P / Invoke.

This can usually 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 calling it, doing:

var MyFunctionPointer = (DMyUserCallFunction)Marshal.GetDelegateForFunctionPointer(AddressOfFunction, typeof(DMyUserCallFunction));

MyFunctionPointer(IntPtr.Zero);

However, for user conditional user calls, this will cause the program to crash. Is there a way I can do this using unsafe code or some kind of wrapper function that puts delegates in place but doesn't force me to write a C ++ DLL?

Thank!

Edit:

As dtb suggested, I created a very small C ++ DLL that I use through P / Invoke to call a function. Anyone is curious that a function in C ++ looks like this:

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;
}
+5
1

# . DLL, ++/CLI DLL.

+2

All Articles