Registriert seit: 17. Mai 2007
482 Beiträge
Delphi XE6 Professional
|
Re: How to - string in DLL
19. Jun 2011, 21:11
Delphi-Quellcode:
library StringDLL;
uses
SysUtils;
function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; stdcall;
var
S: string;
begin
S := 'foo' + Data;
if Assigned(Buffer) then
StrLCopy(Buffer, PWideChar(S), lenBuffer)
;
Result := Length(S);
end;
exports
Foonction;
begin
end.
Dynamic - working:
Delphi-Quellcode:
var
hLib: THandle;
func1: function(s: PChar; Buffer: PChar; lenBuffer: Integer): Integer; stdcall;
len: Integer;
Buffer: PChar;
begin
Buffer := nil;
hLib := LoadLibrary('StringDLL.dll');
if hLib <> 0 then
begin
ListBox1.Items.Add('hlib: ' + IntToStr(hLib));
@func1 := GetProcAddress(hLib, 'Foonction');
if Assigned(func1) then
begin
ListBox1.Items.Add('@func1: ' + IntToStr(Integer(@func1)));
len := func1('bar', nil, 0);
ListBox1.Items.Add('len: ' + IntToStr(len));
try
GetMem(Buffer, len + 1);
len := func1('bar', Buffer, len + 1);
ListBox1.Items.Add(String(Buffer)+ ' [' + IntToStr(len) + ']');
finally
FreeMem(Buffer);
end;
end;
end;
end;
Static - don't working:
Delphi-Quellcode:
function Foonction(Data: PWideChar; Buffer: PWideChar; lenBuffer: Cardinal): Cardinal; external 'StringDLL.dll';
var
Buffer: PWideChar;
BufferSize: DWORD;
begin
BufferSize := Foonction('bar', nil, 0);
ShowMessage(IntToStr(BufferSize));
GetMem(Buffer, BufferSize + 1);
try
Foonction('bar', Buffer, BufferSize + 1);
ShowMessage(Buffer);
finally
FreeMem(Buffer);
end;
end;
Now I have identical sizes, what's wrong?
|
|
Zitat
|