使用delphi的DLL过程中的参数不匹配

时间:2014-07-13 14:46:04

标签: delphi dll parameters

我正在使用Delphi 2007和DLL,我编写了一个程序,它使用整数和字符串作为参数。我在程序中动态加载dll以测试它并在屏幕上打印参数作为程序的第一条指令。对于整数,传递的值为0,对于字符串,传递的值为“”,但是当打印它们时,它们变为类似于14532和垃圾字符串。我知道我必须使用具体的字符串类型进行dll调用,我可以修复它,但整数的情况对我来说似乎很神秘。有什么想法吗?

我现在无法访问代码,但我会尝试重现它:

我的程序类似于

procedure proc(Code: integer; cod: String);stdcall;
begin;
     showmessage(inttostr(code)+ " "+cod);
end;

另一方面是:

type
call: TCall(Code: integer; cod:String)
handler: THandler
end
....
....
procedure onClickEvent(...)
begin;
handler := loadlibrary(.../library.dll);
if handler <> 0 then
begin
    @call := getprocaddress(handler, "proc");
    if call <> nil then
         call(0,"");
end
freelibrary(handler)
end

2 个答案:

答案 0 :(得分:1)

  

注意:您发布的代码不是您的真实代码。它不会   编译。请仅发布直接从您的复制和粘贴的代码   编辑。不要重新输入或补充它。

正如评论所说,不要使用string。这是特定于Delphi的类型,它甚至不总是相同的。其他语言不知道如何使用它。根据您的需求,使用PWideCharPAnsiChar

此外,您对Call的声明是错误的(您必须将其声明为stdcall,从那时起,参数将在堆栈上传递。如果您忘记了,参数将在寄存器中传递,但DLL期望它们在堆栈中:)

type
  TCall = procedure(Code: Integer; Cod: PWideChar) stdcall;

var
  Call: TCall;

在DLL中实现:

procedure proc(Code: integer; Cod: PWideChar); stdcall;
begin
  ShowMessage(IntToStr(Code)+ " " + Cod);
end;

现在你可以这样称呼它:

MyString := 'Hello';
Call(0, PWideChar(MyString)); 

答案 1 :(得分:0)

在dll中使用PChar,而不是字符串。 http://delphi.wikia.com/wiki/Creating_DLLs

中的更多信息