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 :)
Related
I used to hide my background app's console window until I needed it with
ShowWindow(console_hwnd,SW_HIDE);
My Windows got an update and switched to "Windows Terminal" as the default console app.
That line of code doesn't work anymore it just minimizes the console to taskbar. What is the proper way to fully hide Windows Terminal?
EDIT:
Please read the question carefully. It says HIDE the console window. NOT remove the console completely. I need to be able to show it again later. Thats the purpose of SW_HIDE and SW_SHOW that are no longer working with the new Windows Terminal.
If you want to show the console at any time, you could hide and show the console by changing the coordinates of the console.
int main() {
HWND consoleWindow = GetConsoleWindow();
SetWindowPos(consoleWindow, 0, -600, -600, 0, 0, SWP_NOSIZE | SWP_NOZORDER);
return 0;
}
EDIT:
According to this document, the second parameter to ShowWindow() might get ignored the first time it is called. Try calling it twice.
ShowWindow(hWnd, SW_HIDE);
ShowWindow(hWnd, SW_HIDE);
In addition, did you run this program as administrator? If so, I suggest you cancel it, since the window is in a higher privileged process, so your call will be ignored.
Hope it helps.
I am creating a Win32 application, I have followed some setup code for creating a Win32 window without the need for a WinMain which can be found here. The code builds just fine and if I run the application by opening it in a file browser it runs no problem. However, if I run it via the command line it only outputs the console logs and doesn't create a window. I assume that this is also why if I run it via VSCode's debugger it also doesn't launch a window. Has anyone come across this issue before and found a fix for it? It would be pretty had to debug a GUI application if you can't use the debugger and see it at the same time.
This bit of code contains a mistake:
STARTUPINFO si;
GetStartupInfo(&si);
int nCmdShow = si.wShowWindow;
You forgot to check si.dwFlags, as indicated in the documentation for wShowWindow:
If dwFlags specifies STARTF_USESHOWWINDOW, this member can be any of the values that can be specified in the nCmdShow parameter for the ShowWindow function, except for SW_SHOWDEFAULT. Otherwise, this member is ignored.
I have been trying to create a cmd.exe from a c++-program and then being able to execute commands from the program inside the cmd.exe, so I can view the results of the operation in the cmd.exe. CreateProcessA() is the function I used:
_STARTUPINFOA CMD_StartupInfo = {};
CMD_StartupInfo.cb = sizeof(_STARTUPINFOA);
_PROCESS_INFORMATION CMD_ProcessInfo = {};
CreateProcessA("C:\\Windows\\System32\\cmd.exe",
0,
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&CMD_StartupInfo,
&CMD_ProcessInfo);
The cmd.exe opened and it looked to be working. Then I thought I'd use PostMessage() to pass WM_KEYDOWN messages to the cmd.exe to execute a command. For this function I needed a window handle and it turns out that the process of getting a window handle from a process/thread ID which is obtained from the CreateProcessA() is f***ing complicated. You have to loop through all active windows with EnumWindows() that requires a callback function which needs to check whether or not the process ID of the current window is the same as the process ID of the process I created.
I did that and it turned out that none of the windows the function iterated over were my window.
So I presume that means that the thread of this process which I created has no windows but I can see a beautiful-looking cmd.exe right there. What is going on? Is this not considered a window? If not, how do I pass messages to the cmd.exe and if yes, how do I get the window handle?
Any help is appreciated (also ideas to do the entire cmd-thing differently).
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.
Hello fellow programmers, I have a problem with some console applications in a C++ program, and my objective is as follows.
Create first CMD window.
Execute a command. (system("print some error text");)
Create second CMD window.
Change system(...) focus to second CMD window.
Execute a command.
Change system(...) focus to first CMD window.
Execute a command.
The end goal of all this is to create a function that will be executed by a CMD application that will spawn another CMD window, execute a command on it, and then return focus to the original CMD window to continue executing other code. I do not need to keep track of the window, or be able to return to it. Just create new window, switch focus to it, execute a command, return focus to original window.
The first CMD window is created when the application is started.
Executing a command to this window with system(...); works fine.
I create a second CMD window with
HWND new_hWnd = NULL;
ShellExecute(new_hWnd, "open", "cmd.exe", NULL, NULL, SW_SHOW);
This is where I have problems, I have not been able to redirect system(...) to a different CMD window, and this is the part I need help with because if I can figure this out, then steps 5, 6 and 7 will be easy to complete.
I've tried researching this online and have come across some different examples using "pipes" but have not been able to recreate them, or understand them. Also, I noticed there is a
GetConsoleWindow();
function that returns a handle to the current CMD window, which to me kinda signals that there should be a way to switch between CMD windows by using handles, but since I have not switched focus to the other CMD window I can not call that function to get it's handle.
So, how do I make system(...) target different CMD windows with handles? If that is not possible, how can I implement this "pipe" system.
If the solution is the latter, please try to be as detailed and simple as possible with it because every example of it I have found online is really large and hard to read/understand.
If there is no easy way to implement "pipes" then please post or point me to the best(something that will help me understand how pipes work) example you can find and I will keep working with it till I figure it out. Thank you in advance!
You can create a new console for the new process by specifying the dwCreationFlags value CREATE_NEW_CONSOLE when calling CreateProcess.
See the documentation:
Process Creation Flags: https://msdn.microsoft.com/en-us/library/windows/desktop/ms684863(v=vs.85).aspx
CreateProcess function: https://msdn.microsoft.com/en-us/library/windows/desktop/ms682425(v=vs.85).aspx