app handle to pointer
OK so I have this program and I want it to be more response so I've decided to multithread it.
I had been passing &theApp to my threads and it works but gives debug errors. So I have been reading and find that I should pass handles rather than pointers.
So I pass AfxGetInstanceHandle() to the threads. Now how do I get a pointer back from that handle? FromHandle() is not defined for CWinApp and I don't see any other functions that would do this for me.
[487 byte] By [
pim42] at [2007-11-18 3:48:54]

# 1 Re: app handle to pointer
I recommend GetSafeHandle(). As for pointers, I recomment you have the threads communicate to parent thread via messages. MFC is not thread-safe.
Kuphryn
# 2 Re: app handle to pointer
Originally posted by kuphryn
I recommend GetSafeHandle().
:confused: The class 'CWinApp' does not provide such a function...
# 3 Re: app handle to pointer
Well...one question would be why you want to pass the application object around?
Responsiveness deals with the GUI and therefore dialogs, views etc. So, basically you should pass handles to these instead and exchanges messages through these handles...
# 4 Re: app handle to pointer
Originally posted by pim42
I had been passing &theApp to my threads and it works but gives debug errors. So I have been reading and find that I should pass handles rather than pointers.
What kind of debug errors? There should be no problem passing the App pointer, as the problem with passing pointers across threads is valid only for CWnd derived classes.
Another point, why do you need to pass the app to the thread function? You could simply call AfxGetApp() from inside the thread function.
# 5 Re: app handle to pointer
well I ended up solving my problem using a message pump and not threads, but back to this discussion.
The debug errors were line 883 and 884 of wincore.cpp. There is a comment there that explains not to use pointers to MFC objects. theApp varible has pointers to CWnd derived objects so doesn't that make it invalid also since something it contains is invalid?
As to why it was theApp and not the windows... that was the way the code was when I got it. I'm finishing a project someone else started. THe code was not designed to be in a seperate thread.
pim42 at 2007-11-11 3:43:37 >

# 6 Re: app handle to pointer
Originally posted by pim42
theApp varible has pointers to CWnd derived objects so doesn't that make it invalid also since something it contains is invalid?Of course, you're right. Passing the App is just an indirect way of passing the CWnd pointers attached to the app.
# 7 Re: app handle to pointer
Why do I need the CWinApp*?
I think that there is only one way to launch the new view of a document that an MDI application has created, and it's by using the CWinApp::AddDocTemplate() member.
As I'm not programing the app, just a Dll, I have only my Dll pointer, but I also have the HINSTANCE of the main app.
How can I get the app pointer from the handle?
Is there another way to launch my own view from a dll?
Thanks you all.
Juan Carlos