Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Why does AfxGetMainWnd returns nulls?

1,276 views
Skip to first unread message

MegK123

unread,
May 29, 2009, 7:25:01 AM5/29/09
to
Hello all

I am new to MFC. I have a MFC shared dll, which will be intregarted into
another application. To give details abt the dll, it creates an widget
(having grid format). When I call this MFC dll from the exe, it will generate
an object for me. Now prior to creating the object, I have a window that pops
asking few info (like name,type,etc). I want to retrieve the handle to this
window. I have the create button on this window, within whose code I do the
following

IINT32 testClass::onCreate(...)
{
typedef long int jint;
CWnd* pWnd = AfxGetMainWnd();
jint hndl = (jint)pWnd->GetSafeHwnd();

}

But, I always receieve an null from AfxGetMainWnd(). and 0 from get
safehwnd(). I do not know exactly how to associate the CWnd to my class
"testClass".

Any help ?

Thanks in advance
-Megha

David Webber

unread,
May 29, 2009, 7:45:10 AM5/29/09
to
"MegK123" <Meg...@discussions.microsoft.com> wrote in message
news:7271AA19-D5F6-4653...@microsoft.com...


> IINT32 testClass::onCreate(...)
> {
> typedef long int jint;
> CWnd* pWnd = AfxGetMainWnd();
> jint hndl = (jint)pWnd->GetSafeHwnd();
>
> }
>
> But, I always receieve an null from AfxGetMainWnd(). and 0 from get
> safehwnd(). I do not know exactly how to associate the CWnd to my class
> "testClass".


AfxGetMainWnd() should return a pointer to your application's main window,
after it has been created.

When are you calling this, and what do you want to do with it?

(Also GetSafeHwnd() returns a variable of type HWND. This is a ubiquitous
type in Windows. You shouldn't be having to tyedef jint as long int and
using that - just use HWND.)

Dave
--
David Webber
Author of 'Mozart the Music Processor'
http://www.mozart.co.uk
For discussion/support see
http://www.mozart.co.uk/mozartists/mailinglist.htm

MegK123

unread,
May 29, 2009, 8:46:01 AM5/29/09
to
Hi ,

I need to pass this handle to the next screen which will be coded in JAVA. I
need to pass arguments(where the handle is one of them) when i launch the
JVM. I tried without typecasting , but no changes. " I get a null when there
is call to AfxGetMainWnd()" . I need to know the senarios in which
AfxGetMainWnd() return null .

Thanks
-Megha

MegK123

unread,
May 29, 2009, 8:49:02 AM5/29/09
to
>> When are you calling this,

There is a window which is created, it has a create button , on the click of
which i will return to the OnCreate function. There itself i have been
trying to retrieve the main window handle.

Please let me know if there is anything else you need

Thanks
-Megha

Ajay

unread,
May 29, 2009, 9:14:41 AM5/29/09
to
If you call this in a MFC regular DLL, you will get NULL for
AfxGetMainWnd. Are you using a Regular DLL or an MFC Extension DLL? It
would appear that you are using a Regular DLL? If yes, any reason to
do that?

You can use macro AFX_MANAGE_STATE (w/getappmodule) to make it work
the way you want it.

--
Ajay

Scott McPhillips [MVP]

unread,
May 29, 2009, 9:32:58 AM5/29/09
to
"MegK123" <Meg...@discussions.microsoft.com> wrote in message
news:7271AA19-D5F6-4653...@microsoft.com...


It does not sound like your window that pops up is the "main" window. So
AfxGetMainWnd() is not what you want at all. When you create the popup
window its HWND is the m_hWnd member of the window object.

--
Scott McPhillips [VC++ MVP]

AliR (VC++ MVP)

unread,
May 29, 2009, 11:05:36 AM5/29/09
to
There is also the possibility that this is a dialog based application and
the m_pMainWnd variable of CWinApp is not set.

AliR.


"David Webber" <da...@musical-dot-demon-dot-co.uk> wrote in message
news:OcCcAMF4...@TK2MSFTNGP02.phx.gbl...

Tom Serface

unread,
May 29, 2009, 4:54:11 PM5/29/09
to
If the DLL is the one popping up the dialog then knowing the main window
isn't going to help you any. You need to determine who is popping up the
window. As Ajay mentioned, you will get NULL from the call if the DLL
doesn't know about the main window.

When are you making the call? Could it be happening before the mainwnd is
created?

Tom

"MegK123" <Meg...@discussions.microsoft.com> wrote in message
news:7271AA19-D5F6-4653...@microsoft.com...

MegK123

unread,
Jun 1, 2009, 1:40:02 AM6/1/09
to
Hello All,

Its the main exe that is poping up the parent window, whose handle I am
interested in. The child window will be called from my dll. The window is
already created and i just need to retrieve its handle.

Can someone point out on how to use m_hWnd?

As per Ajay's question :
I have been using MFC regular dll and not the extension dll. can you let me
know how to use macro AFX_MANAGE_STATE (w/getappmodule) to make it work?

As per below comments
/*
If this DLL is dynamically linked against the MFC
// DLLs, any functions exported from this DLL which
// call into MFC must have the AFX_MANAGE_STATE macro
// added at the very beginning of the function.
//
// For example:
//
// extern "C" BOOL PASCAL EXPORT ExportedFunction()
// {
// AFX_MANAGE_STATE(AfxGetStaticModuleState());
// // normal function body here
// }
*/

AFX_MANAGE_STATE needs to be used with exported functions .

Thanks
-Megha

Ajay

unread,
Jun 1, 2009, 9:11:48 AM6/1/09
to
On Jun 1, 1:40 am, MegK123 <MegK...@discussions.microsoft.com> wrote:

>
> As per Ajay's question :
> I have been using MFC regular dll and not the extension dll. can you let me
> know  how to  use macro AFX_MANAGE_STATE (w/getappmodule) to make it work?
>
> As per below comments
> /*
> If this DLL is dynamically linked against the MFC
> //              DLLs, any functions exported from this DLL which
> //              call into MFC must have the AFX_MANAGE_STATE macro
> //              added at the very beginning of the function.
> //
> //              For example:
> //
> //              extern "C" BOOL PASCAL EXPORT ExportedFunction()
> //              {
> //                      AFX_MANAGE_STATE(AfxGetStaticModuleState());
> //                      // normal function body here
> //              }
> */
>
> AFX_MANAGE_STATE  needs to be used with exported functions .

Thats true but that doesnt mean it shouldnt be used elsewhere. In your
case, since you want to access app module's state when accessing
mainwindow, use the following:

AFX_MANAGE_STATE(AfxGetAppModuleState()) ;

This should return a non null main window pointer.

Also, why are you using a Regular DLL? Why not an extension DLL?

--
Ajay


MegK123

unread,
Jun 2, 2009, 10:26:01 AM6/2/09
to
HEllo

Can some one plz let me know on how to use in code ?
I have been trying something like
IINT32 testClass::onCreate(IWidget *pWidget,IBOOLEAN &bValid)
{
AFX_MANAGE_STATE(AfxGetAppModuleState()) ;

//does something go here ??

}

But I am not yet sure on how to retrieve the handle???


"Ajay" wrote:

> On Jun 1, 1:40 am, MegK123 <MegK...@discussions.microsoft.com> wrote:
>
> >
> > As per Ajay's question :
> > I have been using MFC regular dll and not the extension dll. can you let me
> > know how to use macro AFX_MANAGE_STATE (w/getappmodule) to make it work?
> >
> > As per below comments
> > /*
> > If this DLL is dynamically linked against the MFC
> > // DLLs, any functions exported from this DLL which
> > // call into MFC must have the AFX_MANAGE_STATE macro

> > // added at the very beginning of the function..

Ajay

unread,
Jun 2, 2009, 10:37:54 AM6/2/09
to
On Jun 2, 10:26 am, MegK123 <MegK...@discussions.microsoft.com> wrote:
> HEllo
>
> Can some one plz let me know on how to use in code ?
> I have been trying something like
> IINT32 testClass::onCreate(IWidget *pWidget,IBOOLEAN &bValid)
> {
>         AFX_MANAGE_STATE(AfxGetAppModuleState()) ;
>
>         //does something go here ??
>
> }
>
> But I am not yet sure on how to retrieve the handle???

Whatever code you want goes in there. If you use AfxGetMainWnd after
the macro, you should get a non null value:

IINT32 testClass::onCreate(IWidget *pWidget,IBOOLEAN &bValid)
{
AFX_MANAGE_STATE(AfxGetAppModuleState()) ;

.......
CWnd* pWnd = AfxGetMainWnd();
ASSERT_VALID(pWnd);
.....
}

--
Ajay

MegK123

unread,
Jun 4, 2009, 12:46:01 AM6/4/09
to
Hello All,

I have tried out the following code
AFX_MANAGE_STATE(AfxGetAppModuleState()) ;

CWnd* pWnd = AfxGetMainWnd();
HWND hndl = pWnd->GetSafeHwnd();

But i still get null .. Below is the data i see in the watch window.
pWnd 0x00000000 {CWnd hWnd=???}

I do not understand the reason yet .. Do we need to associate the CWnd
class to my testClass ??

Thanks
-Megha

Ajay

unread,
Jun 4, 2009, 9:40:52 AM6/4/09
to
On Jun 4, 12:46 am, MegK123 <MegK...@discussions.microsoft.com> wrote:
> Hello All,
>
> I have tried out the following code
>         AFX_MANAGE_STATE(AfxGetAppModuleState()) ;
>
>         CWnd* pWnd = AfxGetMainWnd();
>         HWND hndl = pWnd->GetSafeHwnd();
>
> But i still get null .. Below is the data i see in the watch window.
> pWnd    0x00000000 {CWnd hWnd=???}
>
> I do not understand the reason yet ..  Do we need to associate the CWnd
> class to my testClass ??

This is very puzzling. If you have a RegularDLL, by calling
AfxGetAppModuleState, you are changing the state to that of main app,
where main window resides. AFAIK, pWnd shouldnt be null. You dont need
to associate any CWnd to any testClass.

Just for kicks can you try the following:

AFX_MANAGE_STATE(AfxGetStaticModuleState()) ;

CWnd* pWnd = AfxGetMainWnd();

and note the value of pWnd.

--
Ajay

MegK123

unread,
Jun 5, 2009, 8:39:01 AM6/5/09
to

Hii Ajay ,

I have the already specified the value in the earlier post. It returns a
null.
Though I have seen the value in watch window , the changed value is
reflected, incase you have those concerns .

Thanks
-Megha

Ajay

unread,
Jun 5, 2009, 9:40:29 AM6/5/09
to
On Jun 5, 8:39 am, MegK123 <MegK...@discussions.microsoft.com> wrote:
> Though I have seen the value in watch window , the changed value is
> reflected, incase you have those concerns .

What does this mean?


I am just not sure about what your design is. Which module (dll or
exe) creates the mainframe? Is that in a MFC Regular DLL or exe or
static DLL?

--
Ajay

Tom Serface

unread,
Jun 5, 2009, 10:24:08 AM6/5/09
to
I still wonder if maybe you're seeing a race condition and trying to get the
mainframe value before the mainframe is created?

Also, have you tried the same code in the main module, perhaps at the in of
InitInstance(), just to stop in the debugger and see if it is working there?
You know it should work in IniInstance() after the mainframe is created so
that might be a revealing check.

Tom

"MegK123" <Meg...@discussions.microsoft.com> wrote in message

news:9B53D66B-E043-48B1...@microsoft.com...

Ajay

unread,
Jun 5, 2009, 11:48:18 AM6/5/09
to
On Jun 5, 10:24 am, "Tom Serface" <t...@camaswood.com> wrote:
> I still wonder if maybe you're seeing a race condition

Unless OP is doing multi-threading, this is not possible. Either OP
is accessing it before its assigned or its being done wrong. Its just
not likely to be a race condition.

--
Ajay

Scot T Brennecke

unread,
Jun 6, 2009, 3:56:45 AM6/6/09
to MegK123
Megha,
Ajay and Tom have been working on the assumption that your main .exe
was created with MFC, but nowhere have you said that. In fact, I have
seen you make reference to Java. So, my suspicion is that you do not
have an MFC main application, and there is no MFC main window to retrieve.

Scot

Ajay

unread,
Jun 6, 2009, 10:14:05 AM6/6/09
to
On Jun 6, 3:56 am, Scot T Brennecke <Sc...@Spamhater.MVPs.org> wrote:
> Megha,
>         Ajay and Tom have been working on the assumption that your main .exe
> was created with MFC, but nowhere have you said that.  In fact, I have
> seen you make reference to Java.  So, my suspicion is that you do not
> have an MFC main application, and there is no MFC main window to retrieve.

:-(

--
Ajay

0 new messages