Search code examples
stringdelphidelphi-7

Cmd String to PAnsiChar in delphi


I am relatively new to Delphi and I want to make a quick application the uses the ShellExecute command.

I want to use string values in edit boxes to add to the command line for doing the processing job outside of the application.

Everything works fine, but I get the error :

"Incompatible types: String and PAnsiChar"

I have tried to convert it using: Variable := PAnsiChar(AnsiString(editbox.Text), but to no avail.

Can anyone assist me with this problem please.


Solution

  • In Delphi 7, it's a simple typecast to PChar, which is already a PAnsiChar:

    PChar(YourStringVariable);
    

    or

    PChar('Some text here');  // Cast not needed; demonstration only
    PChar('C:\' + AFileName); // Cast needed because of variable use
    

    Using it with ShellExecute:

    AFile := 'C:\MyDir\Readme.txt';
    Res :=   ShellExecute(0, 'open', PChar(AFile),
                          nil, nil, SW_NORMAL )