Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / lazarus / 2011-08

Converting pchar to pcchar

6 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Alex du Plessis

Hi List,

Could anyone tell me what is the difference between a pchar and a pcchar? |And how does one create the one from the other.

I have a library that was written in c++ and need to enter a string parameter into one of the (crucial) functions. When I used h2pas to convert the c header file it replaced the parameter type with a pcchar on that function. As far as I can determine, the two types are not compatible - the type checking in the compiler throws an error and refuses to compile.

I could state the question differently as well. How do I get a string into a pcchar variable?

Regards
Alex du Plessis

Reply by email

Sven Barth
to Alex du Plessis
Alex du Plessis wrote 5 lines
Hi List, Could anyone tell me what is the difference between a pchar and a pcchar? |And how does one create the one from the other.

If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) you'll see that "ppchar" is defined as "^cchar" and "cchar" is defined as "cint8" which in turn is defined as "shortint". While SizeOf(ShortInt) = SizeOf(Char) is true ("Char" is the base of "PChar"), ShortInt=Char is not.

I personally would just cast one to the other:

=== source begin ===

var
   pcc: PCChar;
   pc: PChar;
begin
   pc := 'Hello World'; // I hope one can assign strings to PChars
directly O.o
   pcc := PChar(pc);
end;

=== source end ===

Show quoted text 9 lines
I have a library that was written in c++ and need to enter a string parameter into one of the (crucial) functions. When I used h2pas to convert the c header file it replaced the parameter type with a pcchar on that function. As far as I can determine, the two types are not compatible - the type checking in the compiler throws an error and refuses to compile. I could state the question differently as well. How do I get a string into a pcchar variable?

You first need to cast the String to PChar (so that the compiler's String->PChar magic can do its work) and then you can cast it to PCChar.

Regards,
Sven

PS: You might be better of posting such questions on fpc-pascal, as more compiler devs are there.

Reply by email

Alex du Plessis
to Sven Barth
Sven Barth wrote 52 lines
On 14.08.2011 18:45, Alex du Plessis wrote: > Hi List, > > Could anyone tell me what is the difference between a pchar and a > pcchar? |And how does one create the one from the other. > If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) you'll see that "ppchar" is defined as "^cchar" and "cchar" is defined as "cint8" which in turn is defined as "shortint". While SizeOf(ShortInt) = SizeOf(Char) is true ("Char" is the base of "PChar"), ShortInt=Char is not. I personally would just cast one to the other: === source begin === var pcc: PCChar; pc: PChar; begin pc := 'Hello World'; // I hope one can assign strings to PChars directly O.o pcc := PChar(pc); end; === source end === > I have a library that was written in c++ and need to enter a string > parameter into one of the (crucial) functions. When I used h2pas to > convert the c header file it replaced the parameter type with a pcchar > on that function. As far as I can determine, the two types are not > compatible - the type checking in the compiler throws an error and > refuses to compile. > > I could state the question differently as well. How do I get a string > into a pcchar variable? You first need to cast the String to PChar (so that the compiler's String->PChar magic can do its work) and then you can cast it to PCChar. Regards, Sven PS: You might be better of posting such questions on fpc-pascal, as more compiler devs are there. -- _______________________________________________ Lazarus mailing list http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus

Thanks Sven - that seems to have done the trick :)

Reply by email

Marco van de Voort
to Sven Barth: If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) you'll see that "ppchar"…
Sven Barth wrote
If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) you'll see that "ppchar" is defined as "^cchar" and "cchar" is defined as "cint8" which in turn is defined as "shortint". While SizeOf(ShortInt) = SizeOf(Char) is true ("Char" is the base of "PChar"), ShortInt=Char is not.

IIRC originally when I added ctypes, name:pcchar was meant to be the translationfrom "const char* name".

C const back then translated to nothing (since Pascal CONST was still by ref then), and it was an attempt to at least register the fact that the declaration had CONST in C.

This because some of the problems of "CONST" on
different architectures were already known by then.

Reply by email

Hans-Peter Diettrich
to Marco van de Voort
Show quoted text 9 lines
On Sun, Aug 14, 2011 at 08:30:20PM +0200, Sven Barth wrote: > If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) > you'll see that "ppchar" is defined as "^cchar" and "cchar" is > defined as "cint8" which in turn is defined as "shortint". While > SizeOf(ShortInt) = SizeOf(Char) is true ("Char" is the base of > "PChar"), ShortInt=Char is not. IIRC originally when I added ctypes, name:pcchar was meant to be the translation from "const char* name".

AFAIR Windows.h defined PCCHAR this way, and PTCHAR as platform specific, i.e. PCHAR on Win16 and PWCHAR on Win32. Even combinations of T and C modifiers had been defined, e.g. PTCCHAR.

The same names in lowercase may have different meanings in C (ctypes.h), but since Pascal identifiers are case insensitive... :-(

DoDi

Reply by email

Marco van de Voort
to Hans-Peter Diettrich
Hans-Peter Diettrich wrote 14 lines
Marco van de Voort schrieb: > On Sun, Aug 14, 2011 at 08:30:20PM +0200, Sven Barth wrote: >> If you look into the unit "ctypes" (located in $fpcdir/rtl/inc) >> you'll see that "ppchar" is defined as "^cchar" and "cchar" is >> defined as "cint8" which in turn is defined as "shortint". While >> SizeOf(ShortInt) = SizeOf(Char) is true ("Char" is the base of >> "PChar"), ShortInt=Char is not. > > IIRC originally when I added ctypes, name:pcchar was meant to be the > translation from "const char* name". AFAIR Windows.h defined PCCHAR this way, and PTCHAR as platform specific, i.e. PCHAR on Win16 and PWCHAR on Win32. Even combinations of T and C modifiers had been defined, e.g. PTCCHAR.

At that time, ctypes was still Unix only. It used to be the first part of what is now "ptypes.inc" in the rtl. Since more and more header units started using it (and some headers were also used on Windows) it was spun out as a separate unit.

So windows headers have nothing to do with it.

Reply by email