[英]Using DLL that using COM in C#
I have been writing DLL on C++, that will be use in C#. 我一直在C ++上编写DLL,它将在C#中使用。 DLL have some function, where I call
DLL有一些功能,我打电话给
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
and next call 和下一个电话
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_PKT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
There are no error then I trying to use this dll in C++ . 没有错误然后我试图在C ++中使用这个DLL。 But if I call function from DLL via C# application I see Error (80010106) Cannot change thread mode after it is set.
但是,如果我通过C#应用程序从DLL调用函数,我看到错误(80010106)设置后无法更改线程模式。 I changed
我变了
hres = CoInitializeEx(NULL, COINIT_MULTITHREADED);
to 至
hres = CoInitialize(NULL);
After this changes error appear after CoInitializeSecurity : 在CoInitializeSecurity之后出现此更改错误:
(80010119) Security must be initialized before any
interfaces are marshalled or unmarshalled. It
cannot be changed once initialized.
How resolve this trouble? 怎么解决这个麻烦?
You could remove the call to CoInitializeEx
and CoInitializeSecurity
from your DLL. 您可以从DLL中删除对
CoInitializeEx
和CoInitializeSecurity
的调用。 They have already been called on the thread - that's why you get these errors. 它们已经在线程上被调用 - 这就是你得到这些错误的原因。
However, if your DLL is trying to initialize a COM thread as a multithreaded apartment, and your C# application is calling the DLL on a single-threaded apartment, then you may have a basic incompatibility. 但是,如果您的DLL尝试将COM线程初始化为多线程单元,并且您的C#应用程序在单线程单元上调用DLL,那么您可能具有基本的不兼容性。 It depends whether the line of code that specified
COINIT_MULTITHREADED
was a deliberate choice with a reason behind it, or just something that seemed to work at the time it was originally written. 这取决于指定
COINIT_MULTITHREADED
的代码行是否是故意选择的原因,或者只是在最初编写时似乎COINIT_MULTITHREADED
东西。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.