4

我在delphi中有一个程序:

procedure PasswordDLL(month  integer; password  pchar); 
export;

该程序应将密码输出到我传入的“密码”pchar ..从我的谷歌..和阅读....参考:HEREHERE

我想出:

[DllImport(
    "DelphiPassword.dll",
    CallingConvention = CallingConvention.StdCall,
    CharSet = CharSet.Ansi,
EntryPoint = "PasswordDLL")]
public static extern void PasswordDLL(    
    int month,
    [MarshalAs(UnmanagedType.LPStr)] string password
    ); 

然后当我打电话时:

string pass = "";
PasswordDLL(2, pass);

所以要输出到“pass”字符串的密码。

但我会得到 BadImageFormatException is unhandled: 尝试加载格式不正确的程序。(来自 HRESULT 的异常:0x8007000B)

听起来我使用的函数格式是错误的?我想知道我是否为 PChar 使用了不正确的 UnmanagedType,但从阅读中,它是 LPWStr 和 LPStr .. 我错过了什么吗?

提前致谢...

4

1 回答 1

2

首先,由于您没有说明您使用的是哪个 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 字符串的指针)编组看起来是正确的。

于 2011-02-23T15:48:22.060 回答