Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to PAnsiChar in Delphi 2009

I'm converting my applications to Delphi 2009 and faced an intriguing issue with some calls that need to convert a string (wide) to AnsiString.

Here's an example to demonstrate the issue I'm having:

var   s: PAnsiChar;  ...  s := PAnsiChar(Application.ExeName); 

With Delphi 2007 and previous versions, s := PChar(Application.ExeName) would return the application exe path.

with Delphi 2009, s := PAnsiChar(Application.ExeName) returns only 'E'.

My guess is that's because I'm converting a unicode string to an ansi string but how can I convert it so that a PAnsiChar gets the full string?

like image 514
smartins Avatar asked Nov 12 '08 12:11

smartins


1 Answers

I have no Delphi 2009 here, so I can't check it. But maybe you have to try:

s := PAnsiChar(AnsiString(Application.ExeName)); 

As gabr already pointed, this is not a very good practice, and you will only use it if you are 100% sure. The string only contains characters that have a direct mapping to the ANSI range.

That's why you should get a warning because you are converting Unicode to ANSI.

like image 64
Toon Krijthe Avatar answered Sep 20 '22 12:09

Toon Krijthe