CoInitialize()在C ++中没有被调用的异常 [英] CoInitialize() has not been called exceptions in C++

查看:972
本文介绍了CoInitialize()在C ++中没有被调用的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

- 我的问题



我得到CoInitialize未被调用exption。



- 我的项目结构



这是我的徽章。我有一个COM DLL,MCLWrapper.dll开发与C#;我有一个注意的本机C + + DLL,ThorDetectorSwitch.dll调用MCLWrapper.dll;最后,我有一个控制台应用程序TDSTest.exe调用ThorDetectorSwitch.dll。基本上,这样的东西:



TDSTest.exe(C ++控制台) - > ThorDetectorSwitch.dll(C + +本机) - > MCLWrapper.dll em>






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

  HINSTANCE hInst = LoadLibrary(_T(C:\\TIS_Nick\\\\Hardware\\Devices\\ThorDetectorSwitch\\TDSTest\\ \\\TDSTest\\Debug\\Modules_Native\\ThorDetectorSwitch.dll)); 

ThorDetectorSwitch.cpp中的构造函数

  ThorDetectorSwitch :: ThorDetectorSwitch():_mcSwitch(__ uuidof(MCLControlClass))
{
_A = WstringToBSTR(LA);
_B = WstringToBSTR(LB);
_C = WstringToBSTR(LC);
_D = WstringToBSTR(LD);

_deviceDetected = FALSE;
}

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



跳转到

  hr = CoCreateInstance(rclsid,pOuter,dwClsContext,__uuidof(IUnknown),reinterpret_cast< void **>(& pIUnknown)); $ com 
$ b

这个小时只是CoInitialize没有被调用。



我一直在想这个标志几天,而不能找出一个解决方案。这里的任何人都可以分享任何想法?非常感谢。

解决方案

您的COM dll要求您处于单线程公寓模式。您需要致电 CoInitialize



将此添加到您的.exe中:

  CoInitialize(nullptr); // NULL如果使用旧的VC ++ 

HINSTANCE hInst = LoadLibrary(_T(C:\\TIS_Nick\\ ...
pre>

-My problem

I got CoInitialize has not been called exption.

-My project structure

Here is my porblem. I have a COM dll, MCLWrapper.dll developped with C#; I have a nother native C++ dll, ThorDetectorSwitch.dll that calls MCLWrapper.dll; And finally, I have a console application TDSTest.exe that calls ThorDetectorSwitch.dll. Basically, something like this:

TDSTest.exe (C++ console) -> ThorDetectorSwitch.dll (C++ native) -> MCLWrapper.dll (C#)


Code in TDSTest.exe that loads the ThorDetectorSwitch.dll:

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

Constructor in 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;
}

The break point hits the first parenthesis of the constructor of the ThorDetectorSwitch.dll above, but the exception occurred immediately if I hit F10 (one more step)

It jumps to

 hr = CoCreateInstance(rclsid, pOuter, dwClsContext, __uuidof(IUnknown), reinterpret_cast<void**>(&pIUnknown));

in the comip.h. The hr is simply "CoInitialize has not been called".

I have been thinking abou this porblem for days, and cannot figure out a solution. Anyone here can sharing any thoughts? Really appreciate it.

解决方案

Your COM dll requires you to be in Single-Threaded Apartment mode. You need to call CoInitialize prior to using it.

Add this to your .exe:

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

HINSTANCE hInst = LoadLibrary(_T("C:\\TIS_Nick\\...

这篇关于CoInitialize()在C ++中没有被调用的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆