windows 为什么 COM CoInitializeSecurity 在我的 DLL 中失败?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5662382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:38:34  来源:igfitidea点击:

Why does COM CoInitializeSecurity fail in my DLL?

windowsdllcom

提问by Felix Dombek

I'm currently studying VSHADOW.EXE 3.0 from the MS Windows SDK 6.1. I have made a version which can be compiled into a DLL that only exports one newly written function which expects the commandline as a string, tokenizes it, and then calls the old wmain. The DLL is not a COM server.

我目前正在从 MS Windows SDK 6.1 学习 VSHADOW.EXE 3.0。我制作了一个可以编译成 DLL 的版本,它只导出一个新编写的函数,该函数将命令行作为字符串,对其进行标记,然后调用旧的wmain. DLL 不是 COM 服务器。

It works exactly as the old one when compiled as an EXE but doesn't quite work when compiled as a DLL because this call fails:

它在编译为 EXE 时与旧的完全一样,但在编译为 DLL 时不太工作,因为此调用失败:

 CoInitializeSecurity(NULL, -1, NULL, NULL, 
                      RPC_C_AUTHN_LEVEL_PKT_PRIVACY, 
                      RPC_C_IMP_LEVEL_IDENTIFY, 
                      NULL, EOAC_NONE, NULL);

which fails with HRESULTerror 0x80010119(RPC_E_TOO_LATE, Security must be initialized before any interfaces are marshalled or unmarshalled. It cannot be changed once initialized.)

失败并HRESULT出现错误0x80010119RPC_E_TOO_LATE必须在编组或解组任何接口之前初始化安全性。一旦初始化就无法更改。

I run the exported function from a VB6 program where the function is imported with Declare Function vss Lib vshadow.dll ....

我从 VB6 程序运行导出的函数,该程序使用Declare Function vss Lib vshadow.dll ....

Does the error mean that the VB6 program already called CoInitializeSecurity? What can I do against the error?

该错误是否意味着 VB6 程序已被调用CoInitializeSecurity?我可以针对错误做些什么?

Also, I have another question: why were exactly the security values RPC_C_AUTHN_LEVEL_PKT_PRIVACYand RPC_C_IMP_LEVEL_IDENTIFYchosen? What impact would other settings have?

另外,我还有一个问题:为什么都是一模一样的,安全值RPC_C_AUTHN_LEVEL_PKT_PRIVACYRPC_C_IMP_LEVEL_IDENTIFY选择?其他设置会产生什么影响?

回答by Hans Passant

There are a couple of standard COM calls that do notbelong in a DLL. Like CoInitializeEx(), the call that initializes COM for a thread. The DLL doesn't own the thread, it is powerless to override the apartment state that the EXE selected.

有几个属于 DLL的标准 COM 调用。与 CoInitializeEx() 一样,为线程初始化 COM 的调用。DLL 不拥有线程,无法覆盖 EXE 选择的单元状态。

CoInitializeSecurity() is another one, it is the job of the EXE to call it. Only it knows the proper values to pass, it's the one that determines the security policy. A DLL can't, it doesn't know anything about the client process.

CoInitializeSecurity() 是另一个,调用它是 EXE 的工作。只有它知道要传递的正确值,它才能确定安全策略。DLL 不能,它对客户端进程一无所知。