Solved

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

Posted on 2010-09-02
4
2,024 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now