Article View: comp.lang.pascal.delphi.misc
Article #600Re: Passing a string to a DLL procedure with PChar
From: Tor Engebakken
Date: Fri, 05 Mar 1999 00:00
Date: Fri, 05 Mar 1999 00:00
96 lines
3423 bytes
3423 bytes
"Le Doute" <ledoute@sceptiques.qc.ca> wrote:
>Hi,
>
>I'm a certainly not an expert in object pascal, and I've just ran into some
>trouble:
>
>I need to convert one of my app (some kind of "utility") into a DLL to sell
>what it does without selling its source (!). I can convert my app to a DLL
>that would require only few inputs.
>
>I need to pass to the DLL some strings. But ObjPascal has its own way to
>manage strings which are not compatible with other compilers. Using
>ShareMem as Borland recommands isn't really what I want.
>
>I have no experience with pointers so I would need some help:
>
>My DLL needs to receive a string.
>
>How can I export this string to the DLL through a pointer, and how do I
>reconvert the pointer to a string inside my DLL? And "vice-versa", how can I
>import a DLL output through a pointer.
>
>For now, my code would look like this:
>
>procedure ProcedureInsideTheDLL (Input:String; Output:String);
>
>
This procedure should be on the DLL-side: (It reads a string from a
indexed string resource of the dll. (Resource,res))
Main file:
library dllstr;
uses
Loadres in 'LOADRES.PAS';
exports
GetResource index 1;
{$R *.RES}
{$R resource.res}
begin
end.
(Unit that actually contain the procedure)
UNIT loadres;
INTERFACE
var
buffer:array[0..255] of char;
Procedure Getresource(i:integer;P:PChar); export;
IMPLEMENTATION
uses sysutils,winprocs;
Procedure Getresource(i:integer; P:PChar);
Begin
LoadString(HInstance,i,Buffer,Sizeof(Buffer));
StrCopy(P,buffer);
end;
end.
This should go on the calling side:
type
Getresproc = procedure(i:integer;P:Pchar);
var
Getres : getresproc;
Function LoadDLL(dllname:string):boolean;
Var
buffer:array[0..255]of char;
entrypoint:TFarproc;
Begin
entrypoint := nil;
StrPCopy(buffer,dllname);
DLLHandle:=LoadLibrary(buffer);
If DLLHandle < 22 then
result :=False
else begin
StrPCopy(buffer,'Getresource');
EntryPoint:=GetProcAddress(DLLHandle,buffer);
If Entrypoint=nil then
result :=False
else begin
@Getres:=EntryPoint;
result:=true;
end;
end;
end;
Hope this helps.
Technical Details
Message-ID:
<36debf3b.24979569@news.online.no>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<2niD2.930$_n2.20928@carnaval.risq.qc.ca>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fd817,37896915cd1a101c X-Google-Attributes: gidfd817,public X-Google-Thread: f973d,37896915cd1a101c X-Google-Attributes: gidf973d,public X-Google-Thread: 105ac8,37896915cd1a101c X-Google-Attributes: gid105ac8,public X-Google-Thread: 1043cf,37896915cd1a101c X-Google-Attributes: gid1043cf,public X-Google-Thread: 10baee,37896915cd1a101c X-Google-Attributes: gid10baee,public X-Google-Thread: 108b0b,37896915cd1a101c X-Google-Attributes: gid108b0b,public From: engebakk@online.no (Tor Engebakken) Subject: Re: Passing a string to a DLL procedure with PChar Date: 1999/03/05 Message-ID: <36debf3b.24979569@news.online.no> X-Deja-AN: 451491027 References: <2niD2.930$_n2.20928@carnaval.risq.qc.ca> X-Complaints-To: abuse@online.no X-Trace: news1.online.no 920626307 130.67.21.154 (Fri, 05 Mar 1999 10:31:47 MET DST) Organization: Telenor Online Public Access Reply-To: engebakk@online.no NNTP-Posting-Date: Fri, 05 Mar 1999 10:31:47 MET DST Newsgroups: comp.lang.pascal.delphi.misc,alt.comp.borland-delphi,alt.comp.lang.borland-delphi,alt.lang.delphi,borland.public.delphi.objectpascal,cern.delphi.general