Solved

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

760 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

22 Experts available now in Live!

Get 1:1 Help Now