Microsoft KB Archive/824480

= BUG: &quot;HRESULT - 0x80010106&quot; error occurs when you run a managed Visual C++ application in Visual Studio .NET =

Article ID: 824480

Article Last Modified on 1/5/2006

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition

-





SYMPTOMS
When you try to run a Managed C++ application that has Component Object Model (COM) initialization on the main thread, you may receive the following error message at run time:

HRESULT - 0x80010106 - Cannot change thread mode after it is set.



CAUSE
Setting the apartment state on the main thread is not reliable.



RESOLUTION
The main function is not the real managed entry point. The real managed entry point is the C Run-Time Libraries (CRT) entry point. To resolve this problem, write the managed entry point and manually initialize the CRT as follows:
 * 1) define _WIN32_WINNT 0x501
 * 2) include 
 * 3) include 

using namespace System;
 * 1) using 

extern &quot;C&quot; void mainCRTStartup;

[System::STAThread] int mymain //the new entry point {   //Initialize COM HRESULT hr = CoInitialize(0); //Initialize the CRT mainCRTStartup; //uninit CoUninitialize; return 0; }

int main {   /*   Do the application work that you would do in your main thread */ } Explicitly specify the entry point to the mymain function. To do this, follow these steps:
 * 1) Click Project, and then click Properties.
 * 2) In the Property Pages dialog box, expand Linker under Configuration Properties, and then click to select Advanced.
 * 3) In the Entry Point text box, type mymain, and then click OK.



STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.



MORE INFORMATION
When the common language runtime must use the Component Object Model (COM) any time before the user application starts, the common language runtime calls the CoInitializeEx method to initialize the main thread to the multi-threaded apartment (MTA). However, the common language runtime calls the CoInitializeEx method only as required. The common language runtime is not deterministic. Typically, applications that require a single-threaded apartment (STA) for the main thread must instead apply the STAThread attribute at the entry point. Also, in C++ the user entry point (main method and its variants) is not a true managed entry point. The C Run-Time Libraries supply the entry points for console applications and for windows applications.

Steps to Reproduce the Behavior 1

 * 1) Start Microsoft Visual Studio .NET 2003.
 * 2) On the File menu, point to New, and then click Project.
 * 3) Expand Visual C++ Projects under Project Types, click MFC, and then click MFC Application under Templates.
 * 4) Type MFCTest in the Name text box, type C:\ in the Location text box, and then click OK.
 * 5) In the MFC Application wizard, click Compound Document Support.
 * 6) Under Compound Document Support, click to select Container/Full server, and then click Finish.
 * 7) In Solution Explorer, right-click MFCTest, and then click Properties.
 * 8) In the Property Pages dialog box, expand C/C++ under Configuration Properties.
 * 9) Set the Debug Information Format property to Disabled.
 * 10) Expand Code Generation, and then set the following properties:
 * 11) * Set the Basic Runtime Checks property to Default.
 * 12) * Set the Enable Minimal Rebuild property to No.
 * 13) Expand Command Line, and then type /clr in the Additional Options text box.
 * 14) Click Apply, and then click OK.
 * 15) Build the application.
 * 16) On the Debug menu, click Start to debug the application. You may receive the error message that is described in the &quot;Symptoms&quot; section of this article.

Steps to Reproduce the Behavior 2
 Start Visual Studio .NET 2003. On the File menu, point to New, and then click Project. Expand Visual C++ Projects under Project Types, click .NET, and then click Console Application (.NET) under Templates. Type TestProject in the Name text box, type C:\ in the Location text box, and then click OK.</li>  In Solution Explorer, double-click TestProject.cpp, and then replace the existing code with the following code:
 * 1) include &quot;stdafx.h&quot;
 * 2) define _WIN32_WINNT 0x501
 * 3) include <objbase.h>
 * 4) include <stdio.h>

using namespace System;
 * 1) using <mscorlib.dll>

int main

{   HRESULT hr = CoInitialize(0); printf(&quot;hr in main thread is %d \n&quot;,hr); } </li> Build, and then run the project. You may receive the run-time error that is described in the &quot;Symptoms&quot; section of this article.</li></ol>

This problem may also occur in Managed C++ applications under the following scenarios:
 * When the application must call the CoInitializeEx method to initialize the main thread to the STA.
 * When you compile MFC OLE based applications by using the /clr option.
 * When you run a Wizard-generated Windows Forms application.

Keywords: kberrmsg kbbug kbappwizard kbcrt kbthread kbcodegen KB824480

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.