Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Strange strings in Delphi XE3

Posted on 2013-06-14
7
Medium Priority
?
495 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 600 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 28

Accepted Solution

by:
Sinisa Vuk earned 1400 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
Technology Partners: 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

971 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