FindWindow does not find the a window
HWND Find = ::FindWindowEx(0, 0, "MozillaUIWindowClass", 0);
findwindow doesn't work c++
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.
FindWindow() doesn't find my window [C++]
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");
C++ FindWindow doesn't work
This issue may be that you're just casting a C-string to a T-string, which is probably a wide character string, so it's not going to work. Try this:
HWND hWnd = FindWindow(0,_T("Skype"));
This ensures the string constant is declared with the appropriate default character width that Windows API functions expect.
FindWindow( ... ) is not 'finding' created message window
I'm using a VS2013 console app. I've modified your code slightly to create a normal window and a message-only window and find both their handles by class name.
Output:
Normal window=00000000003A06FE Message-only window=00000000001F06CA
FindWindow=00000000003A06FE
FindWindowEx=00000000003A06FE
FindWindowEx(HWND_MESSAGE,...)=00000000001F06CA
Code:
typedef std::basic_string<TCHAR> tstring;
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
return DefWindowProc(hwnd, uMsg, wParam, lParam);
}
class MyWindow //: public CWnd
{
public:
tstring className;
HWND hwnd;
HWND hwndMsgOnly;
HWND find1;
HWND find2;
HWND find3;
MyWindow::MyWindow(LPCTSTR pszClassName)
{
className = pszClassName;
//auto wcn = ::AfxRegisterWndClass(NULL);
//auto created = this->CreateEx(0, wcn, pszClassName, 0, 0, 0, 0, 0, HWND_MESSAGE, 0);
// For a console app, `hInstance` is the instance of the program
HINSTANCE hInstance = GetModuleHandle(0);
WNDCLASSEX windowClass;
windowClass.lpszClassName = pszClassName;
windowClass.cbClsExtra = NULL;
windowClass.cbWndExtra = NULL;
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.hbrBackground = (HBRUSH)CreateSolidBrush(RGB(150, 0, 0));
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW);
windowClass.hIcon = LoadIcon(NULL, MAKEINTRESOURCE(IDI_APPLICATION));
windowClass.hIconSm = (HICON)LoadImage(NULL, MAKEINTRESOURCE(IDI_APPLICATION), IMAGE_ICON, 16, 16, NULL);
windowClass.hInstance = hInstance;
windowClass.lpfnWndProc = WindowProc;
windowClass.lpszMenuName = NULL;
windowClass.style = CS_VREDRAW | CS_HREDRAW;
RegisterClassEx(&windowClass);
hwnd = CreateWindowEx(NULL, pszClassName, _T("Basic Window"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 500, 500, NULL, NULL, hInstance, NULL);
hwndMsgOnly = CreateWindowEx(NULL, pszClassName, _T("Dummy name"), 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
}
};
void window_test(MyWindow & wnd)
{
LPCTSTR pszClassName = wnd.className.c_str();
wnd.find1 = FindWindow(pszClassName, nullptr); // = nullptr for OP version; okay for this version
// or using FindWindowExW( ... )
/**
HWND hwndParent is a handle to the parent window whose child windows are to be searched.
If hwndParent is NULL, the function uses the desktop window as the parent window.The function searches among windows that are child windows of the desktop.
If hwndParent is HWND_MESSAGE, the function searches all message-only windows.
**/
HWND hWndParent = nullptr;
wnd.find2 = FindWindowExW(hWndParent, nullptr, pszClassName, nullptr);// = nullptr for OP version; okay for this version
hWndParent = HWND_MESSAGE;
wnd.find3 = FindWindowExW(hWndParent, nullptr, pszClassName, nullptr);// = nullptr for OP version; finds the message-only window in this version
}
void main()
{
MyWindow wnd(_T("test_window"));
cout << "Normal window=" << wnd.hwnd << " Message-only window=" << wnd.hwndMsgOnly << endl;
window_test(wnd);
cout << "FindWindow=" << wnd.find1 << endl;
cout << "FindWindowEx=" << wnd.find2 << endl;
cout << "FindWindowEx(HWND_MESSAGE,...)=" << wnd.find3 << endl;
}
Why I cannot find window?
To summarize the comments there are two errors
1) (See @Tom Brunberg) is that the length is set incorrectly which is why you only get part (about half? of the string)
It should be
copyDataStruct.cbData := sizeof( Char )*(Length(stringToSend) + 1 );
2) The forms caption is being changed which invalidates the expression
FindWindow(PChar('TfrmReceiver'), PChar('frmReceiver'))
because the second parameter is the form's caption (in Delphi terminology)
Can't find window C++
Mode 1 : Use ::FindWindowEx()
Mode 2 : Get window handle from exe name(! Set exe name of Dead Island)
TCHAR* szExeName = _T("Dead Island"); //Dead Island.exe
HANDLE GetHandleOfDeadIsland()
{
HANDLE hDeadIsland = INVALID_HANDLE_VALUE;
HANDLE hSnap = INVALID_HANDLE_VALUE;
PROCESSENTRY32 pe32;
pe32.dwSize = sizeof(PROCESSENTRY32);
hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (INVALID_HANDLE_VALUE != hSnap)
{
if (Process32First(hSnap, &pe32))
{
do
{
//!!! Attention pe32.szExeFile always return exe file name. not window title.
if (NULL != _tcsstr(pe32.szExeFile, szExeName))
{
hDeadIsland = OpenProcess(PROCESS_ALL_ACCESS, TRUE, pe32.th32ProcessID);
break;
}
} while (Process32Next(hSnap, &pe32));
}
}
return hDeadIsland;
}
int main()
{
HANDLE hDeadIsland = GetHandleOfDeadIsland();
if (INVALID_HANDLE_VALUE == hDeadIsland)
{
_tprintf(_T("Not Found\n"));
}
else
{
_tprintf(_T("Found\n"));
}
return GetLastError();
}
win32gui.FindWindow Not finding window
The line
shell.AppActivate("Tera Term VT")
works on the window title and therefore it works.
You should be able to to the same with
hwndMain = win32gui.FindWindow(None, "Tera Term VT")
that is, swapping the arguments so that it also works based on the window title
If you want to work based on the window class name you could use a tool like Spy++ with its Finder Tool to target the Tera Term window and get its window class name from the properties
Related Topics
Best C++ Matrix Library for Sparse Unitary Matrices
Hash Function for User Defined Class. How to Make Friends?
Advantages of Using Arrays Instead of Std::Vector
G++ Can't Find Boost Libraries. I Say They'Re in Plain Sight
What Are Google Test, Death Tests
Thread Safety of Matlab Engine API
Reading from .Txt File into Two Dimensional Array in C++
How to Compile: Unrecognized Relocation
Best Way to Write a Linux Daemon
Sdl Fake Fullscreen Mode on Dual Monitor Setup Under Linux
Are End+1 Iterators for Std::String Allowed
Std::Set Iterator Automatically Const
Can You Make a Computed Goto in C++
Why How to Implicitly Convert an Int Literal to an Int * in C But Not in C++
Tool to Generate Xml File from Xsd (For Testing)
Convert from Utf-8 to Unicode C++
How to Compile a C++ Code in Gcc (G++) to See The Name Mangling on Overloaded Functions