how do i check if a window still exists

hi again!

using CreateProcess(), my application spawns an external program. after the program was started, my application queries it's main window in order to access the client area of the application. this access to the window is done in a loop, which is supposed to end when the external program ends:

do
{
CWnd::FromHandle(theApp->newpanel->GetSafeHwnd())->GetClientRect(lpRect);

hight=lpRect->bottom-lpRect->top;
width=lpRect->right-lpRect->left;

// here goes some other stuff

Sleep(100);
GetExitCodeProcess(theApp->pi_jwalk.hProcess, &dwExitCode);
} while (dwExitCode == STILL_ACTIVE);

the problem should be at hand: when my external program ends, GetClientRect is no longer safe.

now my question is how to make sure that the process is really still running before querying it's windows client area?

thanx in advance for your help.
[1007 byte] By [reset-leo] at [2007-11-19 19:43:06]
# 1 Re: how do i check if a window still exists
if(::IsWindow(theApp->newpanel->GetSafeHwnd()))
{
// ... that window still exists.
}
ovidiucucu at 2007-11-10 23:43:08 >
# 2 Re: how do i check if a window still exists
Can you explain this part of the code

CWnd::FromHandle(theApp->newpanel->GetSafeHwnd())->GetClientRect(lpRect);

I assume it is here where you querry the other process' window but what is newpanel?

Anyway, to verify if a Window exists or not you can use IsWindow( HWND) but I do not think your approach is safe. Basically you will end up doing something like:

1. you check that the external window still exists ( with IsWindow)
2. you do your stuff

But nothing guarantess that the external window/process is not destroyed between 1 and 2. To be on the safe side I'd say you use WinAPI calls for this task. Not that MFC is not good but it hides the errors in this case. Looking at CWnd::GetClientRect implementation you can see that it will crash on debug mode and do nothing on release.

_AFXWIN_INLINE void CWnd::GetClientRect(LPRECT lpRect) const
{ ASSERT(::IsWindow(m_hWnd)); ::GetClientRect(m_hWnd, lpRect); }

The WinAPI function ::GetClientRect returns FALSE if it fails to retrieve this information. This can be used to test that your window still exists at the time of the call.

HWND extwnd;

// first check, common sense
if ( !IsWindow( extwnd))
{
CRect rect;
if ( !::getClientRect( extwnd, rect)) // extra check
return;
}
PadexArt at 2007-11-10 23:44:08 >
# 3 Re: how do i check if a window still exists
Wow! Magisterial! :)
ovidiucucu at 2007-11-10 23:45:01 >
# 4 Re: how do i check if a window still exists
i'm impressed!

Can you explain this part of the code
CWnd::FromHandle(theApp->newpanel->GetSafeHwnd())->GetClientRect(lpRect);I assume it is here where you querry the other process' window but what is newpanel?
your assumption is correct. newpanel actually is the window of the external program. the CreateProcess call is made in another thread of the application, which - after createprocess - finds the main window of the external program and then saves it in theApp (which is a global object) ->newpanel. don't blame me, it's not my code...
actually, the guy who wrote the code used to write "theApp->newpanel->GetClientRect(lpRect);" instead of what i've posted here.

Anyway, to verify if a Window exists or not you can use IsWindow( HWND) but I do not think your approach is safe. Basically you will end up doing something like:

1. you check that the external window still exists ( with IsWindow)
2. you do your stuff

But nothing guarantess that the external window/process is not destroyed between 1 and 2. To be on the safe side I'd say you use WinAPI calls for this task. Not that MFC is not good but it hides the errors in this case. Looking at CWnd::GetClientRect implementation you can see that it will crash on debug mode and do nothing on release.
that's the real reason for my initial question.

HWND extwnd;

// first check, common sense
if ( !IsWindow( extwnd))
{
CRect rect;
if ( !::getClientRect( extwnd, rect)) // extra check
return;
}

thanx a lot for that one!!!!!!!!! :thumb:

solved.
reset-leo at 2007-11-10 23:46:07 >