Saturday, March 20, 2010

CoCreateInstance returns CO_E_SERVER_EXEC_FAILURE (0x80080005) for ATL service created using Microsoft Visual Studio 2008

Create an ATL service project using Microsoft Visual Studio 2008. If you try to create instance of an object which is hosted by the service, CoCreateInstance returns error 0x80080005. The error means server execution failed.


It took lot of time to resolve this error and was quite frustrating as the error message is generic and does not give any clue about what could have gone wrong.
I tried creating a service which has one COM object and has a foo() method which does nothing and just returns. The CoCreateInstance was failing for this object as well with same error code.


While googgling for the error code, I came across an article which talks about a defect in Visual Studio 2008 wizard generated registry files. http://blogs.msdn.com/jigarme/archive/2008/05/08/cocreateinstance-returns-0x80080005-for-visual-studio-2008-based-atl-service.aspx

The APPID entry is missing from the wizard generated .rgs file. The error got fixed after adding the APPID entry in the .rgs file

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}'
}

Saturday, March 6, 2010

GetRecordInfoFromTypeInfo and GetRecordInfoFromGuids returns E_INVALIDARG if COM structure contains a GUID member field

Recently I was working on developing a COM service which returns a SAFERRAY of structure as out parameter. This structure has member of type GUID.

To create SAFEARRAY of custom data types, you need to get the IRecordInfo pointer to the structure and pass it to SafeArrayCreateEx. You can get IRecordInfo pointer to the custom structure by calling either GetRecordInfoFromTypeInfo or GetRecordInfoFromGuids API.

Both of these API returns E_INVALIDARG if the sturcture contains member of type GUID. One way to workaround this problem is to convert GUID as BSTR.

This is a defect in Microsoft Visual Studio 2008 and hopefully will get fixed in next versions. Link to the microsoft page for same. https://connect.microsoft.com/VisualStudio/feedback/details/380266/getrecordinfofromtypeinfo-returns-e-invalidarg-if-a-com-structure-contains-a-guid-field