Board index » delphi » Q: I miss the first letter

Q: I miss the first letter

Help me please! (Borland Delphi)

I don't know why. 'Caption:=' after 'GetPrivateProfileString'
omits the first letter.

    procedure TmdiMain.FormCreate(Sender: TObject);
    var
        szCaption: string[64];
        ofstr: TOfStruct;
    begin
----->      GetPrivateProfileString('Configuration', 'Book Store',
        '', @szCaption, 64, 'c:\books\books.ini');
----->      Caption := szCaption;
    end;

'c:\books\books.ini' is as follows:

    [Configuration]
    Book Store=ABC Bookstore

But I only see 'BC Bookstore' on the caption bar. Where is 'A'?
But when I use functions using @szCaption I get 'A' again.
For example:

    GetPrivateProfileString('Configuration', 'Book Store',
        '', @szCaption, 64, 'c:\books\books.ini');
    Application.MessageBox(@szCaption, 'message box', mb_ok);

gives me the full message:  ABC Bookstore

What should I do?
Thanks in advance.

Chirok Han from Seoul

 

Re:Q: I miss the first letter


Quote
Han Chi Rok wrote:
> I don't know why. 'Caption:=' after 'GetPrivateProfileString'
> omits the first letter.

>     procedure TmdiMain.FormCreate(Sender: TObject);
>     var
>         szCaption: string[64];
>         ofstr: TOfStruct;
>     begin
> ----->      GetPrivateProfileString('Configuration', 'Book Store',
>         '', @szCaption, 64, 'c:\books\books.ini');
> ----->      Caption := szCaption;
>     end;

> But I only see 'BC Bookstore' on the caption bar. Where is 'A'?
> But when I use functions using @szCaption I get 'A' again.
> For example:

>     GetPrivateProfileString('Configuration', 'Book Store',
>         '', @szCaption, 64, 'c:\books\books.ini');
>     Application.MessageBox(@szCaption, 'message box', mb_ok);
> gives me the full message:  ABC Bookstore

The problem is the mixing of pascal strings, which use the first byte to store the string
length, with C-type null terminated strings, which store the string beginning at the first
location of the buffer, and tack on a NULL to indicate the end.  If you define szCaption as

szCaption : ARRAY[0..64] OF CHAR;

you should have no problems.  By the way, the second call to MessageBox works because this
routine probably assumes that the first character in the Caption is relaly the first
character, and voila! the A reappears.

Hope this helps.

Cheers  p. rowntree/ universite de sherbrooke

Re:Q: I miss the first letter


Quote
Han Chi Rok wrote:
>     procedure TmdiMain.FormCreate(Sender: TObject);
>     var
>         szCaption: string[64];
>         ofstr: TOfStruct;
>     begin
> ----->      GetPrivateProfileString('Configuration', 'Book Store',
>         '', @szCaption, 64, 'c:\books\books.ini');
> ----->      Caption := szCaption;
>     end;

GetPrivateProfileString assumes a null-terminated string, whereas you
supply a Pascal <string>, where the first byte is used as count of
characters.  So the "A" in your caption is interpreted as "this string
contains 65 characters".  When you pass it to MessageBox, it is
interpreted by MessageBox as a null-terminated string, and is
displayed correctly.  Simply use <array [0..80] of char> instead of
<string[64]>, then everything should be fine.

Hope this helps,

- Alf

Other Threads