FindWindow is not working? - c++

I just want to know why this code is not working?
HWND hWnds = FindWindow(NULL,(LPCTSTR)"Calculator");
It always returns NULL for the hWnds....
Any idea how to fix this? Thank you :)

Dont cast the string.
HWND hWnds = FindWindow(NULL,_T("Calculator"));

Make a folder named 'Calculator' and enter into it using Windows Explorer.
Now run your program - wrong window handle would be returned!
Using Spy++ on Windows 7, I found out class-name of Calculator window to be: CalcFrame.
Try using this:
HWND hWnds = FindWindow(_T("CalcFrame"), NULL);

Related

C++ findwindow with traditional chinese character [duplicate]

So Im trying to create a camo unlocker but I have never had trouble with getting process id through findwindow
but now Im trying to find black ops 2's proc id but the window name doesn't work
Call of Duty®: Black Ops II
CODE:
#include <iostream>
#include <Windows.h>
#include <tchar.h>
using namespace std;
int main(){
HWND hWnd = FindWindow(0, _T("Call of Duty®: Black Ops II - Multiplayer"));
if(hWnd){
cout << "window found" << endl;
}
return 0;
}
It looks like the registered symbol could be unicode, you'll want to use FindWindowW():
Unicode and ANSI names
FindWindowW (Unicode) and FindWindowA (ANSI)
Alternatively, you could use FindWindowEx() and search for the window class name.
FindWindow works correctly. The possible causes for your problem are:
You have an encoding error. You should use the Unicode API:
HWND hWnd = FindWindowW(NULL, L"Call of Duty®: Black Ops II - Multiplayer");
There is no top level window with that window text. Use a tool like Spy++ to check that.
You should also make sure that you read the documentation carefully. Specifically it states the following:
If the function fails, the return value is NULL. To get extended error information, call GetLastError.
You should do as it says and call GetLastError in event of failure.
I would try to find the window by class as an application may change its title and class names usually do not have fancy characters. If you do not know them look for some tool (Spy++ + i think it comes with Visual Studio) or create a list with a simple tool using EnumWindows and GetClassName
FindWindowA worked for me :) so I just changed from tchar to the normal HWND hWnd = FindWindowA(0, ("Call of Duty®: Black Ops II - Multiplayer"));

how to get text from a window with specific HWND?

I'm new to win32 programming and haven't worked around with cpp for a long time. What I intend to do is to get a window's HWND via spy++, and get the text of this window. The problem is that I don't know how to create a HWND object, could anyone give me some idea? Thanks a lot!
If you have the numeric value of the HWND, you can cast it to the right type. Start with an integer of the right size, e.g.:
uintptr_t numeric_hwnd = 0x987654;
HWND hwnd = reinterpret_cast<HWND>(numeric_hwnd);

Getting Window info from its HWND

I have this code:
HWND WindowHandle;
WindowHandle = FindWindowA( WindowClass, WindowName );
if( WindowHandle != NULL)
{
// here I want to populate a listbox with the window's information like
// its process name and PID, executable file name, and its window title.
}
I already know how to get the Window Title, with SendMessage(); and WM_GETTEXT, but I want to know how to get it's process name and PID, and also the executable file location and name.
Could anyone help me?
Please note that I am working with C++ MFC.
Windows offers a function called GetWindowThreadProcessId.
It sounds like what you want.
Disclaimer: I haven't used it.
You can find the PID in PROCESS_INFORMATION. Just declare say: PROCESS_INFORMATION pi and access PID like this: pi.hProcesss pi.dwProcessId.

FindWindow by class name not working?

To preface we have a strange requirement that all dialogs must be modeless for an MFC application. There is a particular dialog using region drawing and some custom controls to select dates and times for viewing past and future data per view. I need to be able to close this window when it loses focus, the main app gets a system command, etc.
I figured the easiest way to do this would be to register the class like so:
// for CWnd::FindWindow
WNDCLASS wndcls;
SecureZeroMemory(&wndcls, sizeof(WNDCLASS));
wndcls.lpszClassName = L"CTransactionDialog";
wndcls.style = CS_HREDRAW | CS_VREDRAW;
wndcls.lpfnWndProc = AfxWndProc;
wndcls.cbClsExtra = wndcls.cbWndExtra = 0;
wndcls.hInstance = AfxGetInstanceHandle();
wndcls.hIcon = NULL;
#ifndef _WIN32_WCE_NO_CURSOR
wndcls.hCursor = AfxGetApp()->LoadStandardCursor(IDC_ARROW);
#else
wndcls.hCursor = 0;
#endif
wndcls.hbrBackground = (HBRUSH) (COLOR_BACKGROUND + 1);
wndcls.lpszMenuName = NULL;
BOOL retVal = AfxRegisterClass(&wndcls);
if (!retVal)
AfxMessageBox(L"AfxRegisterClass(CTransactionDialog) Failed");
Then later in response to various event handlers and messages where I would want these modeless window or windows to be closed to do something simple like this:
CWnd* pFound = NULL;
while ((pFound = CWnd::FindWindow(L"CTransactionDialog", NULL)) != NULL)
pFound->DestroyWindow();
However despite the registration of the class succeeding and looking at GetRuntimeClass of the dialog in question in debug and seeing that is matches up as expected the FindWindow never seems to find or close these modeless dialogs as expected.
What am I doing wrong or is there a better way to go about this?
Update: This is how the dialog is created via a static method on the dialog class. The dialog resource for the id specified in create has the Popup property set which should resolve to WS_POPUP style under the MFC covers. The dialog shouldn't and doesn't have a parent as far as I knew.
CTransactionDialog* CTransactionDialog::ShowTransactionDialog(const CRect& crCtrlToFloatAbove, UINT dialogID, Itime defaultTime, Itime initialTime)
{
CTransactionDialog* pCTDialog = new CTransactionDialog(crCtrlToFloatAbove, dialogID, defaultTime, initialTime);
pCTDialog->Create(CTransactionDialog::IDD);
pCTDialog->ShowWindow(SW_SHOW);
return pCTDialog;
}
Update: Doh! FindWindowEx isn't finding anything either.
CWnd::FindWindowEx(AfxGetMainWnd()->GetSafeHwnd(), NULL, L"CTransactionDialog", NULL);
However I have a new plan. I'm just going to make my own window message and handle it on the main frame. I think I can get away with passing a pointer to the dialog as the lParam of the message and then casting it to a CWnd* then calling DestroyWindow. It will work for most cases in a very round about way. I may run into trouble with minimizing and maximizing of the main frame window for dialogs that nothing is holding a pointer too but we'll see.
FindWindow doesn't work with child windows. To find a child window, you can use FindWindowEx, passing the HWND of the parent window as the first parameter.
Class name denotes NOT the c++ class name - it denotes the window class name. This name was used to register the window by the OS and has nothing to do with the c++ class.
May MSDN enlight you...
CWnd::FindWindow does not search child windows.
Is it possible that the mode less window that you are creating has a parent set and that is the reason why FindWindow doesn't find it ?
Below is the simplest and cleanest solution I could come up with:
BOOL CTransactionDialog::OnNcActivate(BOOL bActive)
{
if (!bActive)
PostMessage(WM_CLOSE);
return CDialog::OnNcActivate(bActive);
}
Not 100% sure about this but I think you need to set your class name in the resource file. You are defining a windows class and creating a dialog class but you aren't linking them. Setting a class name in the WNDCLASS struct won't help unless you actually have a way of linking it to the dialog. If you use the resource file and define the dialog, and class name in there then it should work.

How to get names of the icons on desktop

guys.
I want to obtain names of icons on desktop in c++. And I know how to get their handle:
HWND hwnd = FindWindow("Progman","Program Manager");
HWND hwndSHELLDLL_DefView = ::FindWindowEx( hwnd, NULL, "SHELLDLL_DefView", NULL );
HWND hwndSysListView32 = ::FindWindowEx( hwndSHELLDLL_DefView, NULL, "SysListView32", "FolderView" );
What's next?
First, you need to get the location of the desktop folder using SHGetFolderLocation.. Next, you enumerate the contents of this folder using IShellFolder::EnumObjects
From here, the sky is the limit. Tons of information on interacting with the windows shell here.
Have fun!
Update:
A quick google search turns up this sample which seems to do exactly what you want.