I'm opening a process in C++ like so
FILE* pipe = _popen(ss.str().c_str(), "r");
This create a cmd window which goes to the forefront of the desktop. I want to prevent the cmd window from opening, and if I can't, a way to keep it minimized.
Tried solutions:
HWND hWnd = GetConsoleWindow();
ShowWindow(hWnd, SW_HIDE);
But that only works if you are calling that function in the process that holds the cmd window.
Instead of using _popen, use CreateProcess and the CREATE_NO_WINDOW flag.
Related
Is there a way to programmatically hide an application on windows? I want to achieve the same thing as the windows+D shortcut, but for a single application. I want to do this from within that application (application consists of several windows, one of those can't be moved, resized, closed or minimized by the user). Application is written in c++ and uses Qt for the UI.
to do so it's so easy:
1- retrieve the handle to that window:
HWND hChild = GetDlgItem(hWnd, ID_MYCHILD);
2- send to it SW_SHOW either using ShowWindow or via SendMessage:
ShowWindow(hChild, SW_HIDE); // hide
ShowWindow(hChild, SW_SHOW); // show
SendMessage(hChild, SW_HIDE, 0, 0); // hide
SendMessage(hChild, SW_SHOW, 0, 0); // show
if the window doesn't belong to your application then:
1 - retrieve the main window with:
HWND hWnd = GetForegroundWindow(void);
2- use the above to hide/show it
ShowWindow(HwndWindow, SW_MINIMIZE);
Here's the MSDN ShowWindow documentation.
In addition you may find EnumChildWindows useful for finding all these windows if their handles aren't readily available to you.
I want to close all the running instances of Notepad through my application.
I got the window handle using
FindWindow() API.
I got the handle of Window.
CloseWindow() API
is minimizing the notepad but I want to close all the instances of Notepad.
How can I achieve this?
By using FindWindow() you will get HWND
Use that HWND to get pid i.e process id.
HWND hWnd; // using findwindow you will get hWnd
DWORD pid;
TCHAR tcInput [MAX_PATH];
CString strName;
GetWindowThreadProcessId(hWnd,&pid);
//::GetWindowText(hWnd,tcInput,MAX_PATH);
//strName = (CString)tcInput;
//if(strName.MakeLower().Find( _T("untitle"))!=-1) //you can check windows title here
KillProcess(pid); // kill the process
When trying to execute cmd.exe with the SW_HIDE paramter:
ShellExecute(NULL, "open", "cmd.exe", NULL, NULL, SW_HIDE);
cmd.exe is actually executed hidden! I don't understand why this happened, does cmd.exe's console window considered to be a "normal" window that accepts the nCmdShow parameter, or does cmd.exe when executed sees that I passed SW_HIDE for the nCmdShow parameter and decides not to show the console window?
Note: I tried this code on Windows 7.
The implication is that cmd.exe simply uses the nCmdShow argument to WinMain (or that it uses SW_SHOWDEFAULT which means the same thing) when showing its window via ShowWindow, without checking or modifying the value first.
It's not documented that it will work and so you shouldn't rely on it, but it's a nice side effect that can be a good way to run batch scripts without a visible window appearing.
Of course if you actually launch an interactive cmd.exe in a hidden window it will be quite difficult to ever make it visible :)
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
I am calling this funtion from my main method in a C++ console application to hide the console window. It does it, but when I run the program the console window flashes for a split second before disappearing.
I want to run the program without this window flashing fully invisible. How to do this?
How would I go about starting a Run Dialog window and having my application input a command to start Steam?
I would like my application to open the standard windows run dialog pre-filled with input.
This is what I have tried:
HINSTANCE result;
result = ShellExecute(NULL, "open", "rundll32.exe", "steam://connect/192.69.96.168:27023", NULL, SW_SHOWDEFAULT);
I don't really understand the parameters for ShellExecute and would like some clarification.
UPDATE
I think I have gotten it to work:
HINSTANCE result;
result = ShellExecute( // WinExec is obsolete.
0, // hwnd
"open", // operation/verb.
"steam://connect/192.69.96.168:27023", // executable.
"", // parameters.
NULL, // directory
SW_SHOW); // how to be displayed
Have a look here, taken from this related question. This will open the run dialog.
Then if your process has the admin rights, you might be able to call SetWindowText on the handle of the input box of the dialog, which you'll find with FindWindowEx or similar.
I really think it's easier to create your own run dialog which looks like the windows one and calls ShellExecute when "ok" is clicked.