Solved

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

Posted on 2010-09-02
4
2,133 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

628 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