首先,由于您没有说明您使用的是哪个 Delphi 版本,因此我将假设Delphi 6回答,除了我熟悉它之外没有其他原因。
您的 Delphi 过程未在其声明中指定调用约定,因此它不会根据您的导入使用stdcall 。它将使用默认的 Delphi寄存器约定,将前几个参数放在寄存器中而不是堆栈中。如果您可以更改您的 Delhpi DLL,请添加stdcall;在声明和重建之后,您的调用约定将匹配。
下表总结了调用约定。
Directive Parameter order Clean-up Passes parameters in registers?
--------- --------------- -------- -------------------------------
register Left-to-right Routine Yes
pascal Left-to-right Routine No
cdecl Right-to-left Caller No
stdcall Right-to-left Routine No
safecall Right-to-left Routine No
查看 .NET 文档,似乎没有与 Delphi 的注册约定相匹配的调用约定(见下表),因此我认为您唯一的选择可能是更改 Delphi DLL 中的约定。
Member name Description
----------- ------------------------
Cdecl The caller cleans the stack. This enables calling functions with varargs, which makes it appropriate to use for methods that accept a variable number of parameters, such as Printf.
FastCall This calling convention is not supported.
StdCall The callee cleans the stack. This is the default convention for calling unmanaged functions with platform invoke.
ThisCall The first parameter is the this pointer and is stored in register ECX. Other parameters are pushed on the stack. This calling convention is used to call methods on classes exported from an unmanaged DLL.
Winapi Supported by the .NET Compact Framework. This member is not actually a calling convention, but instead uses the default platform calling convention. For example, on Windows the default is StdCall and on Windows CE .NET it is Cdecl.
您的 Delphi (6) Pchar(指向空终止的 ANSI 字符串的指针)编组看起来是正确的。