By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,198 Members | 1,698 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,198 IT Pros & Developers. It's quick & easy.

MC++ Unmanaged MFC calling Managed WinForm class

P: n/a

I am thinking of porting an existing MFC application to MC++ and I have
created a simple MFC application to test the environment. My sample MFC
application is compilied with the /clr switch. I added a basic WinForm
class(via the wizard). In my MFC WinApp class I have a menu message handler
to display the standard About Dialog. In the message handler I create, show
and close the window. Everything appears to be working fine until I close
the application and I get an assertion about a COM ref not being released.

Thanks

--
+--------------------------------------+
William F. Kinsley
Sr. Design Engineer
NextGen Healthcare Info. Sys. Inc


// App command to run the dialog
void CMFCTestAppApp::OnAppAbout()
{

#pragma push_macro("new")
#undef new

MFCTestApp::GCAboutDlg *dlg;
dlg = __gc new MFCTestApp::GCAboutDlg ();

#pragma pop_macro("new")

dlg->ShowDialog();
delete dlg;
}
-- Call Stack
-- -------------------------------------------+
mfc71d.dll!CCmdTarget::~CCmdTarget() Line 48 + 0x19 C++

mfc71d.dll!COleMessageFilter::~COleMessageFilter() Line 59 + 0xf C++
mfc71d.dll!COleMessageFilter::`scalar deleting destructor'() + 0xf C++
mfc71d.dll!AfxOleTerm(int bJustRevoke=0) Line 114 + 0x22 C++
mfc71d.dll!AfxOleTermOrFreeLib(int bTerm=1, int bJustRevoke=0) Line 139
C++
mfc71d.dll!AfxWinTerm() Line 48 C++
mfc71d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ *
hPrevInstance=0x00000000, char * lpCmdLine=0x00141f2f, int nCmdShow=5) Line
64 C++
MFCTestApp.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ *
hPrevInstance=0x00000000, char * lpCmdLine=0x00141f2f, int nCmdShow=5) Line
25 C++
MFCTestApp.exe!WinMainCRTStartup() Line 390 + 0x39 C
00e1a09f()
mscorwks.dll!CoInitializeCor() + 0xbe77
mscorwks.dll!CoInitializeCor() + 0xbb94
mscorwks.dll!CoInitializeCor() + 0xbcea
mscorwks.dll!CoInitializeCor() + 0xbd56
mscorwks.dll!CoEEShutDownCOM() + 0x2105
mscorwks.dll!DllGetClassObjectInternal() + 0x65ef
mscorwks.dll!DllGetClassObjectInternal() + 0x66ee
mscorwks.dll!DllGetClassObjectInternal() + 0x6bdc
mscorwks.dll!_CorDllMain() + 0x1f0
mscorwks.dll!_CorExeMain() + 0x47
kernel32.dll!_BaseProcessStart@4() + 0x23


-- MFC Source that is asserting
-- NOTE m_dwRef = 2
-- -------------------------------------------+
CCmdTarget::~CCmdTarget()
{
#ifndef _AFX_NO_OLE_SUPPORT
if (m_xDispatch.m_vtbl != 0)
((COleDispatchImpl*)&m_xDispatch)->Disconnect();
ASSERT(m_dwRef <= 1);
#endif
#ifdef _AFXDLL
m_pModuleState = NULL;
#endif
}
Nov 17 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi William
I added a basic WinForm class(via the wizard).


Since a managed component could only be added into a managed project, how
do you add a WinForm class to a MFC project, even if you add the /clr
compiler switch to the project?
Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 17 '05 #2

P: n/a
From the VS2003 File menu Select Add New Item/Select Windows Form(.NET)

By using the /CLR switch, I am enabling the IJW(It Just Works) which is
suppose to allow the __nogc types to call into managed code and use managed
types. Unless I am missing something, one of the main advantages of using
MC++ is the ability to mix managed and unmanaged code in the same project. I
would also assume that the compiler would not compile/link or run if this
was not the case. My problem is that the application runs with out error
including loading and displaying the WinForm. But when the application
closes, it gets an assertion about a COM ref not being released.

Thanks
Bill
"Gary Chang[MSFT]" <v-******@online.microsoft.com> wrote in message
news:Xu**************@cpmsftngxa10.phx.gbl...
Hi William
I added a basic WinForm class(via the wizard).
Since a managed component could only be added into a managed project, how
do you add a WinForm class to a MFC project, even if you add the /clr
compiler switch to the project?
Thanks!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no

rights. --------------------

Nov 17 '05 #3

P: n/a
Hi William,
By using the /CLR switch, I am enabling the IJW(It Just Works) which is
suppose to allow the __nogc types to call into managed code and use managed types.


If you want to load the managed object in unmanaged code, it appears you
should use the COM Interop:
http://groups.google.com/groups?hl=e...2coff=1&thread
m=%236XbP676DHA.2656%40TK2MSFTNGP11.phx.gbl&rnum=4 &prev=/groups%3Fq%3DMFC%2B
WinForm%26hl%3Den%26lr%3Dlang_en%26newwindow%3D1%2 6c2coff%3D1

By the way, I still could not add a Winform class to my MFC project, would
you please upload a self-contain project(zipped) to us for repro the error?
Thanks for your understanding!

Best regards,

Gary Chang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
--------------------

Nov 17 '05 #4

P: n/a
Hi William,

I experienced the very same problem you have, the assertion failure in
CCmdTarget
destructor after closing the app. I've fixed this problem by forcing garbage
collection
right after the closing the dialog. Try the following

....
dlg->ShowDialog();
delete dlg;
GC::Collect();
....

Apparently some resources are not released on the exit and this causes
assertion to fail.

Regards,
Boris S

"William F. Kinsley" wrote:

I am thinking of porting an existing MFC application to MC++ and I have
created a simple MFC application to test the environment. My sample MFC
application is compilied with the /clr switch. I added a basic WinForm
class(via the wizard). In my MFC WinApp class I have a menu message handler
to display the standard About Dialog. In the message handler I create, show
and close the window. Everything appears to be working fine until I close
the application and I get an assertion about a COM ref not being released.

Thanks

--
+--------------------------------------+
William F. Kinsley
Sr. Design Engineer
NextGen Healthcare Info. Sys. Inc


// App command to run the dialog
void CMFCTestAppApp::OnAppAbout()
{

#pragma push_macro("new")
#undef new

MFCTestApp::GCAboutDlg *dlg;
dlg = __gc new MFCTestApp::GCAboutDlg ();

#pragma pop_macro("new")

dlg->ShowDialog();
delete dlg;
}
-- Call Stack
-- -------------------------------------------+
mfc71d.dll!CCmdTarget::~CCmdTarget() Line 48 + 0x19 C++

mfc71d.dll!COleMessageFilter::~COleMessageFilter() Line 59 + 0xf C++
mfc71d.dll!COleMessageFilter::`scalar deleting destructor'() + 0xf C++
mfc71d.dll!AfxOleTerm(int bJustRevoke=0) Line 114 + 0x22 C++
mfc71d.dll!AfxOleTermOrFreeLib(int bTerm=1, int bJustRevoke=0) Line 139
C++
mfc71d.dll!AfxWinTerm() Line 48 C++
mfc71d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ *
hPrevInstance=0x00000000, char * lpCmdLine=0x00141f2f, int nCmdShow=5) Line
64 C++
MFCTestApp.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ *
hPrevInstance=0x00000000, char * lpCmdLine=0x00141f2f, int nCmdShow=5) Line
25 C++
MFCTestApp.exe!WinMainCRTStartup() Line 390 + 0x39 C
00e1a09f()
mscorwks.dll!CoInitializeCor() + 0xbe77
mscorwks.dll!CoInitializeCor() + 0xbb94
mscorwks.dll!CoInitializeCor() + 0xbcea
mscorwks.dll!CoInitializeCor() + 0xbd56
mscorwks.dll!CoEEShutDownCOM() + 0x2105
mscorwks.dll!DllGetClassObjectInternal() + 0x65ef
mscorwks.dll!DllGetClassObjectInternal() + 0x66ee
mscorwks.dll!DllGetClassObjectInternal() + 0x6bdc
mscorwks.dll!_CorDllMain() + 0x1f0
mscorwks.dll!_CorExeMain() + 0x47
kernel32.dll!_BaseProcessStart@4() + 0x23


-- MFC Source that is asserting
-- NOTE m_dwRef = 2
-- -------------------------------------------+
CCmdTarget::~CCmdTarget()
{
#ifndef _AFX_NO_OLE_SUPPORT
if (m_xDispatch.m_vtbl != 0)
((COleDispatchImpl*)&m_xDispatch)->Disconnect();
ASSERT(m_dwRef <= 1);
#endif
#ifdef _AFXDLL
m_pModuleState = NULL;
#endif
}

Nov 17 '05 #5

P: n/a
Thank you Boris, that resolved my issue. After I tried your solution, I
started researching this I came across another posting in the csaharp
languages news group that describes this as an issue in windows.forms.dll
assembly and that this problem will be fixed in the next version of .NET
Framework. , it appears that this issue can be addressed from either the
managed code side and the unmanaged code side.

Your solution (and the one I am using) works from the managed side of the
fence:
GC::Collect();

And a different approach addresses it from the unmanaged side of the fence:
BOOL CMFCApp::ExitInstance()
{
COleMessageFilter* pFilter = AfxOleGetMessageFilter();
ULONG lCount = pFilter->m_xMessageFilter.Release();
if(lCount<=1)
pFilter->m_xMessageFilter.AddRef();
return CWinApp::ExitInstance();
}

either one of these worked for me. I don't know if anyone can comment if one
is better then the other, but thanks for your help.
Bill
"Boris S" <Boris S@discussions.microsoft.com> wrote in message
news:95**********************************@microsof t.com...
Hi William,

I experienced the very same problem you have, the assertion failure in
CCmdTarget
destructor after closing the app. I've fixed this problem by forcing garbage collection
right after the closing the dialog. Try the following

...
dlg->ShowDialog();
delete dlg;
GC::Collect();
...

Apparently some resources are not released on the exit and this causes
assertion to fail.

Regards,
Boris S

"William F. Kinsley" wrote:

I am thinking of porting an existing MFC application to MC++ and I have
created a simple MFC application to test the environment. My sample MFC
application is compilied with the /clr switch. I added a basic WinForm
class(via the wizard). In my MFC WinApp class I have a menu message handler to display the standard About Dialog. In the message handler I create, show and close the window. Everything appears to be working fine until I close the application and I get an assertion about a COM ref not being released.
Thanks

--
+--------------------------------------+
William F. Kinsley
Sr. Design Engineer
NextGen Healthcare Info. Sys. Inc


// App command to run the dialog
void CMFCTestAppApp::OnAppAbout()
{

#pragma push_macro("new")
#undef new

MFCTestApp::GCAboutDlg *dlg;
dlg = __gc new MFCTestApp::GCAboutDlg ();

#pragma pop_macro("new")

dlg->ShowDialog();
delete dlg;
}
-- Call Stack
-- -------------------------------------------+
mfc71d.dll!CCmdTarget::~CCmdTarget() Line 48 + 0x19 C++

mfc71d.dll!COleMessageFilter::~COleMessageFilter() Line 59 + 0xf C++
mfc71d.dll!COleMessageFilter::`scalar deleting destructor'() + 0xf C++ mfc71d.dll!AfxOleTerm(int bJustRevoke=0) Line 114 + 0x22 C++
mfc71d.dll!AfxOleTermOrFreeLib(int bTerm=1, int bJustRevoke=0) Line 139 C++
mfc71d.dll!AfxWinTerm() Line 48 C++
mfc71d.dll!AfxWinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00141f2f, int nCmdShow=5) Line 64 C++
MFCTestApp.exe!WinMain(HINSTANCE__ * hInstance=0x00400000, HINSTANCE__ * hPrevInstance=0x00000000, char * lpCmdLine=0x00141f2f, int nCmdShow=5) Line 25 C++
MFCTestApp.exe!WinMainCRTStartup() Line 390 + 0x39 C
00e1a09f()
mscorwks.dll!CoInitializeCor() + 0xbe77
mscorwks.dll!CoInitializeCor() + 0xbb94
mscorwks.dll!CoInitializeCor() + 0xbcea
mscorwks.dll!CoInitializeCor() + 0xbd56
mscorwks.dll!CoEEShutDownCOM() + 0x2105
mscorwks.dll!DllGetClassObjectInternal() + 0x65ef
mscorwks.dll!DllGetClassObjectInternal() + 0x66ee
mscorwks.dll!DllGetClassObjectInternal() + 0x6bdc
mscorwks.dll!_CorDllMain() + 0x1f0
mscorwks.dll!_CorExeMain() + 0x47
kernel32.dll!_BaseProcessStart@4() + 0x23


-- MFC Source that is asserting
-- NOTE m_dwRef = 2
-- -------------------------------------------+
CCmdTarget::~CCmdTarget()
{
#ifndef _AFX_NO_OLE_SUPPORT
if (m_xDispatch.m_vtbl != 0)
((COleDispatchImpl*)&m_xDispatch)->Disconnect();
ASSERT(m_dwRef <= 1);
#endif
#ifdef _AFXDLL
m_pModuleState = NULL;
#endif
}

Nov 17 '05 #6

P: n/a
Hi William,

I think you refered to one post by Leo Chen before. :)

Both of these two ways are OK. I feel that unmanaged one is more proactive
for releasing the resource. For GC::Collect(), the release time is judged
by framework.

Thanks.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! C www.microsoft.com/security
Register to Access MSDN Managed Newsgroups!
-http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.as
p&SD=msdn

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.