473,386 Members | 1,830 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,386 software developers and data experts.

Problem using OCX in Mixed MFC & Managed App

Hello

I have a VC7 App consisting of "old" MFC-Source and new Manged Source that uses an OC
in one of its MFC-based dialogs
When started, AfxOleInit (called in InitInstance) fails with a message that "OleInitialize returne
scode RPC_E_CHANGED_MODE"
The documentation says

RPC_E_CHANGED_MODE
A previous call to CoInitializeEx specified the concurrency model for this apartment as multithread apartment (MTA).

I guess, that some CLR - related Code has done this (because the same App without /CLR-Optio
and managed Sources does not have this problem

If I comment out the OleInitialize in InitInstance, then the Message disappeares and all seem
to be OK until a MFC-based dialog which contains an OCX is created
At this point an assertion occurs in occmgr.cpp COccManager::CreateDlgControls at (Line 410

COleControlSiteOrWnd *pTemp =new COleControlSiteOrWnd(::GetDlgItem(pWndParent->GetSafeHwnd(),
pOccDlgInfo->m_pItemInfo[i].nId),pOccDlgInfo->m_pItemInfo[i].bAutoRadioButton)
ASSERT(pTemp->m_hWnd)
How can this problem be solved so that MFC-Source with Dialogs using OCXes can be mixe
with managed C++ source in one App

Thanks in advance for any hint .

Werne

Nov 17 '05 #1
4 6861
Hi Werner,
If I comment out the OleInitialize in InitInstance, then the Message disappeares and all seems
to be OK until a MFC-based dialog which contains an OCX is created.
At this point an assertion occurs in occmgr.cpp
COccManager::CreateDlgControls at (Line 410)
"
COleControlSiteOrWnd *pTemp =new
COleControlSiteOrWnd(::GetDlgItem(pWndParent->GetSafeHwnd(),
pOccDlgInfo->m_pItemInfo[i].nId),pOccDlgInfo->m_pItemInfo[i].bAutoRadioButto
n);
ASSERT(pTemp->m_hWnd);
"
The OleInitialize could not be commented out in InitInstance,and it is true
that your MFC ocx controls should be loaded in a STA thread.

I have a VC7 App consisting of "old" MFC-Source and new Manged Source that uses an OCX
in one of its MFC-based dialogs.
...
I guess, that some CLR - related Code has done this (because the same App
without /CLR-Option
and managed Sources does not have this problem.


Which project type of your VC7 application?
Based on your description, it appears that its main thread is a MTA thread
when complied as managed code, if so, how about call your MFC dialog(with
ocx control) in a child thread which resides in a STA?
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
Hi Gary

thank you very much for your response
Here are the answers to your questions (and my new
questions resulting from your answers ..
Which project type of your VC7 application?
It is an MFC-EXE-App project, created with VC6 (dsw/dsp) and then automatically upgrade
by VS .NET 2003 to VC7 (sln/vcproj
Based on your description, it appears that its main thread is a MTA thread when complied as managed cod

My MFC source itself is not multithreaded; it never starts any child threads an
it never calls CoInitializeEx with COINIT_MULTITHREADED mode
so I don't know why the main thread is a MTA thread
does "/CLR" make it a MTA thread ?
or the usage of .NET Framework classes

Is it possible to make the main thread an STA thread again (as it was before I change
the project to "/CLR" mode and used .NET Framework classes) ?
If yes, how
if so, how about call your MFC dialog(with

ocx control) in a child thread which resides in a STA

how can this be done that my (complete) MFC-App-Source would ru
in a child thread which resides in a STA

Thanks in advance for your answers

Werne

Nov 17 '05 #3
Hi Werner,

Thanks for the quickly reply!

After reviewing your messages carefully, now I can confirm the problem is a
known issue to the VS.NET 2003, you can find the resolution in the
following KB:

BUG: "HRESULT - 0x80010106" Error When You Run a Managed C++ Application
http://support.microsoft.com/?id=824480

However, for a MFC application, the managed entry point function should be
modified as:
//in Appname.cpp
...
#define _WIN32_WINNT 0x501 //for Windows2000, please use 0x0500
#include <objbase.h>
#include <stdio.h>

#using <mscorlib.dll>
using namespace System;

extern "C" void WinMainCRTStartup();

[System::STAThread]
int mymain() //the new entry point
{
//Initialize COM
HRESULT hr = CoInitialize(0);

//Initialize the CRT
WinMainCRTStartup();
//uninit
CoUninitialize();
return 0;
}
...

so I don't know why the main thread is a MTA thread;
does "/CLR" make it a MTA thread ?
or the usage of .NET Framework classes ? Please refer to the above KB.
Is it possible to make the main thread an STA thread again (as it was before I changedthe project to "/CLR" mode and used .NET Framework classes)? We don't recommend make the main thread an STA thread again, you can make
it an STA thread from the beginning, please check the KB.
how can this be done that my (complete) MFC-App-Source would run in a child thread which resides in a STA?

For the reason that your project is a MFC application project upgraded from
the VC6, you needn't take such an approach.
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 #4
Hi Gary

the code snippet from your last post inserted in my <Appname>.cpp solved the problem

Thank you very much

(I had searched the KB for this problem, but did'nt find KB 824480, because I searche
for RPC_E_CHANGED_MODE and AfxOleInit

Best regards

Werne

Nov 17 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nadav | last post by:
Hi, I am writing a performence critical application, this require me to stick to unmanaged C++ as performance is much better using unmanaged C++ ( about 33% better ), Still, I am trying to avoid...
4
by: | last post by:
I am stuck in a situation and I do believe that this should work, but it doesn't. I have a unmanaged dll, that uses MFC. This works great. Now I recompile the unmanaged dll so it contains...
1
by: HMANG | last post by:
I have a c# application that calls a mixed code dll which in turn calls an unmanaged dll that has an inline template class constructor. The c# application runs fine until I use it on IIS The...
4
by: quortex | last post by:
Hey all, I have been working on a project which consists of Native C++ code and managed C# code. In order for the managed C# code to be able to access the native layer I wrap some objects using...
3
by: Mali Guven | last post by:
Hello, I have a native DLL (written in C) which is supposed to call a managed DLL (was written in C#, and an entry point was injected modifying the ildasm'd code). The exectuable calls the...
1
by: Edward Diener | last post by:
In Managed C++ in order to share the functionality of a class between its CLR code and native C++ code, in essence mixed mode programming for a class, I would design the __gc class so its...
4
by: Al Norman | last post by:
We have two separate DLLs that do not interact (directly, at least). One is an MFC extension DLL that was built back in VC++ 6 days (but has been recompiled with VS2005). The other is a DLL that...
0
by: Arne Adams | last post by:
Hi all, i have a managed c++ dll using both managed and unmanaged code and an unmanaged application that calls the unmanaged code of the managed dll. I did follow...
1
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I want to give multiple native classes (in different mixed mode dlls) access to a managed output window (for error messages). Therefore I wrote a native singleton with __declspec...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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...

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.