Question QueryInterface fails in windows service for an interface of type IUnkown

Joseph Raj

New member
Joined
Jul 9, 2009
Messages
1
Programming Experience
1-3
There is an external COM library with threading model APARTMENT. There is no proxy/stub dll present for this library.
The windows service works for IDispatch interface methods present in this library but not for interface of type IUnknown.


The following are the steps followed:
1. Add Reference to the COM library (QsCom.dll) which gives a reference to Interop.QSCOMLib
2. The code fragment which produced the error is given below:
Imports QSCOMLib

Try
Dim mainapiobj As New QSCOMLib.MainAPI
ret = mainapiobj.RGetSystemString(QSCOMLib.RGetSystemString_Topics.RSYS_MACHINEID, IDString)
Catch ex As Exception
WriteToLog(ex.Message)
WriteToLog(ex.InnerException.ToString)
End Try

2. The IDL definition of the interface and the coclass are given below:

[
odl,
uuid(AF0DEB30-1D4C-11D1-9E93-00A0C9050615),
helpstring("IMainAPI Interface")
]
interface IMainAPI : IUnknown {
[helpstring("method RComOpen")]
HRESULT _stdcall RComOpen(
[in] IDispatch* Target,
[in] BSTR Routername,
[in] BSTR RouterFile,
[in] long lVersion,
[out, retval] long* lError);
[helpstring("method RComClose")]
HRESULT _stdcall RComClose(
[in] IDispatch* pTarget,
[out, retval] long* lError);
[helpstring("method RLogin")]
HRESULT _stdcall RLogin(
[in] BSTR User,
[in] BSTR TimeZone,
[in] BSTR AppName,
[in] BSTR AppVer,
[in] BSTR Key,
[out, retval] long* lError);
[helpstring("method RGetVendorCode")]
HRESULT _stdcall RGetVendorCode(
[in] long lVendor,
[out] BSTR* pVenCode,
[out, retval] long* lError);
[helpstring("method RGetVendorName")]
HRESULT _stdcall RGetVendorName(
[in] long lVendor,
[out] BSTR* pVenName,
[out, retval] long* lError);
[helpstring("method RGetVendorId")]
HRESULT _stdcall RGetVendorId(
[in] BSTR Vendor,
[in] long lByType,
[out] long* lVendor,
[out, retval] long* lError);
[helpstring("method RGetSource")]
HRESULT _stdcall RGetSource(
[in] long lVendor,
[in] long lSource,
[out] BSTR* pBuf,
[out, retval] long* lError);
[helpstring("method RGetSourceFullName")]
HRESULT _stdcall RGetSourceFullName(
[in] BSTR Code,
[out] BSTR* pName,
[out, retval] long* lError);
[helpstring("method RGetSourceInfo")]
HRESULT _stdcall RGetSourceInfo(
[in] long lByType,
[in, out] Source_Desc* pSourceDesc,
[out, retval] long* lError);
[helpstring("method RGetSystemVal")]
HRESULT _stdcall RGetSystemVal(
[in] long lTopic,
[in, out] long* lStatus,
[out, retval] long* lError);
[helpstring("method RSrvFileOpen")]
HRESULT _stdcall RSrvFileOpen(
[in] IDispatch* Target,
[in] long Tag,
[in] BSTR Vendor,
[in] BSTR Source,
[in] BSTR File,
[in] long lLoc,
[in] long lType,
[in] long lOpts,
[out, retval] long* lError);
[helpstring("method RSrvGetData")]
HRESULT _stdcall RSrvGetData(
[in] BSTR Rid,
[in, out] unsigned char* pBuf,
[in, out] long* lOffSet,
[in] long lLen,
[out, retval] long* lError);
[helpstring("method RSrvFileClose")]
HRESULT _stdcall RSrvFileClose(
[in] IDispatch* Target,
[in] long Tag,
[in] BSTR Rid,
[out, retval] long* lError);
[helpstring("reserved method")]
HRESULT _stdcall RLockDLL(
[in] BSTR LibName,
[out, retval] long* lError);
[helpstring("method utilDoubleToString")]
HRESULT _stdcall utilDoubleToString(
[in] double dVal,
[in] long lFrac,
[out] BSTR* pString,
[out, retval] long* lError);
[helpstring("method utilValToDouble")]
HRESULT _stdcall utilValToDouble(
[in] long lVal,
[in] long lFrac,
[out] double* dVal,
[out, retval] long* lError);
[helpstring("method utilFormatString")]
HRESULT _stdcall utilFormatString(
[in] long lVal,
[in] long lFmt,
[in] long lFrac,
[out] BSTR* pString,
[out, retval] long* lError);
[helpstring("method utilFormatString2")]
HRESULT _stdcall utilFormatString2(
[in] double dVal,
[in] long lFmt,
[in] long lFrac,
[out] BSTR* pString,
[out, retval] long* lError);
[helpstring("method utilFormatDate")]
HRESULT _stdcall utilFormatDate(
[in] long lVal,
[in] long lFmt,
[out] BSTR* pString,
[out, retval] long* lError);
[helpstring("method utilFormatTime")]
HRESULT _stdcall utilFormatTime(
[in] long lVal,
[in] long lFmt,
[out] BSTR* pString,
[out, retval] long* lError);
[helpstring("method utilGetCurDateNum")]
HRESULT _stdcall utilGetCurDateNum(
[in, out] Date_Struct* pDateStruct,
[out, retval] long* lDateNum);
[helpstring("method utilGetDateNum")]
HRESULT _stdcall utilGetDateNum(
[in, out] Date_Struct* pDateStruct,
[out, retval] long* lDateNum);
[helpstring("method utilDoubleToVal")]
HRESULT _stdcall utilDoubleToVal(
[in] double dVal,
[in] long lFraction,
[out] long* plVal,
[out, retval] long* lError);
[helpstring("method RGetVendorInfo")]
HRESULT _stdcall RGetVendorInfo(
[in] long lByType,
[in, out] Vendor_Desc* pVenDesc,
[out, retval] long* lError);
[helpstring("reserved method")]
HRESULT _stdcall RAdmin(
[in] long* pBuffer,
[in] long lLen,
[in] long lReserved,
[out, retval] long* lError);
[helpstring("method utilGuessFraction")]
HRESULT _stdcall utilGuessFraction(
[in] double dValue,
[out] long* lFraction,
[out, retval] long* lError);
[helpstring("method RProxyOpen")]
HRESULT _stdcall RProxyOpen(
[in] IDispatch* pTarget,
[out, retval] long* lError);
[helpstring("method RProxyClose")]
HRESULT _stdcall RProxyClose(
[in] IDispatch* pTarget,
[out, retval] long* lError);
[helpstring("method RSrvGetFileName")]
HRESULT _stdcall RSrvGetFileName(
[in] BSTR Rid,
[in] long lType,
[in] BSTR* pName,
[out, retval] long* lError);
[helpstring("method RegisterUser")]
HRESULT _stdcall RegisterUser(
[in] long* pRegisterInfo,
[out, retval] long* lError);
[helpstring("method RGetSystemString")]
HRESULT _stdcall RGetSystemString(
[in] long lTopic,
[in, out] BSTR* pString,
[out, retval] long* lError);
[helpstring("method RSrvFileWrite")]
HRESULT _stdcall RSrvFileWrite(
[in] BSTR Rid,
[in] unsigned char* pBuffer,
[in] long lLen,
[in] long lOpts,
[out, retval] long* lError);
[helpstring("method RSetSystemVal")]
HRESULT _stdcall RSetSystemVal(
[in] long lTopic,
[in, out] long* lStatus,
[out, retval] long* lError);
};

[
uuid(9C6EE341-8CE4-11D1-9E93-00A0C9050615),
helpstring("IMainAPI Event Interface")
]
dispinterface _DMainEvents {
properties:
methods:
[id(0x00000065)]
void CommEvent(
[in] long lSubMsg,
[in] Comm_Notify* pNotify);
[id(0x00000066)]
void GenErrEvent(
[in] long lSubMsg,
[in] long lErrId);
[id(0x00000067)]
void StatusEvent(
[in] long lSubMsg,
[in] Status_Notify* pNotify);
[id(0x0000006d)]
void SrvFileEvent(
[in] long lSubMsg,
[in] SrvFile_Notify* pNotify);
};


[
uuid(AF0DEB31-1D4C-11D1-9E93-00A0C9050615),
helpstring("MainAPI Class")
]
coclass MainAPI {
[default] interface IMainAPI;
[default, source] dispinterface _DMainEvents;
};


3. Error: Unable to cast COM object of type 'QSCOMLib.MainAPIClass' to interface type 'QSCOMLib.IMainAPI'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{AF0DEB30-1D4C-11D1-9E93-00A0C9050615}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Object reference not set to an instance of an object.
 
Back
Top