473,395 Members | 2,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

MC++ Unmanaged MFC calling Managed WinForm class


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
6 4732
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Alexander Arlievsky | last post by:
Hi, I have mixed mode dll, which contains files with managed objects written in MC++, and files with regular "C" functions. One of those functions receives function pointer as parameter. I want to...
8
by: Ted Miller | last post by:
Hi folks, I'm looking at moving a large base of C++ code to .Net under tight time constraints. The code runs in mission-critical environments, and I am extremely concerned about the loader lock...
4
by: Rachel Suddeth | last post by:
What is the difference between a managed/unmanaged resource, and how do you tell which is which? I'm trying to understand how to write some Dispose() methods, and we are supposed to put code that...
0
by: Maxwell | last post by:
Hello, I recently completed a MC++ (VS2003) DLL that wraps a non MFC C++ DLL and need to use it in a MC++ Console Application (no forms/guis of any kind just output to console). Trouble is...
1
by: Shawn B. | last post by:
Greetings, With a Managed class, if I'm #including a Windows SDK header file, and call an API, it appears (according to .NET Reflector) that it automatically generates a for each API call that...
6
by: nicolas.hilaire | last post by:
Hi all, i'm not totally clear with some concepts about managed and unmanaged code. I'm asking myself some questions : - i've a MFC app, i want to use the framework dotnet by switching to...
7
by: Jeremy Chaney | last post by:
I have an application written in C# that uses objects written in a managed C++ DLL. When I exit my app, my C# classes have their destructors called, but the MC++ objects that those classes hold...
8
by: STG | last post by:
Greetings, My group has an SDK that was developed 5 years ago with VC++ 6. Over the last years, the requests for a VS.NET SDK has reached critical mass and I am now in the process of doing that....
2
by: Jon Slaughter | last post by:
How difficult is it for one to integrate unmanaged C++ into C#? I know for functions one can use DLLimport but how does one go about doing it for classes? Do I have to completely reimplement the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.