5
我有一个DLL函数:调用一个UnmanagedFunctionPointer在C#中的自定义调用约定
char __usercall MyUserCallFunction<al>(int arg1<esi>)
因为我恨我自己,我想从C#中使用P/Invoke来调用它。
通常,这可以通过获取函数委托,一拉来完成:
[UnmanagedFunctionPointer(CallingConvention.ThisCall, CharSet = CharSet.Auto, SetLastError = true)]
public delegate char DMyUserCallFunction(IntPtr a1);
然后做调用它:
var MyFunctionPointer = (DMyUserCallFunction)Marshal.GetDelegateForFunctionPointer(AddressOfFunction, typeof(DMyUserCallFunction));
MyFunctionPointer(IntPtr.Zero);
对于自定义用户调用约定,但是,这将导致该程序崩溃。是否有某种方式可以使用不安全的代码或某种封装函数将代理置于适当位置,但不会强制我编写C++ DLL?
谢谢!
编辑:
如所建议的通过DTB,我创建了一个非常小的C++ DLL我通过P /调用用来调用该函数。对于任何人来说,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;
}
+1“因为我讨厌自己” – dtb
为什么你需要代表呢?你不能'[DllImport(...)]公共外部字节MyUserCallFunction(IntPtr a1)'? – dtb
该函数并未实际导出。我需要通过注入加载DLL的进程来调用该函数。我为此使用EasyHook--目标是钩住任何我可以在DLL中使用IDA找到的任意函数。 – mweber