FindWindow() -- What is wrong with my code?

Hi guys, first just want to say hello as this is my first post here and I hope someone can help me with the problem I have at the moment.
I just want to write a simple program which tells the user if the default 'calculator' application is running at the time of the program being executed.

This is what I have so far :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

#include <iostream>
#include <Windows.h>


using namespace std;

 int main()

 {
	 HWND hWnds = FindWindow(NULL,(LPCTSTR)"Calculator");

	 if (hWnds == NULL)
	 {
		 cout << "Calculator isn't open." << endl;
	 }
	 else if (hWnds == "Calculator")
	 {
		 cout << "Calculator is open :) " << endl;
	 }


cin.get();

 }


Thank you to anyone who helps
AFAIK you can't compare HWND to string directly.
Deleted if statement and it worked for me.

BUT! I have non-english version of windows and I had to change "Calculator" string to match localized name.

Such program will not work for everyone. I can suggest to check process name: it's same for every Windows localized version AFAIK.

BTW, better post such topics in Windows programming.

EDIT
: Read Disch post below. He more knoleageble on this topic than me.
Last edited on
(LPCTSTR)"Calculator"

Do not cast a literal char array to a LPCTSTR. Ever. It's a bad cast, and it's probably causing the string to be bad.

If you want to know the difference between string types, and what LPCTSTR actually is, I went over all of that in this post:
http://www.cplusplus.com/forum/articles/16820/


That said... you have a few solutions:

Solution 1) Give FindWindow the TCHAR string it expects:

 
HWND hWnds = FindWindow(NULL, TEXT("Calculator") ); // <- note the TEXT macro 



Solution 2) Since you have a char string, use the char version of FindWindow rather than the TCHAR version of it:

 
HWND hWnds = FindWindowA(NULL, "Calculator" ); // <- note FindWindowA, not FindWindow 





Also....
else if (hWnds == "Calculator")

An HWND is not a string, so this will never be true. If FindWindow was successful, it will be a handle to the window (note: the handle is not the title of the window, it's an object by which you can access window properties).

For your purposes, the only thing you care about at this point is that the window is non-null.

1
2
3
4
if(hWnds == NULL)
{  /* window not found*/ }
else
{  /* window was found */ }
Let me also point out that, even if hWnds was a C-string, you shouldn't compare C-strings like that.

1
2
3
4
5
6
7
char Test[] = "Testing...";
char Test2[] = "Testing...";

if(Test == Test2)
{
    // This code will NEVER execute.
}

It's because you will be going to compare pointers, and not the actual content.

You have various ways to test equality:

1
2
3
4
5
6
7
8
9
10
11
12
// 1. strcmp
if(strcmp(Test,Test2) == 0) {
    // Equal
}
// 2. stricmp (case Insensitive strcmp)
if(stricmp(Test,Test2) == 0) {
    // Equal, even with different case
}
// You can also:
// memcmp, not suggested but still can be done
// manually check character by character
// cast to std::string and use operator== 
OK, I'll try out the solutions you guys suggested, thanks for the help
Topic archived. No new replies allowed.