Question

I'm trying to use a COM DLL from VC++ 2005. I created a TestCOMlib.dll with ATL, created a simple interface ISimple and added one property (type LONG, name Property01) and one method (name Method01).

The DLL seems to be correctly registered in the system (I use OleView to check the entries).

I created a simple MFC dialog app to use the COM dll. I'm using the #import directive to incorporate information from the type library. Visual studio created for me the tlh and tli file.

Then I tried to obtain the ISimple interface but I'm obtaining the error 0x80040154. The code I'm running inside the test application is the following:

HRESULT hr = S_OK;
hr = CoInitialize(NULL);

ISimplePtr myRef(__uuidof(ISimple));

// Test prop and method
myRef->Property01 = 5;
LONG test = myRef->Property01;
LONG ret = myRef->Method01(_T("Test input"));
ret = myRef->Method01(NULL);

myRef = NULL;
CoUninitialize();

The row returning the error 0x80040154 is ISimplePtr myRef(__uuidof(ISimple)). OleView correctly display the interface and in the registry the entries seems to be good.

What am I doing wrong? Any idea?

Regards

Was it helpful?

Solution

The underlying class for these smart COM pointers is _com_ptr_t. You are trying to use this constructor:

// Constructs a smart pointer given the CLSID of a coclass. This 
// function calls CoCreateInstance, by the member function
//  CreateInstance, to create a new COM object and then queries for 
// this smart pointer's interface type. If QueryInterface fails with 
// an E_NOINTERFACE error, a NULL smart pointer is constructed.
explicit _com_ptr_t( 
   const CLSID& clsid,  
   IUnknown* pOuter = NULL,  
   DWORD dwClsContext = CLSCTX_ALL 
);

Key point is that you have to pass the CLSID of the coclass, you are passing the IID of the interface. That's why __uuidof(Simple) works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top