Converting a 6.0 MFC app to Visual Studio 2008 (quantum leap)

Hi Experts,

I'm getting an error when the call reaches OleInitialize from the First call to AfxOleInit().  The error is RPC_E_CHANGED_MODE ($80010106).  What does this mean?  I think it has something to do with threading models - which are very mysterious and ominous to me...

Thanks for any help!
Mike
BOOL CMyApp::InitInstance()
{
	if (!InitATL())
		return FALSE;
	
	if(!AfxOleInit())  // Your addition starts here
	{
		AfxMessageBox("Could not initialize COM dll");
		return FALSE;
	}                 // End of your addition
 
....
 
BOOL CMyApp::InitATL()
{
	m_bATLInited = TRUE;
	
#if _WIN32_WINNT >= 0x0400
	HRESULT hRes = CoInitializeEx(NULL, COINIT_MULTITHREADED);
#else
	HRESULT hRes = CoInitialize(NULL);
#endif
	
	if (FAILED(hRes))
	{
		m_bATLInited = FALSE;
		return FALSE;
	}
	
	_Module.Init(ObjectMap, AfxGetInstanceHandle());
	_Module.dwThreadID = GetCurrentThreadId();
	
	LPTSTR lpCmdLine = GetCommandLine(); //this line necessary for _ATL_MIN_CRT
	TCHAR szTokens[] = _T("-/");
	
	BOOL bRun = TRUE;
	LPCTSTR lpszToken = _Module.FindOneOf(lpCmdLine, szTokens);
	while (lpszToken != NULL)
	{
		if (lstrcmpi(lpszToken, _T("UnregServer"))==0)
		{
			_Module.UpdateRegistryFromResource(IDR_MBS, FALSE);
			_Module.UnregisterServer(TRUE); //TRUE means typelib is unreg'd
			bRun = FALSE;
			break;
		}
		if (lstrcmpi(lpszToken, _T("RegServer"))==0)
		{
			_Module.UpdateRegistryFromResource(IDR_MBS, TRUE);
			_Module.RegisterServer(TRUE);
			bRun = FALSE;
			break;
		}
		lpszToken = _Module.FindOneOf(lpszToken, szTokens);
	}
	
	if (!bRun)
	{
		m_bATLInited = FALSE;
		_Module.Term();
		CoUninitialize();
		return FALSE;
	}
	
	hRes = _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER, 
		REGCLS_MULTIPLEUSE);
	if (FAILED(hRes))
	{
		m_bATLInited = FALSE;
		CoUninitialize();
		return FALSE;
	}	
	
	return TRUE;
	
}

Open in new window

LVL 1
threadyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

threadyAuthor Commented:
ok - let me rephrase the question - I realized it is because of the apartment thingamagig.  I changed my CoInitializeEx function to the following and my code works now:
      HRESULT hRes = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); //(NULL, COINIT_MULTITHREADED);

The question is - why is it now single apartment thread now - and what are the consequences of this?  Should I be concerned?  Why was it working in 6.0 for a long time - and now in 2008 it needs to be single threaded?  Thanks again!
Mike
0
primeMover2004Commented:
To answer your original question:

The problem arises from the fact that you first call

CoInitializeEx(NULL, COINIT_MULTITHREADED);

Within AfxOleInit() there's another call to CoInitialize.
Don't do that :) That's where the RPC_E_CHANGED_MODE comes from.

Furthermore MFC applications *must* be initialized as single threaded apartment (STA)
(CoInitialize(NULL))


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
threadyAuthor Commented:
If you say so!  (any reason why they must be?  just so I understand these apartments)?
0
primeMover2004Commented:
Also the help says so :)
http://msdn2.microsoft.com/en-us/library/ms678543.aspx

And from my experience, this is true ;)

You can only initialize a thread to live in a specific department once, the first time, that is.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.