C++ Win 32 API: Problem With Passing window SW_HIDE - c++

I am attempting to hide a 3rd part window on bootup of our computers. I am using the following code.
#include<windows.h>
#include <stdio.h>
int main() {
char windowName[500];
HWND window = FindWindow("WindowClassAsReportedByWindowSpy++", NULL);
//GetWindowText(window, windowName, 63);
ShowWindow(firefox,SW_HIDE);
getchar();
return 0;
}
The only problem is the window will not hide. Any ideas on why this isn't working /how I could accomplish this differently.

Most likely your program calls FindWindow before the target window is created, and so doesn't find it.
You'll need to sleep and retry the find.

You probably want to do sanity checks to make sure FindWindow is not returning NULL. Even better, call FindWindow in a loop until it doesn't return NULL.
#include <windows.h>
#include <stdio.h>
static const wchar_t g_cszFirefoxClass[] = L"firefox";
int __cdecl wmain(__in int argc, __in_ecount_z_opt(argc) wchar_t* _wargv[], __in_z_opt __wenviron[])
{
UNREFERENCED_PARAMETER(argc);
UNREFERENCED_PARAMETER(_wargv);
UNREFERENCED_PARAMETER(__wenviron);
HWND hWnd;
do {
hWnd = FindWindow(g_cszFirefoxClass, NULL);
Sleep(100);
} while (hWnd == NULL);
wprintf(L"[-] Firefox found! [HWND = 0x%X]\n", hWnd);
if (ShowWindow(hWnd, SW_HIDE))
{
wprintf(L"[-] Successfully hid Firefox window!\n");
return EXIT_SUCCESS;
}
else
{
fwprintf(stderr, L"[x] Failed to hide Firefox window..\n");
return EXIT_FAILURE;
}
}

Related

Winapi how to hide/show a third-party program window?

I'm trying to hide/show a third-party program (e.g. Discord):
#include <windows.h>
void show_hide(HWND window)
{
if (IsWindowVisible(window) == TRUE) {
ShowWindow(window, SW_HIDE);
} else {
ShowWindow(window, SW_SHOW);
}
}
BOOL CALLBACK enum_windows_cb(HWND window, const LPARAM lParam)
{
const char* target = "Discord";
char title[255];
if (GetWindowText(window, title, sizeof(title))) {
if (strcmpi(title, target) == 0) {
show_hide(window);
return FALSE;
}
}
return TRUE;
}
int main(int argc, char *argv[])
{
EnumWindows(&enum_windows_cb, 0);
return 0;
}
It's work fine, but if I hide a third-party program by click on close button, my program shows frozen window, until I press the tray icon.
Video demonstration:
https://www.youtube.com/watch?v=P-_Fi-Dew1w
I also wrote message log through microsoft spy++ https://pastebin.com/YwGt88yh
It recorded three actions: click on close button hides program, click on tray icon shows program, and my program tries to show window.
I also tried to add these functions one by one after ShowWindow:
SetForegroundWindow(window);
SendMessage(window, WM_SYSCOMMAND, SC_RESTORE, 0);
UpdateWindow(window);
RedrawWindow(window, 0, nil, RDW_INVALIDATE or RDW_ALLCHILDREN or RDW_UPDATENOW or RDW_FRAME);
SendMessage(window, WM_ACTIVATEAPP, TRUE, GetWindowThreadProcessId(window, 0));
But it did not help. What did I do wrong?
I use windows 10.

c++ beginning to background

I make the bakcground running program.
this program is just simple [05:00 - reboot]..
I want hide the console windows..
Start from the beginning to background the console window
help.
#include "stdafx.h"
#include "stdio.h"
#include "windows.h"
#include "time.h"
#include "shellapi.h"
using namespace std;
int main()
{
while (true) {
time_t timer = time(NULL);
struct tm t;
localtime_s(&t, &timer);
int count_1 = 0;
if (t.tm_hour == 5) {
if (t.tm_min == 00) {
if (count_1 == 0) {
count_1 = 1;
WCHAR path[260];
GetModuleFileName(NULL, path, 260);
HWND console = FindWindow(L"ConsoleWindowClass", path);
if (IsWindow(console))
ShowWindow(console, SW_HIDE); // hides the window
system("shutdown -r");
if (IsWindow(console))
ShowWindow(console, SW_SHOW); // shows the window
}
}
}
}
}
Usually you would use the WinMain entry point instead of main.
But you need to tell your compiler that you're using WinMain which is compiler dependent.
By the way, there is a simpler way to hide the console window.
ShowWindow(GetConsoleWindow(), SW_HIDE);
But be aware that using this method the console is still being created and you might see it for a fraction of a second.

Show a list of applications like alt-tab in Win7

I am trying to print a list of running applications like alt-tab would give me. Here are what I have done so far:
1.In the beginning I tried EnumWindows, but I got hundreds of entries.
2.I found some similar questions and they led me to the blog of Raymond Chen.
http://blogs.msdn.com/b/oldnewthing/archive/2007/10/08/5351207.aspx
However it still shows more than 100 windows(window_num1 being 158 and window_num2 being 329), while alt-tab would give me only 4. What did I do wrong? Here is my code:
#include <windows.h>
#include <tchar.h>
#include <iostream>
using namespace std;
#pragma comment(lib, "user32.lib")
HWND windowHandle;
int window_num1=0;
int window_num2=0;
BOOL IsAltTabWindow(HWND hwnd)
{
if (hwnd == GetShellWindow()) //Desktop
return false;
// Start at the root owner
HWND hwndWalk = GetAncestor(hwnd, GA_ROOTOWNER);
// See if we are the last active visible popup
HWND hwndTry;
while ((hwndTry = GetLastActivePopup(hwndWalk)) != hwndTry)
{
if (IsWindowVisible(hwndTry))
break;
hwndWalk = hwndTry;
}
return hwndWalk == hwnd;
}
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
TCHAR title[500];
ZeroMemory(title, sizeof(title));
//string strTitle;
GetWindowText(hWnd, title, sizeof(title)/sizeof(title[0]));
if (IsAltTabWindow(hWnd))
{
_tprintf(_T("Value is %s\n"), title);
window_num1++;
}
window_num2++;
//strTitle += title; // Convert to std::string
if(_tcsstr(title, _T("Excel")))
{
windowHandle = hWnd;
return FALSE;
}
return TRUE;
}
void MyFunc(void) //(called by main)
{
EnumWindows(MyEnumProc, 0);
}
int main()
{
MyFunc();
cout<<endl<<window_num1<<endl<<window_num2;
return 0;
}
You failure is, that you should walk only visible windows... read the blog again.
For each visible window, walk up its owner chain until you find
the root owner. Then walk back down the visible last active popup
chain until you find a visible window. If you're back to where you're
started, then put the window in the Alt+↹Tab list.
Your code walks over every window!
Just use IsWindowVisible
BOOL CALLBACK MyEnumProc(HWND hWnd, LPARAM lParam)
{
TCHAR title[256] = {0,};
if (IsWindowVisible(hWnd) && GetWindowTextLength(hWnd) > 0)
{
window_num1++;
GetWindowText(hWnd, title, _countof(title));
_tprintf(_T("Value is %d, %s\n"), window_num1, title);
}
return TRUE;
}

Browse For Folder dialog window handle C++

How to get the handle HWND of the dialog which user open when clicking on a button.
I'm using Spy++ to find the window class and tittle, but it says that no such window is found. And how then to get the handle of that dialog in C++ using Win API ?
I hope that I will be able to do that using simple functions as FindWindow, GetParent, any WIN APi function. I do not like to inject something or load DLL. Thanks
UPDATE:
the folder browser dialog is opened by other program. I want to get it's handle from different program , my program. Thanks.
The closest to want i need is for now the function WindowFromPoint
Accessibility will let you capture window creation events from other processes without DLL injection. You can modify the example to accommodate for the browsing window specifically. Here's an example I made previously to test that is based on the one from the article. Modify it however you wish:
#include <iostream>
#include <windows.h>
void CALLBACK proc(HWINEVENTHOOK hook, DWORD event, HWND hwnd, LONG obj, LONG child, DWORD thr, DWORD time) {
if (hwnd && obj == OBJID_WINDOW && child == CHILDID_SELF) {
switch (event) {
case EVENT_OBJECT_CREATE: {
std::cout << "Window created!\n";
break;
}
case EVENT_OBJECT_DESTROY: {
std::cout << "Window destroyed!\n";
break;
}
}
}
}
int main() {
HWINEVENTHOOK hook = SetWinEventHook(EVENT_OBJECT_CREATE, EVENT_OBJECT_DESTROY, nullptr, proc, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (hook) {
UnhookWinEvent(hook);
}
}

How come FindWindow finds a window that EnumChildWindows doesn't?

I'm looking for a window that its class name is "CLIPBRDWNDCLASS" (it can be found in office apps and other applications).
If I use FindWindow or FindWindowEx I find the first HWND that has this class, but I want all the windows with that class, so I decided to use recursive EnumChildWindows to enumerate all windows and find the window I want:
//-------------------------------------------------------------------------------
BOOL CALLBACK enum_wnd_proc(HWND h, LPARAM lp)
{
char cls[1024] = {0};
::GetClassNameA(h, cls, 1024);
if(std::string(cls) == "CLIPBRDWNDCLASS")
{
// match!
}
::EnumChildWindows(h, enum_wnd_proc, NULL);
return TRUE;
}
//-------------------------------------------------------------------------------
int _tmain(int argc, _TCHAR* argv[])
{
::EnumWindows(enum_wnd_proc, NULL);
return 0;
}
//-------------------------------------------------------------------------------
The this is that this window does not return by the EnumWindows, only by FindWindow.
Does anyone can tell why it doesn't work ???
The reason EnumWindows doesn't work is that the window you are looking for is a message only window.
FindWindowEx can find them in two cases:
If both hwndParent and hwndChildAfter are NULL.
If you specify 'HWND_MESSAGE' as your parent window.
This code will find all the relevant windows for you (a modified version of a solution from here):
HWND hWindow = FindWindowExA(HWND_MESSAGE, NULL, "CLIPBRDWNDCLASS", NULL);
while (hWindow )
{
// Do something here with window...
// Find next window
hWindow = FindWindowExA(HWND_MESSAGE, hWindow , "CLIPBRDWNDCLASS", NULL);
}
Also note that unlike what's written in the above link, GetParent() for message only windows does not return HWND_MESSAGE (at least not for my tests).
My old easy way to ENUMERATE all message-only windows:
EnumChildWindows(GetAncestor(FindWindowEx(HWND_MESSAGE,0,0,0),GA_PARENT),addr EnumChildProc,0)
// GetAncestor(FindWindowEx(HWND_MESSAGE,0,0,0),GA_PARENT) = "GetMessageWindow" (class "Message")
// GetAncestor(FindWindowEx(HWND_DESKTOP,0,0,0),GA_PARENT) = GetDesktopWindow (class "#32769")