Solved

Strange strings in Delphi XE3

Posted on 2013-06-14
7
481 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
Independent Software Vendors: 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month7 days, 11 hours left to enroll

632 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