从 .NET 应用程序间接调用 CoInitializeSecurity
我在纯 C 项目中有以下代码:
void Foo()
{
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
}
如果我将项目构建为应用程序 (.exe) 并调用此方法,则一切正常。
如果我将项目构建为静态库 (.lib),在 .NET 应用程序中使用它并调用该方法,CoInitializeSecurity 将返回错误 1008:尝试引用不存在的令牌。
I have the following code in a pure C project:
void Foo()
{
HRESULT hres = CoInitializeEx(0, COINIT_MULTITHREADED);
hres = CoInitializeSecurity(
NULL,
-1, // COM authentication
NULL, // Authentication services
NULL, // Reserved
RPC_C_AUTHN_LEVEL_DEFAULT, // Default authentication
RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation
NULL, // Authentication info
EOAC_NONE, // Additional capabilities
NULL // Reserved
);
}
If I build the project as an Application (.exe) and invoke this method, everything works fine.
If I build the project as a Static Library (.lib), use it in a .NET application and invoke the method, CoInitializeSecurity returns a error 1008: An attempt was made to reference a token that does not exist.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是预期行为
CoInitializeSecurity
并不意味着从 CLR 内执行的代码中调用。如果您明确需要调用
CoInitializeSecurity
,则需要自行托管 CLR 并在进程中启动 CLR 之前调用CoInitializeSecurity
。This is the expected behavior in that
CoInitializeSecurity
is not meant to be called from code executed within the CLR.If you have an explicit need to call
CoInitializeSecurity
, then you need to host the CLR yourself and callCoInitializeSecurity
before the CLR is started in the process.