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

x
?
Solved

Strange strings in Delphi XE3

Posted on 2013-06-14
7
Medium Priority
?
489 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 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 27

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
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