My DLL might send more than one result/return value to exe in one shoot. I still don’t understand how to make the callback function so DLL can communicate with host app.
Here’s the scenario :
App :
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;
Actually I still get an error from code above. So in my case, I need your help to explain and determine what is the correct way to send data from DLL to appp.
You are on the right lines but the most obvious problem that I can see is the use of
stringvariables. These are heap allocated and since you have two separate memory managers you will be allocating on one heap (in the DLL) and then freeing on a different heap (in the app).There are a few options. One options would be to share memory managers but I don’t recommend this for a variety of reasons. Without going into them you state in a comment that you want non Delphi applications to be able to use your DLL which would preclude the use of a shared memory manager.
Another option would be to force the calling app to allocate the memory for the string and then let your DLL copy into that memory. This works fine but is somewhat labour intensive.
Instead I would use a string type which can be allocated in one module but freed in a different module. The COM
BSTRis such a type and in Delphi terms this isWideString. Change the code to useWideStringfor any exported functions.I would also simplify the importing/exporting process and use implicit dynamic linking.
DLL
App