Board index » delphi » Sharing ShortString in Delphi and C++

Sharing ShortString in Delphi and C++

Which data types do I need to use in order to share strings between a D3 app
and a C++ app?  The max. size of any string is about 25 chars.  The data is
being shared via a memory mapped file.

On the Delphi side, I have tried:
myMessage : String[25]; // means 1..25

and on the C++ side:
char myMessage[25];  // means 0..24

Once the D3 app has set the string, the C++ app can read the string but
since the ShortString[25] definition is based on an index of 1 (not 0 as in
C), the first char displayed by the C app is not the first char of the
string (as set in Delphi app) but rather the total number of chars in this
string.

Is there a better method for sharing limited strings between Delphi and C++?
Can the ShortString declaration be forced to use an index of zero instead of
1, hence solving the problem?  Have I used the wrong data types?

Thanks for any help.
--
Chris
ch...@formulate.clara.net

 

Re:Sharing ShortString in Delphi and C++


Quote
Chris Brett <ch...@formulate.clara.net> wrote in message

news:EQFg3.9203$dp1.218853@nnrp4.clara.net...

Quote
> Which data types do I need to use in order to share strings between a D3
app
> and a C++ app?  The max. size of any string is about 25 chars.  The data
is
> being shared via a memory mapped file.

> On the Delphi side, I have tried:
> myMessage : String[25]; // means 1..25

> and on the C++ side:
> char myMessage[25];  // means 0..24

Native Delphi strings are not null-terminated [1], so they're
not compatible with C strings. You could use an array, which
seems to be the most direct analogy:

(* Pascal *)
var
  myMessage : array[0..24] of Char;

/* C */
  char myMessage[25];

[1] they store the length of the string in the first byte, hence the
255 char limit, and the confusion.

Or, you can use Delphi's PChar type, which is compatible
with C-style strings, or you can explicitly convert your Pascal style
strings (look up "String Handling Routines - Null Terminated in the
help index).

HTH

--
Jeremy Collins
Kansai Business Systems
http://www.kansai.co.uk/

Re:Sharing ShortString in Delphi and C++


Im Artikel <EQFg3.9203$dp1.218...@nnrp4.clara.net>, "Chris Brett"
<ch...@formulate.clara.net> schreibt:

Quote
>Which data types do I need to use in order to share strings between a D3 app
>and a C++ app?

Delphi and C strings differ in at least the following points:

Delphi ShortString is an array of characters, where the first character (as
byte) indicates the length of the string, and the contents of the text string
start at the second byte. Null characters can occur in the string.

In C a string starts with the first character, has no character count, and ends
with a Null character (#0 in Delphi). Therefore a string terminates with the
first Null character, unless you provide special string handling methods,
different from the standard C string handling functions.

Depending on your Delphi version and knowledge of C and Delphi, you should try
to find compatible data types, or implement a class in C++ that can deal with
ShortString. Depending on your special situation, this may be more or less
difficult ;-)

DoDi

Other Threads