我一直在與VStudio,谷歌和其他各種工具和網站作鬥爭整天,發現沒有解決方案 - 幫助!絕對COM混亂 - 與早期綁定的C#互操作
我有兩個COM接口(純COM,沒有ATL):
IMyClassFactory和IMyClass與相應實現
我想從C#,但沒有與REGSVR32註冊COM服務器使用它們。 我用CoRegisterClassObject公開類工廠,我可以用非託管代碼成功創建帶有CoCreateInstance的IMyClass對象。
所以C#互操作...
我創建了TLBIMP myComServer.tlb一個.NET包裝並加載它作爲我的C#客戶端的參考。
然後,當我嘗試創建IMyClass的實例,我得到:
An unhandled exception of type 'System.InvalidCastException' occurred in COMTestClient.exe
Additional information: Unable to cast COM object of type 'MyComServerLib.MyClass' to interface type 'MyComServerLib.IMyClass'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{9F8CBFDC-8117-4B9F-9BDC-12D2E6A92A06}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
現在,我已經添加的痕跡到的QueryInterface,當我返回E_NOINTERFACE的唯一情況是,當它的任何請求與統帥相關的接口或用於IManagedObject。
我該如何解決這個問題?
編輯:我的IDL文件:
import "unknwn.idl";
[
object,
uuid(...),
nonextensible,
pointer_default(unique)
]
interface IMyClass : IUnknown
{
HRESULT(SetFirstNumber)(long nX1);
HRESULT(SetSecondNumber)(long nX2);
HRESULT(DoTheAddition)([out,retval] long *pBuffer);
};
[
uuid(...)
]
library MyLib
{
importlib("stdole2.tlb");
[
uuid(...)
]
coclass IMyClassImpl
{
[default] interface IMyClass;
};
}
您可以添加更多關於如何執行任一解決方案的信息嗎?我將用我的IDL文件編輯我的原始文章。謝謝 – georgiosd 2009-10-06 23:02:06
我已經添加了一些更多的細節 – 2009-10-06 23:57:56
COM對象的引用計數實現是否也需要線程安全,使用互鎖操作?如果CLR將COM對象視爲自由線程,它將從終結器線程調用它。 – 2009-10-07 06:36:01