2011-02-22 85 views
1

我試圖從VC++ 2005使用COM DLL。我創建了一個ATL的TestCOMlib.dll,創建了一個簡單的界面ISimple並添加了一個屬性(類型LONG,名稱Property01)和一個方法(名稱Method01)。#import導致HRESULT 0x80040154「未註冊的類」

該DLL似乎在系統中正確註冊(我使用OleView檢查條目)。

我創建了一個簡單的MFC對話框應用程序來使用COM DLL。我正在使用#import指令來整合類型庫中的信息。 Visual Studio爲我創建了tlh和tli文件。

然後我試圖獲得ISimple接口,但我得到錯誤0x80040154。 我測試應用中運行的代碼如下:

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(); 

行返回0x80040154錯誤是ISimplePtr myRef(__ uuidof(ISimple))。 OleView正確顯示接口,並在註冊表中的條目似乎是好的。

我在做什麼錯?任何想法?

問候

+2

糟糕,發現了問題。我必須用__uuidof(簡單)替換__uuidof(ISimple)。 – gionny 2011-02-22 22:30:18

回答

4

對於這些智能COM指針底層類是_com_ptr_t。您正在嘗試使用此構造:

// 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 
); 

關鍵的一點是,你必須通過的CLSID組件類的,要傳遞的接口的IID。這就是爲什麼__uuidof(Simple)有效。

+0

謝謝,花時間爲這個解釋! – gionny 2011-02-23 08:24:40

+0

什麼是IID的方式,我有同樣的錯誤..http://stackoverflow.com/questions/27832181/unhandled-exception-in-vc-hresult-failed請回答它 – Wielder 2015-01-08 06:35:45

相關問題