CoInitialize()在C++中没有被称为异常

Nic*_*sui 8 c++ com dllimport

- 我的问题

我得到的CoInitialize还没有被称为exption.

- 我的项目结构

这是我的问题.我有一个COM DLL,用C#开发MCLWrapper.dll; 我有一个原生的C++ DLL,ThorDetectorSwitch.dll调用MCLWrapper.dll; 最后,我有一个调用ThorDetectorSwitch.dll的控制台应用程序TDSTest.exe.基本上,这样的事情:

TDSTest.exe(C++控制台) - > ThorDetectorSwitch.dll(C++ native) - > MCLWrapper.dll(C#)


TDSTest.exe中加载ThorDetectorSwitch.dll的代码:

HINSTANCE hInst = LoadLibrary(_T("C:\\TIS_Nick\\Hardware\\Devices\\ThorDetectorSwitch\\TDSTest\\TDSTest\\Debug\\Modules_Native\\ThorDetectorSwitch.dll"));
Run Code Online (Sandbox Code Playgroud)

ThorDetectorSwitch.cpp中的构造函数

ThorDetectorSwitch::ThorDetectorSwitch() : _mcSwitch(__uuidof(MCLControlClass))
{
    _A  = WstringToBSTR(L"A"); 
    _B  = WstringToBSTR(L"B");
    _C  = WstringToBSTR(L"C");
    _D  = WstringToBSTR(L"D");

    _deviceDetected = FALSE;
}
Run Code Online (Sandbox Code Playgroud)

断点击中上面ThorDetectorSwitch.dll的构造函数的第一个括号,但是如果我点击F10则会立即发生异常(再多一步)

它跳到了

 hr = CoCreateInstance(rclsid, pOuter, dwClsContext, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown));
Run Code Online (Sandbox Code Playgroud)

在comip.h中.hr简直就是"没有调用CoInitialize".

几天来我一直在想这个问题,无法找到解决办法.这里有人可以分享任何想法?真的很感激.

Ree*_*sey 16

您的COM dll要求您处于单线程公寓模式.您需要CoInitialize在使用之前致电.

将此添加到您的.exe:

CoInitialize(nullptr); // NULL if using older VC++

HINSTANCE hInst = LoadLibrary(_T("C:\\TIS_Nick\\...
Run Code Online (Sandbox Code Playgroud)