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?
I had the exact same problem. The
PAnsiChar
only points to the first character. I wrote the following function to handle the old functionality.I have no Delphi 2009 here, so I can't check it. But maybe you have to try:
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.
I think You are a bit off. Every Win32 API function has a unicode counterpart, if it is expecting a string. Try MapAndLoadW instead of MapAndLoad...
WideCharToMultiByte could help you.
Gamecat explicit conversion works. I'm explaining the problem in more detail below so that perhaps someone can point to a better solution.
I'm using the following function to retrieve the application compilation date:
MapAndLoad requires a PAnsiChar for the ImageName Parameter so I need to convert the unicode string. Is there any other alternative as to explicitly convert to AnsiString first?
Instead of using type
String
, useRawByteString
: