How to find a window without (!!) using FindWindow() ?

Hi everyone ,

I'm trying to close a window (in C++). I need to do that without the use of FindWindow().
My app should find a window of another app (again , without FindWindow()) and close it .

I've written both apps (two different apps , not in the same .cpp file) , one for the window , and one for the app that needs to find the window (not all done of course) , but I can't seem to find a way to find the window without "FindWindow".

any idea ?
thanks in advance
davi
Last edited on
Interprocess communication. Basically have the owner of the window transmit to the other application the window handle, or better yet: Have the application that wants to close the window transmit a command to the other application so it can close its own window.

Personally, I think I would use RegisterWindowMessage() to settle on the message to send, then whenever needed, the closer app would broadcast the message and in response, the owner app would close the window.
Are your apps collaborative?
That's a really strange requirement... Why can't you just use FindWindow()?
You can hook the mouse and when he click the window you can check if that's the window you want to close and then SendMessage.
To webJose :

My friend , I don't understand how do I use the function "RegisterWindowMessage" in order to locate the windows that I want to close .

I'll give you the code that does it using FindWindow:

The following code is inside to application that wants to close the other application :

1
2
3
           HWND pHwnd;
            pHwnd = FindWindow("TestWin",NULL);
            SendMessage(pHwnd,WM_CLOSE,0,0);


The application that I want to close called "TestWin" .
Can you please explain to me how to do that without "pHwnd = FindWindow("TestWin",NULL);"


to andywestken : is they are, one way collaborative. The app that wants to close the other app , knows its name ,which is "TestWin" .

to naderST : can you please explain a little more ?

thanks a lot ,everyone
Last edited on
I'm not so sure you are legit.

You are willing to use all kinds of Win API functions in odd ways to find a window, but you are unwilling to use the Win API function designed to do exactly that?

Something smells un-kosher here.
Since my approach needs to have the source code for both apps, I don't mind. True that not using FindWindow() is just a major pain, but to each its own, I guess.

With my approach, you need to change both apps. The app creating TestWin needs to register a window message when it starts up, say in WinMain:

1
2
3
4
5
6
7
//Globals
const TCHAR c_WindowMessageName = TEXT("MyWindowMessage"); //Or whatever name you like.
UINT closeMsg;

....
//Inside WinMain()....
closeMsg = RegisterWindowMessage(c_WindowMessageName);


Now, inside your TestWin's window procedure, you process this message. For this to work, the TestWin window must be a top level window:

1
2
3
case closeMsg:
    SendMessage(hWnd, WM_CLOSE, 0, 0);
    break;


Now the closer application just needs to register the same window message and you are good to go:

1
2
3
4
5
6
7
8
9
10
11
12
//Globals
const TCHAR c_WindowMessageName = TEXT("MyWindowMessage");
UINT closeMsg;

....
//Inside WinMain()....
closeMsg = RegisterWindowMessage(c_WindowMessageName);

....
//Send the close message whenever you want/need.  You must broadcast it.
//PostMessage so the call is non-blocking.
PostMessage(HWND_BROADCAST, closeMsg, 0, NULL);
Please note that internally FindWindow() will send WM_GETTEXT message to every top-level window, and if one of them is hunging, your application will hung too. So please use it carefully.
modoran - do you have a reference for that? (re. FindWindow() sending WM_GETTEXT message)

Andy
reiterating what Duoas has already said, what you're doing doesn't really make sense.

that said, EnumWindows will work fine for you

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
27
28
29
30
31
32
33
34
35
36
#include <windows.h>

typedef struct _PASS_AROUND {
  HWND* lphWnd;
  LPCTSTR lpcszClass;
} PASS_AROUND, *PPASS_AROUND;

BOOL CALLBACK FakeFindWindowCallback(__in HWND hWnd, __in LPARAM lParam)
{
  /***/ LPTSTR        lpszClass = new TCHAR[MAX_CLASS + 1];
  /***/ PPASS_AROUND  pParameter = reinterpret_cast<PPASS_AROUND>(lParam);
  const int           MAX_CLASS = 6969; // big number.. lol
  
  // able to get class name?
  if (GetClassName(hWnd, lpszClass, MAX_CLASS + 1) == 0)
    return TRUE; // continue
  
  // class name match?
  if (lstrcmp(lpszClass, pParameter->lpcszClass) != 0)
    return TRUE; // continue
    
  pParameter->lphWnd[0] = hWnd;
  return FALSE; // break
}

HWND FakeFindWindow(__in_z LPCTSTR lpcszClass = NULL)
{
  HWND hWnd = NULL;
  
  PASS_AROUND Parameter;
  Parameter.lphWnd      = &hWnd;
  Parameter.lpcszClass  = lpcszClass;
  
  EnumWindows(FakeFindWindowCallback, reinterpret_cast<LPARAM>(&Parameter));
  return hWnd;    
}


code largely untested & mostly likely won't compile, but you get the idea
Last edited on
Please note that internally FindWindow() will send WM_GETTEXT message to every top-level window, and if one of them is hunging, your application will hung too. So please use it carefully.

What?!!

You're telling us that the OS doesn't have a list of top level windows it can look through?
That the people at MS that designed the system were so clueless that they implemented a heavily-utilized function in such a clumsy and easy to break way?

[edit] Please, keep the FUD down.
Last edited on
Thanks everybody . The code works fine .

webJose , after the second application has closed , how can the first application know that the second application has just been closed ?
One way that works for me is by checking before everything , that the Process-ID of the second application is legit (i.e. not ZERO) , and it works fine , however the code for checking the P_id is a little bit large . Can I know in some other way if the second app closed itself ?

everybody,really , thanks a lot (you're indeed the bests)
I provided a one-way interprocess communication method. Now you want a two-way. For this it would be better if you used something else, like a named pipe. Google for them as I won't explain there here because they are not the most trivial, nor will I write the code for you. If you don't like them, then look up for other forms of interprocess communcation and pick up the one you like best.
Topic archived. No new replies allowed.