473,785 Members | 2,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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::GCA boutDlg *dlg;
dlg = __gc new MFCTestApp::GCA boutDlg ();

#pragma pop_macro("new" )

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

mfc71d.dll!COle MessageFilter:: ~COleMessageFil ter() Line 59 + 0xf C++
mfc71d.dll!COle MessageFilter:: `scalar deleting destructor'() + 0xf C++
mfc71d.dll!AfxO leTerm(int bJustRevoke=0) Line 114 + 0x22 C++
mfc71d.dll!AfxO leTermOrFreeLib (int bTerm=1, int bJustRevoke=0) Line 139
C++
mfc71d.dll!AfxW inTerm() Line 48 C++
mfc71d.dll!AfxW inMain(HINSTANC E__ * hInstance=0x004 00000, HINSTANCE__ *
hPrevInstance=0 x00000000, char * lpCmdLine=0x001 41f2f, int nCmdShow=5) Line
64 C++
MFCTestApp.exe! WinMain(HINSTAN CE__ * hInstance=0x004 00000, HINSTANCE__ *
hPrevInstance=0 x00000000, char * lpCmdLine=0x001 41f2f, int nCmdShow=5) Line
25 C++
MFCTestApp.exe! WinMainCRTStart up() Line 390 + 0x39 C
00e1a09f()
mscorwks.dll!Co InitializeCor() + 0xbe77
mscorwks.dll!Co InitializeCor() + 0xbb94
mscorwks.dll!Co InitializeCor() + 0xbcea
mscorwks.dll!Co InitializeCor() + 0xbd56
mscorwks.dll!Co EEShutDownCOM() + 0x2105
mscorwks.dll!Dl lGetClassObject Internal() + 0x65ef
mscorwks.dll!Dl lGetClassObject Internal() + 0x66ee
mscorwks.dll!Dl lGetClassObject Internal() + 0x6bdc
mscorwks.dll!_C orDllMain() + 0x1f0
mscorwks.dll!_C orExeMain() + 0x47
kernel32.dll!_B aseProcessStart @4() + 0x23


-- MFC Source that is asserting
-- NOTE m_dwRef = 2
-- -------------------------------------------+
CCmdTarget::~CC mdTarget()
{
#ifndef _AFX_NO_OLE_SUP PORT
if (m_xDispatch.m_ vtbl != 0)
((COleDispatchI mpl*)&m_xDispat ch)->Disconnect() ;
ASSERT(m_dwRef <= 1);
#endif
#ifdef _AFXDLL
m_pModuleState = NULL;
#endif
}
Nov 17 '05 #1
6 4757
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.m icrosoft.com> wrote in message
news:Xu******** ******@cpmsftng xa10.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%40TK2MSFT NGP11.phx.gbl&r num=4&prev=/groups%3Fq%3DMF C%2B
WinForm%26hl%3D en%26lr%3Dlang_ en%26newwindow% 3D1%26c2coff%3D 1

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::GCA boutDlg *dlg;
dlg = __gc new MFCTestApp::GCA boutDlg ();

#pragma pop_macro("new" )

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

mfc71d.dll!COle MessageFilter:: ~COleMessageFil ter() Line 59 + 0xf C++
mfc71d.dll!COle MessageFilter:: `scalar deleting destructor'() + 0xf C++
mfc71d.dll!AfxO leTerm(int bJustRevoke=0) Line 114 + 0x22 C++
mfc71d.dll!AfxO leTermOrFreeLib (int bTerm=1, int bJustRevoke=0) Line 139
C++
mfc71d.dll!AfxW inTerm() Line 48 C++
mfc71d.dll!AfxW inMain(HINSTANC E__ * hInstance=0x004 00000, HINSTANCE__ *
hPrevInstance=0 x00000000, char * lpCmdLine=0x001 41f2f, int nCmdShow=5) Line
64 C++
MFCTestApp.exe! WinMain(HINSTAN CE__ * hInstance=0x004 00000, HINSTANCE__ *
hPrevInstance=0 x00000000, char * lpCmdLine=0x001 41f2f, int nCmdShow=5) Line
25 C++
MFCTestApp.exe! WinMainCRTStart up() Line 390 + 0x39 C
00e1a09f()
mscorwks.dll!Co InitializeCor() + 0xbe77
mscorwks.dll!Co InitializeCor() + 0xbb94
mscorwks.dll!Co InitializeCor() + 0xbcea
mscorwks.dll!Co InitializeCor() + 0xbd56
mscorwks.dll!Co EEShutDownCOM() + 0x2105
mscorwks.dll!Dl lGetClassObject Internal() + 0x65ef
mscorwks.dll!Dl lGetClassObject Internal() + 0x66ee
mscorwks.dll!Dl lGetClassObject Internal() + 0x6bdc
mscorwks.dll!_C orDllMain() + 0x1f0
mscorwks.dll!_C orExeMain() + 0x47
kernel32.dll!_B aseProcessStart @4() + 0x23


-- MFC Source that is asserting
-- NOTE m_dwRef = 2
-- -------------------------------------------+
CCmdTarget::~CC mdTarget()
{
#ifndef _AFX_NO_OLE_SUP PORT
if (m_xDispatch.m_ vtbl != 0)
((COleDispatchI mpl*)&m_xDispat ch)->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.d ll
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::ExitIn stance()
{
COleMessageFilt er* pFilter = AfxOleGetMessag eFilter();
ULONG lCount = pFilter->m_xMessageFilt er.Release();
if(lCount<=1)
pFilter->m_xMessageFilt er.AddRef();
return CWinApp::ExitIn stance();
}

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.m icrosoft.com> wrote in message
news:95******** *************** ***********@mic rosoft.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::GCA boutDlg *dlg;
dlg = __gc new MFCTestApp::GCA boutDlg ();

#pragma pop_macro("new" )

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

mfc71d.dll!COle MessageFilter:: ~COleMessageFil ter() Line 59 + 0xf C++
mfc71d.dll!COle MessageFilter:: `scalar deleting destructor'() + 0xf C++ mfc71d.dll!AfxO leTerm(int bJustRevoke=0) Line 114 + 0x22 C++
mfc71d.dll!AfxO leTermOrFreeLib (int bTerm=1, int bJustRevoke=0) Line 139 C++
mfc71d.dll!AfxW inTerm() Line 48 C++
mfc71d.dll!AfxW inMain(HINSTANC E__ * hInstance=0x004 00000, HINSTANCE__ * hPrevInstance=0 x00000000, char * lpCmdLine=0x001 41f2f, int nCmdShow=5) Line 64 C++
MFCTestApp.exe! WinMain(HINSTAN CE__ * hInstance=0x004 00000, HINSTANCE__ * hPrevInstance=0 x00000000, char * lpCmdLine=0x001 41f2f, int nCmdShow=5) Line 25 C++
MFCTestApp.exe! WinMainCRTStart up() Line 390 + 0x39 C
00e1a09f()
mscorwks.dll!Co InitializeCor() + 0xbe77
mscorwks.dll!Co InitializeCor() + 0xbb94
mscorwks.dll!Co InitializeCor() + 0xbcea
mscorwks.dll!Co InitializeCor() + 0xbd56
mscorwks.dll!Co EEShutDownCOM() + 0x2105
mscorwks.dll!Dl lGetClassObject Internal() + 0x65ef
mscorwks.dll!Dl lGetClassObject Internal() + 0x66ee
mscorwks.dll!Dl lGetClassObject Internal() + 0x6bdc
mscorwks.dll!_C orDllMain() + 0x1f0
mscorwks.dll!_C orExeMain() + 0x47
kernel32.dll!_B aseProcessStart @4() + 0x23


-- MFC Source that is asserting
-- NOTE m_dwRef = 2
-- -------------------------------------------+
CCmdTarget::~CC mdTarget()
{
#ifndef _AFX_NO_OLE_SUP PORT
if (m_xDispatch.m_ vtbl != 0)
((COleDispatchI mpl*)&m_xDispat ch)->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.microso ft.com/default.aspx?sc id=/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
2843
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 pass to it delegate to managed object method. I know how to do it from C# - DllImport etc. I suspect I can declare "stub" method using those attributes, so it will actually reference "C" method, and call this stub, letting marshaler to do the...
8
4525
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 problem and the potential for deadlocks. After pouring over the available information, and trying a few experiments, I am still left with a few questions and issues I hope someone out there can shed some light on.
4
40032
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 deals with managed in one place, and code that deals with unmanaged in another place, but I can't seem to find anything that clearly explains what that means. I think if I used a Windows API function to optain a handle, that handle would be an...
0
1608
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 that when I ran it and looked at memory usage (in Windows task manager) it looked as if there was a very slow leak. To isolate the issue:
1
1259
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 I make. In other words, it isn't actually calling the native method directly, right? So, when I create a variable based on a struct (such as POINT or COORD) in my Managed class, I can't just pass it in to the API call, right? It seems that...
6
1461
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 /clr compilation mode. Is my app in CLR, or only the call to the
7
1658
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 references to do not get invoked (I can observe this from both breakpoints in the code, and trace output to the console). I was under the impression that when my C# object goes out of scope, it would automatically dispose of all of the references...
8
1645
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. Actually, the first release of this 'port' is be a simple rebuild of the unmanaged C++ SDK in VS.NET. I have done this part already, using VS.NET 2003.
2
12360
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 classes in managed C++ as a wrapper to the unmanaged C++ classes or is there an easier way? Essentially what I have done is written a C++ kernel mode driver and I want to use it from my C# program. Because it requires some setup outside the...
0
9645
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9480
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10152
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9950
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8974
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6740
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5381
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5511
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3650
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.