Board index » delphi » Passing String Lists to and from DLLs ?

Passing String Lists to and from DLLs ?

I have a DLL which I want to use to generate three lists of strings of
variable length.  

I have tried creating a TStringList in my main application and passing
it by reference to the DLL function.  When I try to add strings within
the DLL I get a GPF.  I presume this is because either the DLL is not
allowed to access the Add method or the Add method doesn't like the
address of the string passed by the DLL.

Can anyone suggest another method that works?  I don't really want to
write them out to disk (which is my only solution at the moment).  It
works but it seems messy.

Thanks,

Dave
--
Dave Ansell
Marconi Electronic Systems

 

Re:Passing String Lists to and from DLLs ?


Are you using the Shared Memory manager, as advised by the comment at the
top of a Library project?

Jim Green

Quote
Dave Ansell wrote:
> I have a DLL which I want to use to generate three lists of strings of
> variable length.

> I have tried creating a TStringList in my main application and passing
> it by reference to the DLL function.  When I try to add strings within
> the DLL I get a GPF.  I presume this is because either the DLL is not
> allowed to access the Add method or the Add method doesn't like the
> address of the string passed by the DLL.

> Can anyone suggest another method that works?  I don't really want to
> write them out to disk (which is my only solution at the moment).  It
> works but it seems messy.

> Thanks,

> Dave
> --
> Dave Ansell
> Marconi Electronic Systems

Re:Passing String Lists to and from DLLs ?


Quote
> Dave Ansell wrote:

> > I have a DLL which I want to use to generate three lists of strings of
> > variable length.

> > I have tried creating a TStringList in my main application and passing
> > it by reference to the DLL function.  When I try to add strings within
> > the DLL I get a GPF.  I presume this is because either the DLL is not
> > allowed to access the Add method or the Add method doesn't like the
> > address of the string passed by the DLL.

> > Can anyone suggest another method that works?  I don't really want to
> > write them out to disk (which is my only solution at the moment).  It
> > works but it seems messy.

I very quickly found that string-lists cannot be reliably passed to
DLLs.  In each and every case where I wanted to do so, I found that it
was necessary to change the protocol between the application and the DLL
to pass one value at a time through repeated calls.

Alternatively, a different data structure could be built and a pointer
to it sent.  The problem is the strings, which are reference-counted and
'god knows what else.'

Re:Passing String Lists to and from DLLs ?


Jim
  Sorry, I should have said its a D1 app.  It looks like the shared
memory manager fixes this problem in D3 as you say... but not available
in D1 :-(

Dave

Quote
Jim Green wrote:

> Are you using the Shared Memory manager, as advised by the comment at the
> top of a Library project?

> Jim Green

Re:Passing String Lists to and from DLLs ?


you could convert the stringlist to 1 string
using
stringlist.Commatext

then pass it as a pchar

then convert back
again using
stringlist.commatext

John C

Re:Passing String Lists to and from DLLs ?


Quote
Dave Ansell wrote:

> Jim
>   Sorry, I should have said its a D1 app.  It looks like the shared
> memory manager fixes this problem in D3 as you say... but not available
> in D1 :-(

String-lists definitely AREN'T reliable across DLL-boundaries.  What you
need to do is to set up your protocol between the two modules so that
they communicate the information one element at a time, e.g.

        DLLRoutine( DLL_STRINGLIST_CLEAR, '');
        for n := 0 to myList.Count - 1 do
          DLLRoutine ( DLL_STRINGLIST_ADD, myList[n] );

That works reliably.  If you need to retrieve the values back, I suggest
that you write a routine that is passed a PChar (array[0..255] of char)
allocated by the caller:

        myList.Clear;
        n := 0;
        while DLLRoutine ( DLL_STRINGLIST_GET, n, mybuffer ) do begin
          myList.Append(StrPas(myBuffer));
          Inc(n);
        end;

Since my ChimneySweep? product has to be cross-compatible between 16 and
32-bit mode I have become very familiar with these techniques.

Re:Passing String Lists to and from DLLs ?


You can share memory in delphi 1 too.
Check out "GlobalAlloc" with GMEM_SHARE option and "GlobalLock",
"GlobalUnlock", "GlobalFree" too.

On Mon, 11 Jan 1999 08:07:39 -0800, Dave Ansell

Quote
<dave.ans...@gecm.DONT.SPAM.ME.com> wrote:
>Jim
>  Sorry, I should have said its a D1 app.  It looks like the shared
>memory manager fixes this problem in D3 as you say... but not available
>in D1 :-(

>Dave

>Jim Green wrote:

>> Are you using the Shared Memory manager, as advised by the comment at the
>> top of a Library project?

>> Jim Green

Other Threads