如何将表单句柄传递给 DLL 以在 Windows API 中使用?

发布于 2024-12-17 10:50:20 字数 1613 浏览 4 评论 0原文

首先,我对 DLL 不太熟悉。我以前做过,但了解很少,总是遇到问题。

我正在构建的这个 DLL 需要将 Windows 窗体句柄 (HWND) 传递到 DLL 函数中,并且 DLL 应使用该句柄调用 Windows API 函数。当尝试调用任何函数(从 Win7InitTaskbar 开始)时,我不断遇到访问冲突 - 就好像它甚至无法调用该函数一样。这让我得出结论,它一定是 HWND 参数导致它崩溃...我想...

library Win7;

uses
  //Do I need ShareMem?
  //ShareMem,      //<---
  Windows,
  Forms,
  JDWin7,
  SysUtils,
  Classes;

{$R *.res}

function Win7InitTaskbar(const FormHandle: HWND): Bool; stdcall;
begin
  Result:= InitializeTaskbarAPI(FormHandle);
end;

function Win7InitForm(const FormHandle: HWND): Bool; stdcall;
begin

end;

function Win7SetTaskbarState(const AState: Cardinal): Bool; stdcall;
begin
  Result:= SetTaskbarProgressState(AState);
end;

function Win7SetTaskbarValue(const ACurrent: UInt64; const AMax: UInt64): Bool; stdcall;
begin                                        //is UInt64 Safe for DLL?
  Result:= SetTaskbarProgressValue(ACurrent, AMax);
end;

exports
  Win7InitTaskbar,
  Win7InitForm,
  Win7SetTaskbarState,
  Win7SetTaskbarValue;

begin
end.

DLL 函数的实现:

function Win7InitTaskbar(const FormHandle: HWND): Bool;
  external W7DLL;
function Win7SetTaskbarState(const AState: Cardinal): Bool;
  external W7DLL;
function Win7SetTaskbarValue(const ACurrent: UInt64; const AMax: UInt64): Bool;
  external W7DLL;

无论我使用 ShareMem 还是不(我也不想使用)。使用 HWND 参数发布函数是否安全?我也尝试过 LongWord,但仍然没有成功。如果我直接在应用程序内部使用内部函数 InitializeTaskbarAPI,它实际上可以在 DLL 外部完美运行。但在本例中,我想将它们放在共享 DLL 中。

另外,将 UInt64 传递到 DLL 中是否安全?当我获得源代码时,其中一个函数已经使用此参数类型发布了。

First of all, I'm not too comfortable with DLL's. I've done them before, but know very little and always have problems.

This DLL I'm building requires passing a windows form handle (HWND) into the DLL function, and the DLL shall call a Windows API function using that handle. I keep getting an access violation when trying to call any function (starting from Win7InitTaskbar) - as if it failed to even call the function. That made me conclude that it must be the HWND parameter making it crash... I think...

library Win7;

uses
  //Do I need ShareMem?
  //ShareMem,      //<---
  Windows,
  Forms,
  JDWin7,
  SysUtils,
  Classes;

{$R *.res}

function Win7InitTaskbar(const FormHandle: HWND): Bool; stdcall;
begin
  Result:= InitializeTaskbarAPI(FormHandle);
end;

function Win7InitForm(const FormHandle: HWND): Bool; stdcall;
begin

end;

function Win7SetTaskbarState(const AState: Cardinal): Bool; stdcall;
begin
  Result:= SetTaskbarProgressState(AState);
end;

function Win7SetTaskbarValue(const ACurrent: UInt64; const AMax: UInt64): Bool; stdcall;
begin                                        //is UInt64 Safe for DLL?
  Result:= SetTaskbarProgressValue(ACurrent, AMax);
end;

exports
  Win7InitTaskbar,
  Win7InitForm,
  Win7SetTaskbarState,
  Win7SetTaskbarValue;

begin
end.

Implementation of DLL functions:

function Win7InitTaskbar(const FormHandle: HWND): Bool;
  external W7DLL;
function Win7SetTaskbarState(const AState: Cardinal): Bool;
  external W7DLL;
function Win7SetTaskbarValue(const ACurrent: UInt64; const AMax: UInt64): Bool;
  external W7DLL;

I had this problem whether I used ShareMem or not (Which, I also do not want to use). Is it safe to publish the function with a HWND parameter? I tried LongWord as well, still no luck. The internal function InitializeTaskbarAPI does in fact work perfectly outside of the DLL, if i were to use it directly inside the app. But in this case, I want to put these in a shared DLL.

Also, is it safe to pass UInt64 into a DLL? One of the functions was already published with this parameter type when I got the source.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

べ映画 2024-12-24 10:50:20

在我看来,您的问题与使用 Sharemem 或将 Form.Handle 传递给 HWND 参数无关。

这只是调用约定不匹配。您导出为 stdcall,然后导入为 register。每当你这样做时,运行时错误肯定会随之而来。

您需要这样做:

function Win7InitTaskbar(const FormHandle: HWND): Bool; 
  stdcall; external W7DLL;
function Win7SetTaskbarState(const AState: Cardinal): Bool;
  stdcall; external W7DLL;
function Win7SetTaskbarValue(const ACurrent: UInt64; const AMax: UInt64): Bool;
  stdcall; external W7DLL;

并且无论其价值如何,您都不需要这里的 Sharemem。仅当您在一个模块中分配内存但在另一个模块中释放内存时才需要这样做。将 Form.Handle 传递给 DLL 中的 HWND 参数也不是问题。当您调用 Windows API 函数时,您总是会执行此操作。

Your problem here appears to me to be unrelated to using Sharemem or passing Form.Handle to an HWND parameter.

It is simply a calling convention mismatch. You export as stdcall but then import as register. Whenever you do that, runtime errors are sure to follow.

You need to do it like this:

function Win7InitTaskbar(const FormHandle: HWND): Bool; 
  stdcall; external W7DLL;
function Win7SetTaskbarState(const AState: Cardinal): Bool;
  stdcall; external W7DLL;
function Win7SetTaskbarValue(const ACurrent: UInt64; const AMax: UInt64): Bool;
  stdcall; external W7DLL;

And for what it is worth, you don't need Sharemem here. You only need that when you allocate memory in one module but free it in a different one. And passing Form.Handle to an HWND parameter in a DLL is not a problem. You do this all then time when you call Windows API functions.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文