Convert String to PChar and viceversa


Some API routines need PChar type info and that's driving me mad, as  I don't know how to convert string to PChar.

Example

var
  Directory: string;
begin
  Directory := Edit1.Text + '\*.*';
  FindFirstFile(Directory,fcFileName);
                   {don't know if this parameter is rigth, but it's not the point}.
end;

An error occurs because Directory must be PChar. How could I solve this problem?

I've looked up in Delphi help for 'string to PChar conversions' and there's actually a help file about that, but it doesn't help much. It only explains what a PChar is and what it's  functionality, but it doesn't mention anything about 'string to PChar conversions' as it's title says.
:( I'm sorry to say that Delphi4 help is not as good as I expected, but that's life...

My last question has to do with standards.. I mean, why do API's need PChar types and not string or integer types. I know they are null terminated strings, and I believe they are pointers, but that doesn't help me much.
There must be some interesting reason, and I would like to know why.

Thanks. Sorry for the points.
:)
pablocastaAsked:
Who is Participating?
 
rwilson032697Connect With a Mentor Commented:
Do this:

 FindFirstFile(PChar(Directory),fcFileName);

Windows API routines take pointers to chars for their arguments - thats just life! Delphi uses nice string types which makes life much easier. The cast is all you need, so its pretty simple...

Cheers,

Raymond.
0
 
pablocastaAuthor Commented:
thanks.
0
All Courses

From novice to tech pro — start learning today.