Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi - show a console app from SW_HIDE state

I have a console application in Delphi, what i start from an other application this way:

FillChar(ExecInfo, SizeOf(ExecInfo), 0);
With ExecInfo Do Begin
  cbSize :=       SizeOf(ExecInfo);
  fMask :=        SEE_MASK_NOCLOSEPROCESS or SEE_MASK_NOASYNC;
  Wnd :=          GetActiveWindow();
  lpVerb :=       PChar('runas');
  lpFile :=       PChar(FsCurrentPath + 'Install\Install_Elevated.exe');
  lpDirectory :=  PChar(FNew.sBinDir);
  lpParameters := PChar(sl.DelimitedText);
  nShow :=        SW_HIDE
End;
ShellExecuteEx(@ExecInfo);

In some condition i would like to make it show itself (take in SW_SHOWNORMAL state). How can i do it?

This way it does not show:

ShowWindow(GetConsoleWindow, SW_SHOW);

Even not this way:

BringWindowToTop(GetConsoleWindow);
SetActiveWindow(GetConsoleWindow);
SetForegroundWindow(GetConsoleWindow);
ShowWindow(GetConsoleWindow, SW_SHOW)

But it shows itself this way:

MessageBox(GetConsoleWindow, PChar(IntToStr(GetConsoleWindow)), PChar(''), MB_SETFOREGROUND);
ShowWindow(GetConsoleWindow, SW_SHOW);

But of course i dont want this message box.

What is the problem?

like image 771
trob Avatar asked Feb 06 '23 00:02

trob


1 Answers

The shell passes the information you supply with SHELLEXECUTEINFO through CreateProcess() to the console application, which honors that information when you first try to show the console window.

The documentation for ShowWindow() says:

nCmdShow [in]
Type: int

Controls how the window is to be shown. This parameter is ignored the first time an application calls ShowWindow, if the program that launched the application provides a STARTUPINFO structure. Otherwise, the first time ShowWindow is called, the value should be the value obtained by the WinMain function in its nCmdShow parameter. In subsequent calls, this parameter can be one of the following values...

So, the first time you call ShowWindow, the SW_HIDE that was passed to ShellExecuteEx() takes effect. In the subsequent calls, the parameter you specify takes effect instead.

like image 141
Sertac Akyuz Avatar answered Feb 15 '23 18:02

Sertac Akyuz