Running C++ program invisible - c++

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?

Related

Hiding the new Windows Terminal

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.

C++ system tray only program

After checking Microsoft's documentation on system tray icons (that I could find):
Shell_NotifyIconA function
NOTIFYICONDATAA structure
I've noticed that a window handle (HWND) is REQUIRED. This is very bad for what I'm trying to accomplish, as I'm looking to create a program that only reacts to the system tray: it doesn't "minimize" the window to the tray, it just uses notifications (clicking/right clicking on the icon) to interact.
How would I go about doing this?
The Windows 7 SDK contains an example called NotificationIcon. This example contains a line
ShowWindow(hwnd, nCmdShow);
in its wWinMain function. The effect of this call is that you see a program window.
Just change this line to
ShowWindow(hwnd, SW_HIDE);
to hide the program window and only display the icon in the system tray. As others have pointed out the program needs a program window, even if it is not visible.

Restore a minimized window of another application (C++ WinAPI)

I'm working on a C++ program that which launches a .NET Winforms application.
If the app is already running, I want to restore the window instead. I grab the .NET app's window handle and use SetForegroundWindow() to bring to to the front.
This works except when the application is minimized.
I've tried combinations of the following:
ShowWindow(windowHandle, SW_SHOW);
ShowWindow(windowHandle, SW_RESTORE);
and
SendMessage(windowHandle, WM_SYSCOMMAND, SC_RESTORE, 0);
However, when this code is executed, the event becomes stuck. In the tray I see ".NET-BroadcastEventWindow.2.0.0.0.378734a.0" come up as a second window in the tray along with my .NET app, and the app is never restored properly.
This only happens when I try to send a SW/SC_RESTORE or SHOW event. SetForegroundWindow() has no issues.
Has anyone else seen this problem before?
I've had similar problems in the past that I've solved using Get/SetWindowPlacement():
// Ensure that the given window is not minimized.
// If it is minimized, restore it to its normal state.
void EnsureNotMinimized(HWND hWnd)
{
WINDOWPLACEMENT placement;
placement.length = sizeof(placement);
if(!GetWindowPlacement(hWnd, &placement))
return;
BOOL minimized = (placement.showCmd & SW_SHOWMINIMIZED) != 0;
if(!minimized)
return;
placement.showCmd = SW_SHOWNORMAL;
SetWindowPlacement(hWnd, &placement);
}
However, I've only used this for windows that belong to my own application. I don't know if security would allow it to be used on outsiders.

Debugging "crashed" top most window

I have application that must run in Top most window. It works great till it crashes. When it crashes window becomes whiteis and windows crash window shows asking if you want to debug it.
In that moment I would like to use that option to debug that application. But I can't see anything because of that crashed topmost window.
I can't run application without topmost window. I can't attach debugger to application while it is running. My only possibility is to attach debugger after crash. But then I can't see anything.
Is there any way to "hide" crashed topmost window?
UPDATE:
I'm using WinApi.
You can hide a window using ShowWindow(HWND,int):
If you have the handle of the window:
ShowWindow(hwnd, SW_HIDE);
If you only know the title:
HWND hwnd = FindWindowA(NULL,"Title");
ShowWindow(hwnd, SW_HIDE);
So when you want to do something but the crashed window is in the way, run these commands to hide it.

Win32 programming hiding console window

I'm learning C++ and I made a new program. I deleted some of my code and now my console window is not hidden. Is there a way to make it hide on startup without them seeing it?
If you're writing a console program and you want to disconnect your program from the console it started with, then call FreeConsole. Ultimately, you probably won't be satisfied with what that function really does, but that's the literal answer to the question you asked.
If you're writing a program that you never want to have a console in the first place, then configure your project so that it is not a console program. "Consoleness" is a property of the EXE file. The OS reads that setting and decides whether to allocate a console for your program before any of your code ever runs, so you can't control it within the program. Sometimes a non-console program is called a "GUI program," so you might look for a choice between "console" and "GUI" in the configuration options of your development environment. Setting it to GUI doesn't require that you have any user interface at all, though. The setting merely controls whether your program starts with a console.
If you're trying to write a program that can sometimes have a console and sometimes not, then please see an earlier question, Can one executable be both a console and GUI app?
Assuming you're on windows, configure your linker to make a gui-program, not a console program.
VS: Look in Linker ptions on project properties
LINK: add /SUBSYSTEM:WINDOWS
MinGW: -mwindows
#include <windows.h>
#include <iostream>
using namespace std;
void Stealth()
{
HWND Stealth;
AllocConsole();
Stealth = FindWindowA("ConsoleWindowClass", NULL);
ShowWindow(Stealth,0);
}
int main()
{
cout<<"this sentence is visible\n";
Stealth(); //to hide console window
cout<<"this sentence is not visible\n";
system("PAUSE");
return EXIT_SUCCESS;
}
I used to use ShowWindow (GetConsoleWindow(), SW_HIDE); in such case, however if you no need console, so don't create console app project.
As already said, starting the application with console or not is set in the exe. Using gnu compiler the option is -mwindows for no console, for example
g++ -mwindows winapp.c
it seems that the method
#define _WIN32_WINNT 0x0500
#include <wincon.h>
....
case WM_CREATE :
ShowWindow (GetConsoleWindow(), SW_HIDE);
close all parent consoles as well, so if you launch the winapp.exe from a
command line console this will be closed as well!
To literally hide/show the console window on demand, you could use the following functions:
It's possible to hide/show the console by using ShowWindow. GetConsoleWindow retrieves the window handle used by the console.
IsWindowVisible can be used to checked if a window (in that case the console) is visible or not.
#include <Windows.h>
void HideConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
}
void ShowConsole()
{
::ShowWindow(::GetConsoleWindow(), SW_SHOW);
}
bool IsConsoleVisible()
{
return (::IsWindowVisible(::GetConsoleWindow()) != FALSE);
}
You can create your window minimized. Or paint it outside the visible screen.
But you could also have messed with the window creation flags. If you really messed things up. It is often better to start a new window. (Or restore from a previous version, or the backup).
You can try this
#include <windows.h>
int main() {
::ShowWindow(::GetConsoleWindow(), SW_HIDE);
MessageBox(NULL,"The console Window has been hidden.","Console Hidden",MB_ICONINFORMATION);
return 0;
}
It is part of the win32 API, which you can include using "#include "
https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-showwindow
The first argument tells the program to get the console window that is currently running the program. The second argument passes down the instruction for what you want to do with the window. "SW_HIDE" hides the window, while "SW_SHOW" shows the window.