Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / VC10.0

COM Executable Crash compiled with VS2012 on Windows 2012

5.00/5 (3 votes)
19 Apr 2013CPOL 12.1K  
If you are migrating your COM application to Visual Studio 2012 then this could be helpful for you.

I faced this problem while migrating one of my COM applications from VS2008 to VS2012.

Problem: COM application crashes when compiled with VS2012 on Windows 2012

  1. Search for a class like below in your code which inherits from CAfwAtlExeModuleT<>.
  2. C++
    class CAsset_Tree_Module : public CAfwAtlExeModuleT< CAsset_Tree_Module >
  3. You might see a method similar to the one below in that class (if yes than there is a trap):
  4. C++
    HRESULT RegisterClassObjects(DWORD dwClsContext, DWORD dwFlags) throw()
    {
        return AtlComModuleRegisterClassObjects(&_AtlComModule, CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE);
    }
  5. As per Microsoft, in Visual Studio 2012, this function cannot be used in applications that execute in the Windows Runtime.
  6. http://msdn.microsoft.com/en-us/library/vstudio/hd3ht4xt%28v=vs.100%29.aspx

Just change the version of Visual Studio (2010 to 2012) in the above link to see the difference. 

Here is the solution:

Replace this line:

C++
return AtlComModuleRegisterClassObjects(&_AtlComModule, CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE);

with this code block:

C++
#if _MSC_VER > 1600
    dwFlags &= ~REGCLS_MULTIPLEUSE;
    dwFlags |= REGCLS_SINGLEUSE;
    return __super::RegisterClassObjects(dwClsContext,dwFlags);

#else
    return AtlComModuleRegisterClassObjects(&_AtlComModule, 
                  CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE);
#endif

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)