简体   繁体   English

Delphi XE2中从dll调用程序

[英]Calling procedure from dll in Delphi XE2

So, I'm trying to call a procedure from a DLL in Delphi XE2.所以,我试图从 Delphi XE2 中的 DLL 调用一个过程。 but the procedure just won't assign.但程序只是不会分配。

I have tried several examples found on the internet.我已经尝试了在互联网上找到的几个例子。 The DLL is being loaded as expected. DLL 正在按预期加载。 The exports are correctly written.导出正确写入。

Everything seems fine but still no success.一切似乎都很好,但仍然没有成功。

What is up with that?这是怎么回事?

The code I have is the following我的代码如下

type
  TStarter = procedure; stdcall;

...

fTheHookStart: TStarter;

...

procedure TForm1.LoadHookDLL;
begin
  LogLn('Keyboard Hook: Loading...');
  // Load the library
  DLLHandle := LoadLibrary('thehookdll.DLL');

  // If succesful ...
  if Handle <> 0 then
  begin
    LogLn('Keyboard Hook: DLL load OK!');
    LogLn('Keyboard Hook: assigning procedure ...');

    fTheHookStart := TStarter(GetProcAddress(DLLHandle, 'StartTheHook'));
    if @fTheHookStart <> nil then
    begin
      LogLn('Keyboard Hook: procedure assignment OK!');
      LogLn('Keyboard Hook: Starting...');
      fTheHookStart;
    end
    else
    begin
      LogLn('Keyboard Hook: procedure assignment FAIL!');
      FreeLibrary(DLLHandle);
      if Handle <> 0 then LogLn('Keyboard Hook: DLL free OK!') else LogLn('Keyboard Hook: DLL free FAIL!');
    end;
  end
  else
  begin
    LogLn('Keyboard Hook: DLL load FAIL!');
  end;
end;

One error is that you assign DllHandle when you load the dll, but then you check if Handle <> nil.一个错误是您在加载 dll 时分配了DllHandle ,但随后您检查Handle <> nil。 Handle is actually your forms handle, which ofcourse is not nil.句柄实际上是您的表单句柄,当然不是零。 That will not matter if the loading succeeded, but if it failed, you will get wrong logging.加载成功与否无关紧要,但如果失败,您将得到错误的日志记录。 Since you also have some logging functions, what does the log show?既然你也有一些日志功能,那么日志显示什么?

As I understand it, the DLL loads, but GetProcAddress returns nil .据我了解,DLL 加载,但GetProcAddress返回nil There is only one such failure mode.只有一种这样的故障模式。 The DLL does not export a function with that name. DLL 不会导出具有该名称的函数。

Watch out for name decoration and letter case.注意名称装饰和字母大小写。 C and C++ DLLs may export decorated names. C 和 C++ DLL 可以导出修饰名称。 And exported names are sensitive to letter case.导出的名称对字母大小写敏感。

Use dumpbin or Dependency Walker to check the exported function name.使用dumpbin或 Dependency Walker 检查导出的函数名称。

For reference, when GetProcAddress fails, as the documentation explains, a call to GetLastError will yield an error code.作为参考,当GetProcAddress失败时,如文档所述,调用GetLastError将产生错误代码。


And it looks like the other answer is onto something.看起来另一个答案是关于某事的。 You believe that you have loaded the DLL correctly, but your code doesn't perform that check correctly.您相信您已正确加载了 DLL,但您的代码没有正确执行该检查。

If you'd called GetLastError then the system could have alerted you to this.如果您调用了GetLastError那么系统可能会提醒您这一点。 If you'd inspected the variables under the debugger, the problem would have been obvious.如果您检查了调试器下的变量,问题就会很明显。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM