【发布时间】:2011-07-01 17:00:12
【问题描述】:
我的 DLL 可能会一次性向 exe 发送多个结果/返回值。我仍然不明白如何制作回调函数,以便 DLL 可以与主机应用程序通信。
这是场景:
应用:
type
TCheckFile = function(const Filename, var Info, Status: string): Boolean; stdcall;
var
CheckFile: TCheckFile;
DLLHandle: THandle;
Procedure Test;
var
Info,Status : string;
begin
....
// load the DLL
DLLHandle := LoadLibrary('test.dll');
if DLLHandle <> 0 then
begin
@CheckFile := GetProcAddress(DLLHandle, 'CheckFile');
if Assigned(CheckFile) then
beep
else
exit;
end;
// use the function from DLL
if Assigned(CheckFile) then
begin
if CheckFile(Filename, Info, Status) then
begin
AddtoListView(Filename, Info, Status);
end;
end;
...
end;
DLL:
function CheckFile(const Filename, var Info,Status: string): Boolean; stdcall;
var
Info, Status: string;
begin
if IsTheRightFile(Filename, Info,Status) then
begin
result := true;
exit;
end
else
begin
if IsZipFile then
begin
// call function to extract the file
ExtractZip(Filaname);
// check all extracted file
for i := 0 to ExtractedFileList.count do
begin
IsTheRightFile(ExtractedFile, Info, Status) then
// how to send the Filename, Info and Status to exe ?? // << edited
// SendIpcMessage('checkengine', pchar('◦test'), length('◦test') * SizeOf(char)); error!
// "AddtoListView(Filename, Info);" ???
end;
end;
end;
end;
实际上我仍然从上面的代码中得到一个错误。因此,就我而言,我需要您的帮助来解释和确定将数据从 DLL 发送到 appp 的正确方法。
【问题讨论】:
-
GetProcAddress(DLLHandle, 'CheckFile'); - 它返回一个有效的句柄吗?您没有忘记在 IMPORTS 部分指定您的 dll 功能吗?
-
您的意思是
Info和Status而不是Info和Filename?因为Filename是const,不能修改。要修改Info和Status,您需要在您的DLL 函数中为其设置一个值。请注意,如果您不在uses子句中使用ShareMem,则传递字符串可能会导致问题。 -
@heximal :是的,它返回一个有效的句柄。我确信导入/导出功能没有问题。我尝试在 DLL 中使用虚拟函数。
-
@ba__friend。嗯..我使用 FastMM 是因为我希望非 Delphi exe 可以使用我的 DLL。
-
您的代码是否真的包含带有
Info&Status形式参数和Info&Status局部变量的函数?