OnClose of foreign window 
Author Message
 OnClose of foreign window

Hi,

I have a foreign window created with CreateProcess and the only handler I have
is a "static HWND".
Now I need to change some variable when this window is closed. Does somebody
have an idea how I can get to the OnClose event (or anything comparable) of
this window, and how I make sure it's always handled ?

Thanks.



Fri, 11 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:
>I have a foreign window created with CreateProcess and the only handler I have
>is a "static HWND".
>Now I need to change some variable when this window is closed. Does somebody
>have an idea how I can get to the OnClose event (or anything comparable) of
>this window, and how I make sure it's always handled ?

If you want to know when the process has closed (rather than a window
associated with it), keep the process handle that CreateProcess
returns and use it with GetExitCodeProcess (if you're polling) or
WaitForSingleObject.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 11 Apr 2003 03:00:00 GMT  
 OnClose of foreign window


Quote:

> >I have a foreign window created with CreateProcess and the only handler I
have
> >is a "static HWND".
> >Now I need to change some variable when this window is closed. Does somebody
> >have an idea how I can get to the OnClose event (or anything comparable) of
> >this window, and how I make sure it's always handled ?

> If you want to know when the process has closed (rather than a window
> associated with it), keep the process handle that CreateProcess
> returns and use it with GetExitCodeProcess (if you're polling) or
> WaitForSingleObject.

Thanks, I now have no errors with the following code :

(Code in control class)
CArray<HANDLE*,HANDLE*> OpenedWindows; //this is the declaration of a handle
array

(if CreateProcess is activated)
HANDLE hand=pi.hProcess; //pi is the process information structure
if (OpenedWindows.GetSize()==0) OpenedWindows.SetSize(0,1);
OpenedWindows.Add(&hand); //for each process created, add a handler to the
array

(Code in main (?) view)
void CMyMDIAppsView::OnActivate(UINT nState, CWnd* pWndOther, BOOL bMinimized)
{
 CFormView::OnActivate(nState, pWndOther, bMinimized);
 for(int i=0;i<m_MyControl1.OpenedWindows.GetSize();i++)
 {
  int nExitCode;
  GetExitCodeProcess(m_MyControl1.OpenedWindows.GetAt(i),(unsigned
long*)&nExitCode);
  if (nExitCode==STILL_ACTIVE);
  else
  {
   int z=5; // do something if a window has ceased to exist
  }
 }

Quote:
}

- do you think this is the correct approach for detecting closed windows ?

Thanks.



Fri, 11 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:

>Thanks, I now have no errors with the following code :
>(if CreateProcess is activated)
>HANDLE hand=pi.hProcess; //pi is the process information structure

[...]

You might have a handle leak. Don't forget that if you use CreateProcess,
you need to close the PROCESS_INFORMATION.hThread and
PROCESS_INFORMATION.hProcess handles when you're done with
them.

From the CreateProcess docs:
| The created process remains in the system until all threads
| within the process have terminated and all handles to the
| process and any of its threads have been closed through calls
| to CloseHandle. The handles for both the process and the main
| thread must be closed through calls to CloseHandle. If these
| handles are not needed, it is best to close them immediately
| after the process is created.

--
 - Katy

Katy Mulvey             Please post replies to the newsgroup, thanks!
ORMEC Systems           Check out the VC++ FAQ: http://www.mvps.org/vcfaq
http://www.ormec.com    MVP/VC++  http://support.microsoft.com/support/mvp



Fri, 11 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:
>CArray<HANDLE*,HANDLE*> OpenedWindows;

You might want to change its name since these are process handles not
Windows. You probably don't need them as pointers either - just copy
the handle value.

Quote:
>- do you think this is the correct approach for detecting closed windows ?

Remember, it's a process not a window!

Other than Katy's suggestion to remember to close all the handles when
you're done with them, I can't spot anything else.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Fri, 11 Apr 2003 03:00:00 GMT  
 OnClose of foreign window


Quote:

> >CArray<HANDLE*,HANDLE*> OpenedWindows;

> You might want to change its name since these are process handles not
> Windows. You probably don't need them as pointers either - just copy
> the handle value.

Ok done.

Quote:

> >- do you think this is the correct approach for detecting closed windows ?

> Remember, it's a process not a window!

What exactly is the difference between closing the process and closing the
window in this case ?

Quote:
> Other than Katy's suggestion to remember to close all the handles when
> you're done with them, I can't spot anything else.

If a handle in the array is empty (because its associated window/process is
closed, can I still close it ?


Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window


Quote:


> >I now have no errors with the following code :
> >(if CreateProcess is activated)
> >HANDLE hand=pi.hProcess; //pi is the process information structure

> [...]

> You might have a handle leak. Don't forget that if you use CreateProcess,
> you need to close the PROCESS_INFORMATION.hThread and
> PROCESS_INFORMATION.hProcess handles when you're done with
> them.

What happens with them if I close the window ?
If they are empty, can I still close them ?

Quote:

> From the CreateProcess docs:
> | The created process remains in the system until all threads
> | within the process have terminated and all handles to the
> | process and any of its threads have been closed through calls
> | to CloseHandle. The handles for both the process and the main
> | thread must be closed through calls to CloseHandle. If these
> | handles are not needed, it is best to close them immediately
> | after the process is created.

Am I right in saying that all created threads are encapsulated in the process ?
So when I close/destroy the process, all threads are closed/destroyed as well I
presume ?

Thanks.



Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:
>What exactly is the difference between closing the process and closing the
>window in this case ?

A process does not necessarily have any windows. On the other hand, a
single process could have multiple top level windows.

Quote:
>> Other than Katy's suggestion to remember to close all the handles when
>> you're done with them, I can't spot anything else.

>If a handle in the array is empty (because its associated window/process is
>closed, can I still close it ?

It depends what do you mean by "empty".

If you've got a process handle in the array that you'll no longer be
needing, then you need to use CloseHandle on the process handle -
otherwise you'll retain the process resource until your process
closes.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window


Quote:

> >What exactly is the difference between closing the process and closing the
> >window in this case ?

> A process does not necessarily have any windows.

But in my case it has exactly one window I presume ? (I open one foreign
application per CreateProcess)

Quote:
> On the other hand, a
> single process could have multiple top level windows.

If I put this in a hierarchical structure, can I say that
Process>Windows>Threads ?
So the process is the top level ? So if I close the process, all windows and
threads associated are closed as well ?
But what happens when e.g. the Excel window is closed - is the associated
process then closed as well ?

Quote:
> >> Other than Katy's suggestion to remember to close all the handles when
> >> you're done with them, I can't spot anything else.

> >If a handle in the array is empty (because its associated window/process is
> >closed, can I still close it ?

> It depends what do you mean by "empty".

I presumed that when the window was closed, the associated process has value
NULL ?

Quote:
> If you've got a process handle in the array that you'll no longer be
> needing, then you need to use CloseHandle on the process handle -
> otherwise you'll retain the process resource until your process
> closes.

I am unsure about the order of things now :
- I want to change some variable when a foreign window created with
CreateProcess is closed
-- Which variables/objects remain/retain their value after this ?
-- Shouldn't I call CloseHandle at the utter last point (so after changing the
variable ?)

Thanks (once more ;-))



Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:
>> A process does not necessarily have any windows.

>But in my case it has exactly one window I presume ?

It depends on the application!

Quote:
>So if I close the process, all windows and
>threads associated are closed as well ?

Yes.

Quote:
>But what happens when e.g. the Excel window is closed - is the associated
>process then closed as well ?

It depends on how Excel works. With most applications the answer is
yes - when you close the top level window, the process exits as well.

Quote:
>I presumed that when the window was closed, the associated process has value
>NULL ?

No, you've got a copy of the process handle. Closing the window won't
affect that handle. If the process has exited, calling
GetExitCodeProcess on your handle will enable you to know that the
process has ended.

Quote:
>I am unsure about the order of things now :
>- I want to change some variable when a foreign window created with
>CreateProcess is closed

What variables are these? Do they belong to your application?

Quote:
>-- Shouldn't I call CloseHandle at the utter last point

You'll close the process handle when you no longer need it.

For a good clear explanation of processes, I suggest that you have a
read of Jeffrey Richter 's book "Advanced Windows" (I think the latest
version is now titled "Programming Applications for Microsoft
Windows").

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window
I'm now thinking about opening for example Excel in my own child window.
I think I have more and easier control over it then.
Don't you think this is possible and the better way ?
And if this has happened, the Window will have it's own thread, but in the same
process, right ?

B.t.w. I have the book you mentioned (by Richter) right here :-)

Thanks for your help.



Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:
>I'm now thinking about opening for example Excel in my own child window.
>I think I have more and easier control over it then.

I think this will be a thousand times harder, but don't let me put you
off if you want to try it.

Quote:
>B.t.w. I have the book you mentioned (by Richter) right here :-)

Good.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Sat, 12 Apr 2003 03:00:00 GMT  
 OnClose of foreign window


Quote:
> >I'm now thinking about opening for example Excel in my own child window.
> >I think I have more and easier control over it then.

> I think this will be a thousand times harder, but don't let me put you
> off if you want to try it.

Can you please tell me why you think that ?
So what's better : a child process (thread, child window) or a parallel process
(process, own window) and why ?

Thanks.



Sun, 13 Apr 2003 03:00:00 GMT  
 OnClose of foreign window

Quote:
>>I'm now thinking about opening for example Excel in my own child window.
>Can you please tell me why you think that ?

Because in-place activation is a difficult and complex thing to do
(assuming that's what you were intending).

Quote:
>So what's better : a child process (thread, child window) or a parallel process
>(process, own window) and why ?

What you were doing would seem the most easy to me - though I really
don't know what it was that you were doing.

Dave
--
MVP VC++ FAQ: http://www.mvps.org/vcfaq
My address is altered to discourage junk mail.
Please post responses to the newsgroup thread,
there's no need for follow-up email copies.



Sun, 13 Apr 2003 03:00:00 GMT  
 OnClose of foreign window


Quote:

> >>I'm now thinking about opening for example Excel in my own child window.

> >Can you please tell me why you think that ?

> Because in-place activation is a difficult and complex thing to do
> (assuming that's what you were intending).

I don't get this "In-place activation" exactly from my MSDN library, but it
seems to be having to do with a container/server application (I have an MDI)
and be barely supported by MFC.
What I imagine to do is when the user clicks on a button named Excel, a new
child window opens and inside that window (and only inside that window) Excel
runs. So something like "CreateWindow" and then "CreateProcessInThatWindow".
Isn't this possible ?


Sun, 13 Apr 2003 03:00:00 GMT  
 
 [ 20 post ]  Go to page: [1] [2]

 Relevant Pages 

1. Saving window position onClose

2. Splitter Windows Make FrameWnd::OnClose give Access Violation

3. Getting HICON for foreign window?

4. Subclassing foreign window and changing memory in it

5. Writing to foreign windows...

6. CAsyncSocket::OnClose()

7. OnClose again

8. OnClose and OnQueryEndSession

9. OnClose vs. WindowDestroy

10. Overriding OnClose()

11. Why Isnt CSocket::OnClose Getting Called

12. Onclose() ?

 

 
Powered by phpBB® Forum Software