Title: Passing String Data From DLL functions
This FAQ is to describe a method on how to pass string data (or record data) between a DLL function and a calling function. While you could use sharemem, this is not compatible with other languages and severely restricts the usefulness of the DLL code.
DLLs are discrete processes. This means that the only means of communication a DLL function has with the outside world is from the discrete variables passed to it. A common mistake is to involve a local variable in a return to PChar which is invalid, returns garbage data, or results in an access violation.
Sample DLL code
CODE
library testdll; uses sysutils, registry, windows;
// written by Glenn9999 at tek-tips.com. Demo illustrating how to pass string
// or record data from a DLL to a main program WITHOUT using ShareMem.
function GetWallPaperPath(wallpaper: PChar; var buflen: Word): Bool; stdcall;
{ gets the desktop wallpaper as currently set for the current user.
receives: Wallpaper as pointer to data buffer, buflen as the length of the
buffer provided to the DLL. Returns the data at the address supplied in
"wallpaper", and the actual length of the data in "buflen" }
var
regobj: TRegistry;
wpresult: string;
begin
Result := false;
regobj := TRegistry.Create;
try
regobj.rootkey := HKEY_CURRENT_USER;
if regobj.OpenKey('Control Panel\Desktop', false) then
begin
wpresult := regobj.ReadString('Wallpaper');
// You can't use wpresult as a direct basis for data return since it
// resides in an address that is not valid to the calling application.
// so you copy it to a supplied PChar which represents a buffer of
// sufficient size.
StrPCopy(wallpaper, wpresult);
// pass the real length of the data in buffer.
buflen := Length(wpresult);
Result := true;
end
finally
regobj.CloseKey;
regobj.free;
end;
end;
exports
GetWallPaperPath;
begin
end.
Main code which calls the DLL function.
CODE
{$APPTYPE CONSOLE}
program testmain; uses sysutils;
// main program written by Glenn9999 at tek-tips.com. Illustrates calling the
// DLL function provided as a sample.
function GetWallPaperPath(wallpaper: PChar; var buflen: Word): Bool;
stdcall; external 'testdll.dll';
var
Teststring: string;
teststringlen: Word;
begin
{ this is how I usually call Windows API functions which involve string data.
While there may be an easier way which is compatible, this seems the easiest
that I know. You can change the size of a string, point to the string and
then change it back to the actual length of the returned data. }
teststringlen := 255;
SetLength(teststring, teststringlen);
GetWallPaperPath(PChar(teststring), teststringlen);
SetLength(teststring, teststringlen);
{ writing * here so the length of the string can be easily spotted. Returning
a string that is complete and of the right length is perhaps the more difficult thing in calling "compatible" DLL functions which return string data.}
writeln('WallPaper path for current user is: ', teststring, '*');
readln;
end.