Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2010-09-02
4
Medium Priority
?
2,190 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 2000 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

670 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