CoCreateInstance returns 0x80080005 for Visual Studio 2008 based ATL service??

I just worked with a customer who was facing interesting issue. He was getting 0x80080005 at client when he calls CoCreateInstance. The COM component is implemented within an ATL Service which is running on the same machine (we are not hitting wire/network). Now, typical meaning of this error is,

CO_E_SERVER_EXEC_FAILURE                                      winerror.h
Server execution failed

Generally this means Server (out of process server, either stand alone EXE or service) did not respond to RPCSS within definite time about COM server's successful start. Many things could have happened (may be Server got some exception/error, or server got crashed!)

But in this case, even if I start the service before making call from client, I get same response from client. Something else was wrong! And after lots of checks here and there, we found out that it is a BUG!

The bug shows up when,

  • You create a brand new ATL Service project using Wizard with Visual Studio 2008.

The root cause is, newly generated CoClass's rgs file (that holds the component registration information) doesn't contain entry for AppID. So, even if you register the COM component (using <exename.exe> /service), you dont get AppID key generated for CoClass. And COM runtime returns this error at client.

To fix it,

  • Look at your rgs file (for each ATL simple objects you added to COM service project),

  • Replace snippet looking similar to following,

    ForceRemove {9ACB97C6-4492-40E8-A1BD-51DFE8962E92} = s 'CMyObject Class'

                {

                      ProgID = s 'ATLComService.CMyObject.1'

                      VersionIndependentProgID = s 'ATLComService.CMyObject'

                      ForceRemove 'Programmable'

                      LocalServer32 = s '%MODULE%'

                      'TypeLib' = s '{E423A7DE-58C7-4535-A864-395DCEDE941F}'

                }

    with 

                ForceRemove {9ACB97C6-4492-40E8-A1BD-51DFE8962E92} = s 'CMyObject Class'

                {

                      ProgID = s 'ATLComService.CMyObject.1'

                      VersionIndependentProgID = s 'ATLComService.CMyObject'

                      ForceRemove 'Programmable'

                      LocalServer32 = s '%MODULE%'

                      val AppID = s '%APPID%'

                      'TypeLib' = s '{E423A7DE-58C7-4535-A864-395DCEDE941F}'

                }

There is also another way to permanently fix this issue (Product Team is planning to fix this issue in Visual Studio 2008 SP1), you can also,

  • Go to C:\Program Files\Microsoft Visual Studio 9.0\VC\VCWizards\CodeWiz\ATL\Simple\Templates\1033\object.rgs

  • Add following line right before statement for "typelib"

    [!if APPID_EXIST]

    val AppID = s '[!output APPID_REGISTRY_FORMAT]'

    [!endif]

That should fix the issue.

Stay tuned.. Wave