FindWindow() doesn't find my window [C++] - hwnd

This is not a complex question. I'm having trouble finding the handle that belongs to iTunes. But although iTunes is running in the background, it keeps telling me it can't find the window. So I went on checking whether I miss typed the window name, but spy++ pointed out to me that I was using the correct window name and class name (see below). I'm sure it's a small mistake but I can't seem to find it. Does anyone have an insight? Thanks in advance.
HWND hwnd;
hwnd = FindWindow((LPCWSTR)"iTunes",(LPCWSTR)"iTunes");
if (hwnd != 0){
cout << "WINDOW FOUND" << endl;
} else {
cout << "WINDOW NOT FOUND" << endl;
cout << hwnd << endl;
}

You are using ANSI strings with what appears to be the Unicode version of FindWindow.
Many Win32 functions are actually a pair of function and a macro. For example, FindWindow is defined approximately like this:
HWND WINAPI FindWindowA(LPCSTR lpClassName, LPCSTR lpWindowName);
HWND WINAPI FindWindowW(LPCWSTR lpClassName, LPCWSTR lpWindowName);
#if (UNICODE)
# define FindWindow FindWindowW
#else
# define FindWindow FindWindowA
#endif
Try explicitely calling FindWindowA or using wide strings like this:
HWND hwnd = FindWindow(L"iTunes", L"iTunes");

Related

How to use run-time dll injecting to write into a Running and Temp notepad/txt file

Basically I created a simple Dll injection code with a basic Dll that shows a message box and my question is how can I now use the Dll file to make it write text into the Notepad while its running and it hasn't been saved/is temporary?
Is there a way to find a path to the file?
(i don't think so because its still writing into the ram and is untitled so doesn't have a save on any drives)
Or is there a stream i can write into?
Like Timo said in the comment, you can get the handle to the Notepad's Window, and then use it to write text into it, and it even wouldn't require from you to inject your DLL into the Notepad Process.
In Windows, every window is receiving messages from the Operating System (or from other programs, like what you're going to try out), those messages tell the window what action was made, for instance - Pressing down a key, or pressing a mouse button. That way, the program controlling the Window will know what kind of action it's supposed to do.
You can use the Spy++ Tool (spyxx.exe, comes builtin in every Visual Studio) to track what Windows does when you press a key down the keyboard into the Notepad's Window.
You can read further about the topic of window messages here:
https://learn.microsoft.com/en-us/windows/win32/learnwin32/window-messages
And working with Spy++ here:
https://learn.microsoft.com/en-us/visualstudio/debugger/introducing-spy-increment?view=vs-2019
Opening Spy++, we can see all the Windows in the our session, and we look for the Notepad's:
how it looks
Small POC I made for searching the appropriate Window, according to what found by Spy++ (forgive me for the globals, and make sure you are aware of what I did here):
HWND notepad_window;
HWND notepad_edit;
BOOL CALLBACK GetEditWindow(HWND hWnd, LPARAM lParam)
{
wchar_t window_text[MAX_PATH] = { 0 };
if (0 != GetClassNameW(hWnd, window_text, MAX_PATH))
{
std::wstring text(window_text);
if (std::wstring::npos != text.find(L"Edit"))
{
std::wcout << "Found it" << text << std::endl;
notepad_edit = hWnd;
return false;
}
}
return true;
}
BOOL CALLBACK GetNotepadWindow(HWND hWnd, LPARAM lParam)
{
wchar_t window_text[MAX_PATH] = { 0 };
// Fun fact - The GetWindowTextW also posts a window message to the window, the WM_GETTEXT message
if (0 != GetWindowTextW(hWnd, window_text, MAX_PATH))
{
std::wstring text(window_text);
if (std::wstring::npos != text.find(L"Notepad"))
{
std::wcout << "Found it" << text << std::endl;
notepad_window = hWnd;
return false;
}
}
return true;
}
int wmain()
{
EnumWindows(GetNotepadWindow, 0);
EnumChildWindows(notepad_window, GetEditWindow, 0);
}
After you have the HWND, you can start dealing with the message posting. You can use Spy++ again to 'tap' on the Notepad's Edit Window, and see every Window Message sent when pressing down a key, you will see the WM_KEYDOWN, followed by a WM_KEYUP most likely.
Then, you can use the PostMessage WinAPI function to post your own WM_KEYDOWN/KEYUP messages to the Notepad Window, hence controlling the input it gets.

Activate Window from code problem

I have a window on my desktop called: "Kaspersky Anti-Virus Configuration Wizard"
Here is some info about the window:
>>>> Window <<<<
Title: Kaspersky Anti-Virus Configuration Wizard
Class: AVP.ConfigureWizard
Position: 612, 247
Size: 499, 388
Style: 0x94CA0044
ExStyle: 0x00010100
Handle: 0x00081308
The window does not appear in the Windows Task Manager Tasks list (only it's process exists in processes list as "avp.exe" what, as far as i think, making it hard for me to acheive my goal. First of all i would appriciate that someone will explain how can Kaspersky Programmed i window that does not exists in "Application" Tab in "Windows Task Manager". Secondly I would be very thankful if you can help me solve my problem which is detailed here:
I want to make the window activeate (Set Focus on the window) from code (C++ \ Autoit).
I tried to use the FindWindow function of WinAPI but couldn't get the handle to this window.
I got the handle with GetForegroundWindow function and I've found out that when i use EnumWindows function the handle to kaspersky configuration window was not in the list..
this was my code:
BOOL CALLBACK EnumWindowsProc(__in HWND hwnd, __in LPARAM lParam)
{
if(g_hWnd == hwnd)
{
cout << "Found window";
return FALSE;
}
return TRUE;
}
BOOL CALLBACK EnumDesktopProc(
__in LPTSTR lpszDesktop,
__in LPARAM lParam
)
{
EnumDesktopWindows(OpenDesktop(lpszDesktop,DF_ALLOWOTHERACCOUNTHOOK,FALSE,DESKTOP_ALL),EnumWindowsProc, NULL);
return true;
}
BOOL CALLBACK EnumWindowStationProc(
__in LPTSTR lpszWindowStation,
__in LPARAM lParam
)
{
EnumDesktops(OpenWindowStation(lpszWindowStation,FALSE, WINSTA_ALL_ACCESS),EnumDesktopProc, NULL );
return true;
}
int main()
{
Sleep(3000);
g_hWnd = GetForegroundWindow(); //Here i switch to kaspersky window to get it's handle
EnumWindowStations(EnumWindowStationProc, NULL); //I call EnumDesktopWindows in EnumDesktops in EnumWindowStations to search in all HWND of my Operation System.
}
the cout << "Found Window" statement never executed.
I would be very grateful if you could help me slove this and show me the ability to make this window active.

How to get readable classname and title from HWND handle? in WinApi c++

I am using the following enumchild proc to get hwnd of each window, the problem is that i am unable to somehow detect any info from each hwnd so i can do what i want with the ones that are detected as the ones i need.
For example, how could i get window class name and the title of each window in the enum bellow?
I tried something like..
EDITED: copy pasted(if that helps)
TCHAR cName[MAX_PATH];
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) {
TCHAR cName[MAX_PATH];
GetClassName(hwnd, cName, _countof(cName));
cout << cName << endl;
return TRUE;
}
int _tmain(int argc, _TCHAR* argv[])
{
HWND hwnd = FindWindow(0, TEXT("reference"));
EnumChildWindows(hwnd, EnumChildProc, 0);
system("PAUSE");
return 0;
}
It just returns the hexadec handle info and every single time it is same, shouldnt the GetClassName func change the cName into new handle each time?
Also GetClassName function returns number of chars written to cName, i dont really see how this is useful to me? I need to get my cName in some readable format so i can do something like
if(className == TEXT("classnameiamlookingfor" && hwndtitle = TEXT("thetitlethatinterestsme") DOSOMETHINGWITHIT();
But all i get here is hexadec mess.
Isn't it Unicode build?
Check again with below:
TCHAR className[MAX_PATH];
GetClassName(hwnd, className, _countof(cName));
_tprintf(cName);

How to find the class name & title of a program in c++?

The question is how to find the class name from running programs and title of those programs. I know there already exist some scanning tools like WinDowse or spy++ from visual studio, but what I am asking you is how to make programs like those in our own source code, what function to use, is there some open source program that can help? Code appreciated, link's also :)
Use EnumWindows to enumerate all top-level windows and get their handle.
Pass the handle to GetWindowText and GetClassName to get the window title and window class respectively.
Example:
EnumWindows(EnumProc, 0);
...
BOOL CALLBACK EnumProc(HWND hWnd, LPARAM lParam) {
TCHAR title[256];
TCHAR className[256];
GetWindowText(hWnd, title, 256);
MessageBox(NULL, title, NULL, MB_OK);
GetClassName(hWnd, className, 256);
MessageBox(NULL, className, NULL, MB_OK);
return TRUE;
}

Drawing on the Desktop Background (WIN32)

Is there any way to draw on the desktop background in WIN32 and also receive notifications when the desktop background is repainted?
I tried this:
desk = GetDesktopWindow();
dc = GetDC(desk);
MoveToEx(dc,0,0,NULL);
LineTo(dc,1680,1050);
ReleaseDC(desk,dc);
But it draws on the whole screen, even over windows that are on the screen.
You can use Spy++ to find which window is the desktop background window.
On my system I see the following hierarchy:
Window 000100098 "Program Manager" Progman
Window 0001009E "" SHELLDLL_DefView
Window 00100A0 "FolderView" SysListView32
I guess you are referring to the SysListView32 - the window with all the icons. You can use FindWindowEx to find this window.
Edit
You should use a combination of FindWindowEx and EnumerateChildWindows. The code presented below can be compiled in a command line box like this: cl /EHsc finddesktop.cpp /DUNICODE /link user32.lib
#include <windows.h>
#include <iostream>
#include <string>
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
std::wstring windowClass;
windowClass.resize(255);
unsigned int chars = ::RealGetWindowClass(hwnd, &*windowClass.begin(), windowClass.size());
windowClass.resize(chars);
if (windowClass == L"SysListView32")
{
HWND* folderView = reinterpret_cast<HWND*>(lParam);
*folderView = hwnd;
return FALSE;
}
return TRUE;
}
int wmain()
{
HWND parentFolderView = ::FindWindowEx(0, 0, L"Progman", L"Program Manager");
if (parentFolderView == 0)
{
std::wcout << L"Couldn't find Progman window, error: 0x" << std::hex << GetLastError() << std::endl;
}
HWND folderView = 0;
::EnumChildWindows(parentFolderView, EnumChildProc, reinterpret_cast<LPARAM>(&folderView));
if (folderView == 0)
{
std::wcout << L"Couldn't find FolderView window, error: 0x" << std::hex << GetLastError() << std::endl;
}
HWND desktopWindow = ::GetDesktopWindow();
std::wcout << L"Folder View: " << folderView << std::endl;
std::wcout << L"Desktop Window: " << desktopWindow << std::endl;
return 0;
}
Here are the results after running finddesktop.exe
Folder View: 000100A0
Desktop Window: 00010014
As you can see the window handles are quite different.
Just quoting MSDN:
The GetDesktopWindow function returns
a handle to the desktop window. The
desktop window covers the entire
screen. The desktop window is the area
on top of which other windows are
painted.
So you are getting a Window with nested windows on them.
I'm not quite a WIN32 user, but I think the approach here is get to the lower level, take control of the graphic object that is painting the background picture, and draw there.