P/Invoke 和 C# 的自定义调用约定
-
23-08-2019 - |
题
我有一个业务案例,在使用 P/Invoke 时,我需要能够指定自己的调用约定。具体来说,我有一个使用非标准 ABI 的遗留 dll,并且我需要能够指定每个函数的调用约定。
例如,该 dll 中的一个函数通过 EAX 和 EBX 接受前两个参数,其余参数通过堆栈接受。另一个函数通过 ECX 接受一个参数,其余的在堆栈上。我有几百个这样的函数,并且希望避免编写自己的中间桥 DLL 来访问这些函数。
我的另一个选择是手动滚动我自己的自定义 P/Invoke,由于显而易见的原因,这是不可取的。
任何帮助表示赞赏,谢谢,
解决方案
我不明白您对自定义 P/Invoke 的意思,但我不明白如果没有带有内联汇编的非托管 C++,您如何摆脱困境。然而,由于几乎所有内容都作为 32 位值传递,因此您可能只需为每个函数签名编写一个代理,而不是每个函数一个代理。或者您可以编写一个代码生成器,从 XML 生成代理。不过,我不认为这个版本太不可取,因为所有代理函数都非常简单:
int RealFunction(int param1, const char * param2, char param 3);
int MyFunction(int param1, int param2, int param3) { // argument types do not matter as long as they are not doubles or structures
__asm {
mov eax, param1
mov ebx, param2
push param3
call RealFunction
; depending on calling convention, you might need to do add esp, 12 here
; if RealFunction does not return its result in eax, you will need to do mov eax, <wherever the return value is> here
}
}
其他提示
我相当确定,如果没有单独的 dll,就没有内置方法可以完成您想要的任务。除了运行时系统支持的调用约定之外,我还没有看到任何指定调用约定的方法。
我不久前正在学习调用约定,并编写了一些代码来转换调用约定。该代码从特殊的 C# 包装器调用,包装器库使用反射发射(无法使 Marshal.getdelegateforfunctionpointer 工作)为特殊的裸存根方法发出新的 p/invoke 方法。它修复参数,然后调用实际方法。
这是c代码。我手头没有 C# 部分:(当时我也在学习汇编程序,所以代码可能很糟糕:)
typedef struct
{
USHORT ParameterOneOffset; // The offset of the first parameter in dwords starting at one
USHORT ParameterTwoOffset; // The offset of the second parmaeter in dwords starting at one
} FastCallParameterInfo;
__declspec( naked,dllexport ) void __stdcall InvokeFast()
{
FastCallParameterInfo paramInfo;
int functionAddress;
int retAddress;
int paramOne, paramTwo;
__asm
{
// Pop the return address and parameter info. Store in memory.
pop retAddress;
pop paramInfo;
pop functionAddress;
// Check if any parameters should be stored in edx
movzx ecx, paramInfo.ParameterOneOffset;
cmp ecx,0;
je NoRegister;
// Calculate the offset for parameter one.
movzx ecx, paramInfo.ParameterOneOffset; // Move the parameter one offset to ecx
dec ecx; // Decrement by 1
mov eax, 4; // Put 4 in eax
mul ecx; // Multiple offset by 4
// Copy the value from the stack on to the register.
mov ecx, esp; // Move the stack pointer to ecx
add ecx, eax; // Subtract the offset.
mov eax, ecx; // Store in eax for later.
mov ecx, [ecx]; // Derefernce the value
mov paramOne, ecx; // Store the value in memory.
// Fix up stack
add esp,4; // Decrement the stack pointer
movzx edx, paramInfo.ParameterOneOffset; // Move the parameter one offset to edx
dec edx; // Decrement by 1
cmp edx,0; // Compare offset with zero
je ParamOneNoShift; // If first parameter then no shift.
ParamOneShiftLoop:
mov ecx, eax;
sub ecx, 4;
mov ecx, [ecx]
mov [eax], ecx; // Copy value over
sub eax, 4; // Go to next
dec edx; // decrement edx
jnz ParamOneShiftLoop; // Loop
ParamOneNoShift:
// Check if any parameters should be stored in edx
movzx ecx, paramInfo.ParameterTwoOffset;
cmp ecx,0;
je NoRegister;
movzx ecx, paramInfo.ParameterTwoOffset; // Move the parameter two offset to ecx
sub ecx, 2; // Increment the offset by two. One extra for since we already shifted for ecx
mov eax, 4; // Put 4 in eax
mul ecx; // Multiple by 4
// Copy the value from the stack on to the register.
mov ecx, esp; // Move the stack pointer to ecx
add ecx, eax; // Subtract the offset.
mov eax, ecx; // Store in eax for later.
mov ecx, [ecx]; // Derefernce the value
mov paramTwo, ecx; // Store the value in memory.
// Fix up stack
add esp,4; // Decrement the stack pointer
movzx edx, paramInfo.ParameterTwoOffset; // Move the parameter two offset to ecx
dec edx; // Decrement by 1
cmp edx,0; // Compare offset with zero
je NoRegister; // If first parameter then no shift.
ParamTwoShiftLoop:
mov ecx, eax;
sub ecx, 4;
mov ecx, [ecx]
mov [eax], ecx; // Copy value over
sub eax, 4; // Go to next
dec edx; // decrement edx
jnz ParamTwoShiftLoop; // Loop
NoRegister:
mov ecx, paramOne; // Copy value from memory to ecx register
mov edx, paramTwo; //
push retAddress;
jmp functionAddress;
}
}
不隶属于 StackOverflow