Board index » delphi » Null-terminated block of null-terminated strings

Null-terminated block of null-terminated strings

Hi!

Can anyone assist me in making a "null-terminated block of
null-terminated strings", ie what is the structure like?

After some fiddling, this worked:

var
 s: string[20];
 s2: string;
begin
 s2:=s[0]+char(0)+s[1]+char(0)+s[2]+char(0) ....  ;
 ...

I can't understand why I have to null-terminate strings that alreay
should have been null-terminated... There gotta be a better way?

- Bjoern

-----------------------------------------------------------------------
   Bjoern Stensrud -  Norwegian University of Science and Technology
 E-Mail: bjoer...@idt.ntnu.no - URL: http://www.idt.ntnu.no/~bjoernst/
-----------------------------------------------------------------------

 

Re:Null-terminated block of null-terminated strings


 Bjoern Stensrud wrote in article <33CBA596.6...@idt.ntnu.no>...

Quote
>Hi!

>Can anyone assist me in making a "null-terminated block of
>null-terminated strings", ie what is the structure like?

>After some fiddling, this worked:

>var
> s: string[20];
> s2: string;
>begin
> s2:=s[0]+char(0)+s[1]+char(0)+s[2]+char(0) ....  ;
> ...

>I can't understand why I have to null-terminate strings that alreay
>should have been null-terminated... There gotta be a better way?

Typically this is done with API calls where you want to pass the equivalent
of an array of strings back & forth. It avoids the necessity of using a
callback function to do exactly the same thing but with much more work.

Remember how strings are represented in C. You have a buffer that has
characters followed by a null character. Now, let's say you want to pass 5
of these strings to the DLL function. You could do something rather complex
involving the use of callback functions, or you could concatenate each of
these null-terminated strings together, ending the whole thing with another
null character. The DLL function could then parse through the buffer and
pick out each "substring" from the string you passed in.

The Delphi long string type does not have null termination outright (normal
manipulation of the long string type won't show you the final null
character, even though it's there). If you cast it as a PChar to pass to a
DLL, the null is present, as far as the DLL function is concerned.

So assuming you're using Delphi 2/3, here's what you can do...

var
                       s1: string;
s2: string;
s3: string;
begin
s1 = 'null';
s2 = 'test';
s3 := s1 + char(0) + s2 + char(0); // the body of the string is
'null\0test\0'
MyDLLFunction(PChar(s3)); // the DLL sees 'null\0test\0\0'
end;

Likewise, with functions that return a "null-terminated block of
null-terminated strings", you can parse through the list until you hit
\0\0, meaning you hit the end of the filled-in data in your string buffer.

Hope this helps,
Rob

------------------
Rob Powers
*...@registrysystems.com
Anti-Spam... replace *** with robert for email address
Opinions mine, not my employer's

Re:Null-terminated block of null-terminated strings


Quote
In article <33CBA596.6...@idt.ntnu.no> Bjoern Stensrud <bjoer...@idt.ntnu.no> writes:
>Can anyone assist me in making a "null-terminated block of
>null-terminated strings", ie what is the structure like?
>After some fiddling, this worked:
>var
> s: string[20];
> s2: string;
>begin
> s2:=s[0]+char(0)+s[1]+char(0)+s[2]+char(0) ....  ;
> ...
>I can't understand why I have to null-terminate strings that alreay
>should have been null-terminated... There gotta be a better way?

If you are using the Delphi "string" type, either in D1 or D2/D3, there is
logically =no= terminating null-byte in the string's value.  Never mind that
in D2/D3 there's always gonna be a zero-byte following the value; that null is
not part of the string's value itself.

This is why the value of S[0] does not include the terminating null byte.  
Logically it is not there.

The Delphi "string" type is not the same as the C/C++ "null-terminated array
of characters."

So if you want to create a string containing a bunch of values followed by
nulls, yes, you must insert those nulls yourself as you have done.

Re:Null-terminated block of null-terminated strings


        Hi, Bjoern Stensrud!

 On Tue, 15 Jul 1997 18:30:14 +0200 you wrote:

Quote
>Can anyone assist me in making a "null-terminated block of
>null-terminated strings", ie what is the structure like?

>After some fiddling, this worked:

>var
> s: string[20];
> s2: string;
>begin
> s2:=s[0]+char(0)+s[1]+char(0)+s[2]+char(0) ....  ;

>I can't understand why I have to null-terminate strings that alreay
>should have been null-terminated... There gotta be a better way?

I think the Delphi engine removes the #0 when concatenating strings.
Have a look: If you were a compiler, how would you interpret
s[0] + s[1] statement? You would think that a programmer wants to have
a string as a result => you have to remove the #0 character from the
end of s[0].

So, your method is quite right, IMHO.

Yours,
  Serge Sushko,
  sus...@aha.ru

Other Threads