Solved

Strange strings in Delphi XE3

Posted on 2013-06-14
7
477 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
[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
  • 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 27

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 31

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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 31

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 31

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 31

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 31

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

752 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