Solved

Strange strings in Delphi XE3

Posted on 2013-06-14
7
455 Views
Last Modified: 2013-06-15
Hi all.
I have this piece of code:

function TfrmMain.RunAsAdmin(hWnd: HWND; aFile,
  aParameters: AnsiString): Boolean;
var
  sei: TShellExecuteInfoA;
  ExitCode: DWord;
begin
  ShowMessage('aFile '+aFile);
  result := False;
  FillChar(sei, SizeOf(sei), 0);
  sei.cbSize := SizeOf(sei);
  sei.Wnd := hWnd;
  sei.fMask := SEE_MASK_NOCLOSEPROCESS{SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI};
//  sei.lpVerb := 'runas';
  sei.lpFile := PAnsiChar(aFile);
  ShowMessage('lpFile: '+sei.lpFile);
  sei.lpParameters := PAnsiChar(aParameters);
  sei.nShow := SW_SHOWNORMAL;
  if ShellExecuteEx(@sei) then
  begin
     repeat
       Application.ProcessMessages;
       GetExitCodeProcess(sei.hProcess, ExitCode) ;
     until (ExitCode <> STILL_ACTIVE) or
                        Application.Terminated;
     result := True;
  end
  else
    ShowMessage('FAILED');
//    RaiseLastOSError;
end;

Open in new window


Well: the two ShowMessages in lines 7 and 15 of the snippet above areexecuted correctly and they show the expected file name but after that I get the following image:
Error messageThen the FAILED message is shown.

Any idea?

Thanks in advance for any help
Marco
0
Comment
Question by:Marco Gasi
  • 5
7 Comments
 
LVL 6

Assisted Solution

by:House_of_Dexter
House_of_Dexter earned 150 total points
ID: 39249104
I don't have XE3...but aren't string Unicode now?  so this would be the cause of your problems...sei.lpFile := PAnsiChar(aFile);...convert your string to PChar...

I would bet there's a compiler directive to keep it AnsiString...

-Rick
0
 
LVL 25

Accepted Solution

by:
Sinisa Vuk earned 350 total points
ID: 39249145
You can try this:

1. go back to unicode strings (string instead of ansistring, pchar instead of pansichar), set fMask parameter as:
sei.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_UNICODE;

Open in new window



2. or use CreateProcess instead of ShellExecute:
http://delphidabbler.blogspot.com/2010/02/unicode-environment-blocks-and.html
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 39249568
Thanks to all for your replies.

@sinisav: hi. I'll try later to revert to unicode strings but I toke a look at CreateProcess in the link you provided above and I don't understand two things:

- what and how I have to pass as EnvBlock (the second parameter of the procedure ExecProgWithUnicodeEnv(const ProgName: string; EnvBlock: Pointer))?
- With ShellExecute I pass a file name and the file is executed or opened with the associated program; with Createprocess it seems I can execute a program but how can I open a specific file?

Last but not least: I'm porting a program from Delphi7 to Delphi XE3 and I migrated all string to ansiString but from what you say I think to have made wrong... Maybe I can open a new question for this, uh?

Thank you very mutch
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 30

Author Comment

by:Marco Gasi
ID: 39250002
I've just tried to come back to string type:

function TfrmMain.RunAsAdmin(hWnd: HWND; aFile,
  aParameters: String): Boolean;
var
  sei: TShellExecuteInfoA;
  ExitCode: DWord;
begin
  ShowMessage('aFile '+aFile);
  result := False;
  FillChar(sei, SizeOf(sei), 0);
  sei.cbSize := SizeOf(sei);
  sei.Wnd := hWnd;
  sei.fMask := SEE_MASK_NOCLOSEPROCESS{SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI};
//  sei.lpVerb := 'runas';
  sei.lpFile := PChar(aFile);
  ShowMessage('lpFile: '+sei.lpFile);
  sei.lpParameters := PChar(aParameters);
  sei.nShow := SW_SHOWNORMAL;
  if ShellExecuteEx(@sei) then
  begin
     repeat
       Application.ProcessMessages;
       GetExitCodeProcess(sei.hProcess, ExitCode) ;
     until (ExitCode <> STILL_ACTIVE) or
                        Application.Terminated;
     result := True;
  end
  else
    ShowMessage('FAILED');
//    RaiseLastOSError;
end;

Open in new window


but i receive this message:

[dcc32 Error] UMain.pas(917): E2010 Incompatible types: 'PAnsiChar' and 'PWideChar'

?
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 39250033
I get the same error message replacing

sei.fMask := SEE_MASK_NOCLOSEPROCESS;

with

sei.fMask := SEE_MASK_NOCLOSEPROCESS or SEE_MASK_UNICODE;
0
 
LVL 30

Author Comment

by:Marco Gasi
ID: 39250310
Ooops: replaced TShellExecuteInfoA with TShellExecuteInfo and the error has gone :)
Now I'm going to test if it works...
0
 
LVL 30

Author Closing Comment

by:Marco Gasi
ID: 39250326
Thanks to everyone.
I awarded more points to sinisav because his answers has been more detailed.

Cheers
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

759 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

20 Experts available now in Live!

Get 1:1 Help Now