Delphi Xe.
It is given:
1.dll, in it a unit
unit DllUnit; interface
uses windows, sysutils;
Procedure GuPrcA(var p:PAnsiChar;const l:integer); StdCall;
Procedure GuPrcW(var p:PWideChar;const l:integer); StdCall;
Exports GuPrcA,GuPrcW;
implementation
procedure GuMes(s:string);
begin
MessageBox(0,pchar(s),'From dll',mb_iconinformation);
end;
Procedure GuPrcW(var p:PWideChar;const l:integer); // wide
var s:widestring;
begin
if (p=nil)or(l<1) then begin p:=nil;exit;end;
SetLength(s,trunc(l/sizeof(widechar)));Move(p^,Pointer(s)^,l);
gumes('l: '+inttostr(l)+', nl: '+inttostr(length(s))+#10+'-'+s+'-');
s:=widestring(Uppercase(s));Move(Pointer(s)^,p^,l);
end;
Procedure GuPrcA(var p:Pansichar;const l:integer); // ansi
var s:ansistring;
begin
if (p=nil)or(l<1) then begin p:=nil;exit;end;
SetLength(s,l);Move(p^,Pointer(s)^,l);
gumes('l: '+inttostr(l)+', nl: '+inttostr(length(s))+#10+'-'+s+'-');
s:=ansistring(AnsiUppercase(s));Move(Pointer(s)^,p^,l);
end;
Initialization
ReportMemoryLeaksOnShutdown:=true;
end.
2.The program, at a window memo and 2 buttons
...
implementation
{$R *.dfm}
Procedure GuPrcA(var p:PansiChar;const l:cardinal); StdCall; external 'mydll.dll' name 'GuPrcA';
Procedure GuPrcW(var p:PwideChar;const l:cardinal); StdCall; external 'mydll.dll' name 'GuPrcW';
procedure TForm1.Button6Click(Sender: TObject);
var p:pwidechar;c:cardinal;s:widestring;
begin
s:=widestring(memo1.Text);
c:=length(s)*sizeof(widechar);
p:=allocmem(c);
Move(Pointer(s)^,p^,c);
GuPrcW(p,c);
s:='';setlength(s,trunc(c/sizeof(widechar)));
Move(p^,Pointer(s)^,c);
Freemem(p,c);
memo1.Text:='='+s+'= l:'+inttostr(c);
end;
procedure TForm1.Button7Click(Sender: TObject);
var p:pansichar;c:cardinal;s:ansistring;
begin
s:=ansistring(memo1.text);
c:=length(s);
p:=allocmem(c);
Move(Pointer(s)^,p^,c);
GuPrcA(p,c);
s:='';setlength(s,c);
Move(p^,Pointer(s)^,c);
Freemem(p,c);
memo1.Text:='='+s+'= l:'+inttostr(c);
end;
Initialization
ReportMemoryLeaksOnShutdown:=true;
end.
To download an source code both it is possible here: http://www.multiupload.com/WSZKF8IGP1
By pressing buttons there is a transfer in dll lines (ansi or wide string), its display in msgbox dll, its processing there (idle time uppercase) and return to the program. The manager of memory is not used (fast – simple – sharemem). Both in a program and in dll are included ReportMemoryLeaksOnShutdown (display of events about memory losses) which both are silent, i.e. like all would work also lengths of lines too everywhere coincide.
That is necessary:
-
To look, whether there are no there errors (to check up on errors)
-
Whether it is possible to optimise or offer a way better or easier
-
Whether it is possible to address to this DLL with such procedures from VB or C++/C# ?
Thanks
If you do not want to use the heap manager for you transmission of data, just use
WideStringkind of string.It will be (a bit) slower, but it will allow you to change the string length on every side.
And it will be true Unicode, so you won’t have any trouble about char set and such when using native Delphi
UnicodeStringunder XE. You can useWideStringin your code and the conversion tostringwill be done silently.It will be all managed by Windows, so even non Delphi libraries or applications (like .Net or C++) would be able to handle it directly.