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
> 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
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
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
You can use macro AFX_MANAGE_STATE (w/getappmodule) to make it work
the way you want it.
--
Ajay
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.
"David Webber" <da...@musical-dot-demon-dot-co.uk> wrote in message
news:OcCcAMF4...@TK2MSFTNGP02.phx.gbl...
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...
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
>
> 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
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..
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
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
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
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
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
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...
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
:-(
--
Ajay