Re: MFC and User Defined objects
one-trick-pony wrote:
Currently, I am retreiving main gui window handle just before running
code for thread creation. I tried retrieving handle using the code
below without success.
CString a;
CDummyDlg i;
HWND h = i.m_hWnd;
a.Format("i.m_hWnd %d", h);
AfxMessageBox(a);
This code fails. I don't understand why. It always gets 0 as
handle. What am I doing wrong?
A CWnd object such as CDummyDlg does not have a window (so it does not
have a valid m_hWnd) when it is constructed. It only has a window and
valid m_hWnd after its Create (or DoModal) function is called. The
design of MFC requires this "two stage construction".
The code listed below works . I get
a valid window handle. But why can't I retrieve the handle using
above method? To me above code seems more logical.
HWND mywin;
mywin = ::FindWindow(0,"Dummy");
a.Format("Window Handle %d", mywin);
AfxMessageBox(a);
Your thread depends on the HWND, so the thread should not be created
until the HWND is available. That happens in OnInitDialog. So create
the thread there. Before you create it set mywin = m_hWnd.
The other thing is I decalared mywin as a global variable. Because
otherwise, I get failure inside my c->c++ adapter code. It reports
that mywin is undeclared.
// Adapter code
UINT CDummyDlg::tMain(LPVOID p)
{
CDummyDlg * self = (CDummyDlg *)p;
self->tMain(mywin); <<< Here it says undeclared variable if I don't
make mywin global variable.
return 0;
}
If you guys eliminate some mysteries on how to appropriately construct
my code, that will help me become a better coder. Thanks.
If you see two posts, please disregard one.
You created mywin as a local variable. That means it does not exist
outside the function where it was created. See C++ "scope" rules.
--
Scott McPhillips [VC++ MVP]