0

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.

user3499229
  • 35
  • 2
  • 4
  • What about `hwnd = FindWindowA(NULL, "Skype");`? – Mustafa Chelik Jan 30 '15 at 18:15
  • A cast means "I know what I am doing, now shut up". However, if you _do_ know what you're doing, you often don't need that cast in the first place. Irony. – MSalters Jan 30 '15 at 18:38
  • A bit off-topic: Are you developing a kind of "plugin" for skype? if so, remember there's a skype API: https://msdn.microsoft.com/en-us/library/office/dn745878 (I haven't used with C++ but C#.NET but I think C#'s one are nothing but wrappers to the C++) – Jack Jan 30 '15 at 19:20

1 Answers1

3

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.

Gerald
  • 23,011
  • 10
  • 73
  • 102
  • 4
    Actually, `_T` doesn't convert anything. Conversions are what happens when you cast something. This is just a macro to stick an `L` prefix on a string. – MSalters Jan 30 '15 at 18:36
  • You're right, I had a brain fart and used the wrong wording. I fixed it. – Gerald Jan 30 '15 at 19:21
  • @MSalters: Why use it then? I mean, Isn't easy to rather just use `L` prefix on a string than this macro? – Jack Jan 30 '15 at 19:22
  • @Jack what the macro does depends on the compiler settings for the application. If the compiler settings specify a Unicode character set, then it will prepend the L. If the settings specify MBCS, then it does not prepend the L. Using _T guarantees that it will be correct in either case. – Gerald Jan 30 '15 at 19:25
  • it does L conditionally. if UNICODE is defined. This is the standard calling style of the windows API – pm100 Jan 30 '15 at 19:25
  • Along the same vein, most Windows API function calls are actually macros as well. There are 2 versions of `FindWindow`; `FindWindowA` and `FindWindowW`. `FindWindow` is a macro that resolves to `FIndWindowA` or `FindWindowW` depending on the character set configuration. Unless you are calling the A or W versions directly, you should always use `_T` to wrap your string constants. – Gerald Jan 30 '15 at 19:28