- 1 year ago
How to hide ShellExecuteEx console window but not Application window?
When using ShellExecuteEx
to launch an application, you can hide the console window while keeping the application window visible by using the SW_HIDE
flag in the nShow
parameter of the SHELLEXECUTEINFO
structure.
Here's how you can achieve this in C++:
cpp#include <Windows.h>
#include <Shellapi.h>
int main()
{
SHELLEXECUTEINFO shExecInfo = { 0 };
shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
shExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
shExecInfo.lpFile = L"YourApplication.exe"; // Replace with the path to your application executable
shExecInfo.lpParameters = L""; // Optional: Add any command-line parameters here
shExecInfo.nShow = SW_HIDE; // Hide the console window
if (ShellExecuteEx(&shExecInfo))
{
// The application was launched successfully
WaitForSingleObject(shExecInfo.hProcess, INFINITE);
CloseHandle(shExecInfo.hProcess);
}
else
{
// Error handling for failed execution
}
return 0;
}
In the code above, we set the nShow
parameter of the SHELLEXECUTEINFO
structure to SW_HIDE
, which hides the console window when the application is launched. The WaitForSingleObject
function waits for the launched process to terminate, and CloseHandle
closes the handle to the process.
Please note that this method will only work if the application itself does not create a new console window or explicitly show a console window within its own code. If the application is designed to show a console window, you won't be able to hide it using ShellExecuteEx
. In such cases, you would need to modify the source code of the application to prevent it from showing the console window.