c++findwindow

C++ FindWindow doesn't work


I have Windows 8 and Visual Studio 2013.

#include <iostream>
#include <windows.h>
using namespace std;




int main()

{

HWND hWnd = FindWindow(0,(LPCTSTR)"Skype");
if (hWnd == 0)

{

    cerr << "Cannot find window" << endl;
}
return 0;
}

The window is called "Skype" TLoginForm in Spy++ so I use the correct name but I get the error message.(Cannot find window) I know there are lot of similar questions but i didn't get answer.


Solution

  • 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.