Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: PChar to PAnsiChar



Permlink Replies: 3 - Last Post: Jun 10, 2015 12:33 PM Last Post By: Brian Hamilton ...
Hubert Anemaat

Posts: 9
Registered: 9/26/02
PChar to PAnsiChar
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 10, 2015 1:05 AM
Hello,

I have the following code in Delphi 2007:

WinExec( PChar( 'Notepad ' + TextFile), SW_SHOW);

In Delphi XE8 I get the hint PAnsiChar.
So I changed it to:

WinExec( PAnsiChar( 'Notepad ' + TextFile), SW_SHOW);

But then the compiler gives the warning: W1044 suspicious typecast of string to PAnsiChar.

How can this be solved ?

Thanks,
Hubert
Lajos Juhasz

Posts: 801
Registered: 3/14/14
Re: PChar to PAnsiChar
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 10, 2015 1:29 AM   in response to: Hubert Anemaat in response to: Hubert Anemaat
Hubert Anemaat wrote:

WinExec( PAnsiChar( 'Notepad ' + TextFile), SW_SHOW);

But then the compiler gives the warning: +W1044 suspicious typecast
of string to PAnsiChar+.

How can this be solved ?

You can find a goot hint on MSDN
(https://msdn.microsoft.com/en-us/library/windows/desktop/ms687393(v=vs.85).aspx)

*Note*  This function is provided only for compatibility with 16-bit
Windows. Applications should use the CreateProcess function.

Simply don't use it it's not recommended to be used even by Microsoft.

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: PChar to PAnsiChar
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 10, 2015 10:03 AM   in response to: Hubert Anemaat in response to: Hubert Anemaat
Hubert wrote:

I have the following code in Delphi 2007:

WinExec() has been deprecated since 32bit Windows was first introduced.
You need to use ShellExecute/Ex() or CreateProcess() instead. In this case,
you should use ShellExecute() so that you can open the text file in the user's
default text app instead of forcing Notepad:

ShellExecute(0, nil, PChar(TextFile), nil, nil, SW_SHOW);


In Delphi XE8 I get the hint PAnsiChar.

That is because WinExec() does not support Unicode strings at all. ShellExecute/Ex()
and CreateProcess() do.

So I changed it to:
WinExec( PAnsiChar( 'Notepad ' + TextFile), SW_SHOW);

You have to use a temp AnsiString in order to cast to PAnsiChar:

WinExec(PAnsiChar(AnsiString('Notepad ' + TextFile)), SW_SHOW);


But then the compiler gives the warning: +W1044 suspicious typecast
of string to PAnsiChar+.

That is correct, because a (Unicode)String cannot be cast to a PAnsiChar,
only to a P(Wide)Char.

How can this be solved ?

Stop using an outdated API.

--
Remy Lebeau (TeamB)
Brian Hamilton ...

Posts: 556
Registered: 10/14/04
Re: PChar to PAnsiChar
Click to report abuse...   Click to reply to this thread Reply
  Posted: Jun 10, 2015 12:33 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
yes, the main thing, is to convert pchar to PAnsiChar(AnsiString(
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02