MC++ Unmanaged MFC calling Managed WinForm class

  • Thread starter Thread starter William F. Kinsley
  • Start date Start date
W

William F. Kinsley

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
}
 
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.
--------------------
 
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
 
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=en&lr=lang_en&newwindow=1&c2coff=1&thread
m=%236XbP676DHA.2656%40TK2MSFTNGP11.phx.gbl&rnum=4&prev=/groups%3Fq%3DMFC%2B
WinForm%26hl%3Den%26lr%3Dlang_en%26newwindow%3D1%26c2coff%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.
--------------------
 
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
 
Hi William,

I found one community member Boris has posted a valuable suggestion to your
problem yesterday, do you have tried it ?
If you have any more concerns on it, please feel free to post here.


Good Luck!

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.
--------------------
 
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
 
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.
 
where can i put the /CLR to use windows form in my MFC application.
please can any one explain these steps.
Thanks

Fromhttp://www.developmentnow.com/g/42_2004_10_0_0_23540/MC-Unmanaged-MFC....

Posted via DevelopmentNow.com Groupshttp://www.developmentnow.com/g/

If I understand your question correctly, you should right click on
your project and select properties. On the properties pop-up. choose
Configuration properties, you should see on your right a line "Common
LAnguage Runtime support", you can select /clr option there. You also
have to add
#include <afxwinforms.h> in your stdafx.h, if you want to call Windows
Form controls in your MFC app.

Hope it help,
Tom
 
Back
Top