Solved

Delphi 2007 to Delphi 2010 (String, AnsiString and more)

Posted on 2010-09-02
4
2,048 Views
Last Modified: 2012-05-10
Hello!

Me again =X
hehe

i like to do the things correctly and not just to silence the compiler =)

Using Delphi 2010, in a project made in Delphi 2007 a lot of bugs corrected...
Anyway, the typecast between String->PAnsiChar and back i have some doubts

example

function GetSomePath: String;
begin
  Result := 'C:\MyFolder\';
end;

WinExec(PAnsiChar(GetSomePath + 'SomeFile.exe'), SW_SHOWNORMAL);

Works but i got...
W1044 Suspicious typecast of string to PAnsiChar over PAnsiChar(GetSomePath)

To silence the compiler i can do

WinExec(PAnsiChar(AnsiString(GetSomePath) + 'SomeFile.exe'), SW_SHOWNORMAL);

But, i really don't know if its the right way... its not i think :S

Can somebody tell me the right way to TypeCast String, AnsiString, PChar, PAnsiChar with no data loss?

Best Regards,
Carlos
0
Comment
Question by:cebasso
  • 3
4 Comments
 
LVL 25

Expert Comment

by:epasquier
ID: 33585594
>> WinExec(PAnsiChar(AnsiString(GetSomePath) + 'SomeFile.exe'), SW_SHOWNORMAL);

That is what the compiler does for you when you wrote the previous code (the one with warnings)

If you cast explicitly, the advantage is that the compiler does not complain, but the code is less readable...
Matter of choice, really, but the result is exactly the same (and as you said, both work)
0
 
LVL 25

Accepted Solution

by:
epasquier earned 500 total points
ID: 33585651
the best solution would be to create a GetSomePathA function , and call this one with WinExec.
As we said before, force the 'A' version when some API NEED Ansi

function GetSomePathA: ANSIString;
begin
 Result := 'C:\MyFolder\'; // always ANSI (that string is a path!!)
end;

function GetSomePathW: WideString;
begin
 Result := GetSomePathA; // always convert here
end;

function GetSomePath:String;
begin
 Result := GetSomePathA; // convertion will occur only with Delphi >2009
end;

Call :
 WinExec(PAnsiChar(GetSomePathA + 'SomeFile.exe'), SW_SHOWNORMAL);

in other places that do have to use ANSI, use the generic version :
 lblPath.Caption:=GetSomePath;

Open in new window

0
 
LVL 25

Expert Comment

by:epasquier
ID: 33585663
One way to be sure if some extra conversion is done somewhere is to look at CPU code. Try with the examples above, with Delphi 2007 and 2009, you will see differences between GetSomePathW and GetSomePath, which codes are almost the same.
0
 
LVL 4

Author Closing Comment

by:cebasso
ID: 33585692
Ahh ok!

So the best choice is really use a A version of many func/proc's like we said before...
:D
Thank you again!

Cheers!
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question