从 .NET 应用程序间接调用 CoInitializeSecurity

发布于 2024-12-07 16:26:05 字数 793 浏览 0 评论 0原文

我在纯 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

坏尐絯℡ 2024-12-14 16:26:05

这是预期行为 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 call CoInitializeSecurity before the CLR is started in the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文