FindWindow fails


J

jc

Hello,

I am trying to register my own unique window class.
The registration seems successful (the window is
displayed and there are no error messages), but when
I use FindWindow, to try and find the registered window,
the functions returns NULL

How come the window registrtation is OK, but the Findwindow
fails?

BOOL CMyApp::InitInstance()
{

WNDCLASS wndcls;

memset(&wndcls, NULL, sizeof(WNDCLASS)); // start with NULL

// Specify my own class name for using FindWindow later
wndcls.lpszClassName = _T("279CB4A9_E688_4280_9639_396697EC4FB8");

// Register the new class and exit if it fails
if(!AfxRegisterClass(&wndcls))
{
TRACE("Class Registration Failed\n");
return FALSE;
}
....

///

void CMainFrame::OnClose()
{
// TODO: Add your message handler code here and/or call default

CWnd* pWnd = FindWindow(T(_"279CB4A9_E688_4280_9639_396697EC4FB8"),NULL);

//pWnd == NULL

CDocument *pDoc = GetActiveDocument();

if (pWnd != NULL) pWnd->PostMessage(WM_CLOSE);

pDoc->SetModifiedFlag(false);

CFrameWnd::OnClose();
}

TIA,
-jc
 
Ad

Advertisements

D

David Lowndes

I am trying to register my own unique window class.
The registration seems successful (the window is
displayed and there are no error messages), but when
I use FindWindow, to try and find the registered window,
the functions returns NULL

Have you tried using Spy++ to see what the window is actually created
as?

Dave
 
B

Ben Voigt [C++ MVP]

jc said:
Hello,

I am trying to register my own unique window class.
The registration seems successful (the window is
displayed and there are no error messages), but when
I use FindWindow, to try and find the registered window,
the functions returns NULL

How come the window registrtation is OK, but the Findwindow
fails?

My guess is that you didn't actually create the window using that WNDCLASS.
Your WNDCLASS is a local variable, it's going away at the end of
InitInstance (with bad implications for when you go to unregister it).
 
J

jc

Hello,

Thanks for all of the suggestions.

However, I found a simple solution on-line.

void CMainFrame::OnClose()
{
CWnd* pWnd = FindWindow(NULL,_T("My App Title"));

if (pWnd != NULL) pWnd->PostMessage(WM_CLOSE);

CFrameWnd::OnClose();
}

Thanks,
-jc
 
M

Mihai N.

However, I found a simple solution on-line.

That is probably not a solution.
You still don't find the window, but it goes away because you close the
parent.

If the window is something you create, the safest way is to "remember"
the HWND when you create the window.

FindWindow/FindWindowEx is should be used to find windows that belong
to another task/thread.
And in general finding a window by title is tricky, since the title
can be localized.
 
Ad

Advertisements

J

jc

Hello Mihai,

Thank you for your response.
You're right, I did not find the window. Actually, I did not need to find
the window. I needed to find a handle to another application, that I
wanted to close, when my main application closed.

Thanks,
-jc
 
Ad

Advertisements

M

Mihai N.

You're right, I did not find the window. Actually, I did not need to find
the window. I needed to find a handle to another application, that I
wanted to close, when my main application closed.

Is the window you are looking for the main Window of the application?
(check using Spy++ if the window if the child of the "Desktop" window).
If it is not, then see which one is the main window.
That is probably the one that needs to get the WM_CLOSE message anyway.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top