C# COM dll has REGDB_E_CLASSNOTREG error in C++ project

大憨熊 提交于 2019-12-11 05:52:24

问题


I have a C# dll that I properly have registered for COM Interop, and made COM visible. Using cppbuilder, I imported the type library, which generated the wrapper classes, and I am now attempting to use to create an instance of my C# class. However, I'm getting a REGDB_E_CLASSNOTREG error in my C++ code. I verified the dll is in the registry, and even re-registered it with regasm. No change. What could I be missing?

Here is my C++ code:

_MyClassPtr obj;
HRESULT hr = obj.CreateInstance(__uuidof(MyClass));
//now hr equals REGDB_E_CLASSNOTREG

I've also tried it as such:

IMyClass* obj;
HRESULT hr = CoCreateInstance(__uuidof(MyClass), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMyClass), (void**) &obj);
//same result, hr equals REGDB_E_CLASSNOTREG

I do have one additional dependency in the C# app. I registered it for COM as well with no difference, but did not import it's type library into the C++ project.

UPDATE: based on the comments below, I discovered that CreateInstance is looking up the class guid in the following places in the registry:

HKCU\Software\Classes\Wow6432Node\CLSID\{guid} 
HKCR\Wow6432Node\CLSID\{guid}
HKCU\Software\Classes\CLSID\{guid}
HKCR\CLSID\{guid}

But, going through the registry, the only entry under any of the CLSID nodes that is related to my assembly is the guid for the assembly itself, which is, of course, different than the guid for the class, or the interface.

I've manually run regasm under both x86 and x64 mode to try to acheive different results. No differences.


回答1:


Well, I found out what would work.

IMyClassPtr obj;
HRESULT hr = obj.CreateInstance(CLSID_MyClass);

CLSID_MyCLass was a guid constant in the generated MyClass_TLB.cpp file. Using it instead of __uuidof(...) for the class types enabled everything to start working correctly.



来源:https://stackoverflow.com/questions/13073488/c-sharp-com-dll-has-regdb-e-classnotreg-error-in-c-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!