Board index » delphi » String and Pchar(Char)

String and Pchar(Char)

Hello.

How can I convert strings from C (not C++) to Delphi and viceversa? Where
can find examples about it?

I'm writing a DLL called by a program write in C++. This program have a
simple DLL with source code as example.

I'm a novice in programming :-).

I need translate a function like this

int WINAPI command(char string1[256], char string2[256])
        {
        char option1[256],option2[256];
        sscanf(string1,"%s %s",option1,option2);
        if(strcmp(option1,"Program name")==0)
                {
                sprintf(string2,"My program");
                return 1;
                }
        if(strcmp(option1,"get")==0)
                {
                if(strcmp(option2,"Version")==0)
                        {
                        sprintf(string2,"Version 3.14");
                        return 1;
                        }
                }
        }

in my Delphi function

function command(string1: PChar; string2: PChar): Integer;
var
        str,option1,option2: string;
        i: Integer;
begin  
        str:=string(str);
        i:=1;
        while str[i]<>' ' do begin
                option1[i]:=str[i];
                inc(i);
        end;
        inc(i);
        while str[i]<>' ' do begin
                option2[i]:=str[i];
                inc(i);
        end;
        if option1='Program name' then begin
                StrPCopy(string2,'My program');
                result:=1;
        end;
        if option1='get' then begin
                if option2='Version' then begin
                        StrPCopy(string2,'Version 3.14');
                        result:=1;
                end;            
        end;
end;

Thanks a lot.

 

Re:String and Pchar(Char)


Look up PChar in Delphi help. If you look around you will see specific
sections on converting from null terminated strings to delphi strings and so
forth. If you intend to pass string references from C to a dll written in
Delphi and back, then passing by PChar is the fine. Casting from Delphi
string to PChar is also fine provided you are aware of the hazard associated
with this. Namely, Delphi strings are garbage-collected regardless of any
PChar type references to them. Once again, its all in the help files.

As for the code you posted, well you have translated to Delphi, have you
tried to run it?  What was the problem?

Dave

Quote
Angel Galan <aga...@ci.ub.es> wrote in message

news:Xns91968C632E5B9agalanciubes@193.145.223.123...
Quote
> Hello.

> How can I convert strings from C (not C++) to Delphi and viceversa? Where
> can find examples about it?

> I'm writing a DLL called by a program write in C++. This program have a
> simple DLL with source code as example.

> I'm a novice in programming :-).

> I need translate a function like this

> int WINAPI command(char string1[256], char string2[256])
> {
> char option1[256],option2[256];
> sscanf(string1,"%s %s",option1,option2);
> if(strcmp(option1,"Program name")==0)
> {
> sprintf(string2,"My program");
> return 1;
> }
>     if(strcmp(option1,"get")==0)
> {
> if(strcmp(option2,"Version")==0)
> {
>     sprintf(string2,"Version 3.14");
> return 1;
> }
> }
>     }

> in my Delphi function

> function command(string1: PChar; string2: PChar): Integer;
> var
> str,option1,option2: string;
> i: Integer;
> begin
> str:=string(str);
> i:=1;
> while str[i]<>' ' do begin
> option1[i]:=str[i];
> inc(i);
> end;
> inc(i);
> while str[i]<>' ' do begin
> option2[i]:=str[i];
> inc(i);
> end;
> if option1='Program name' then begin
> StrPCopy(string2,'My program');
> result:=1;
> end;
> if option1='get' then begin
> if option2='Version' then begin
> StrPCopy(string2,'Version 3.14');
> result:=1;
> end;
> end;
> end;

> Thanks a lot.

Other Threads