开发者

Indirectly calling CoInitializeSecurity from a .NET application

开发者 https://www.devze.com 2023-04-10 01:06 出处:网络
I have the following code in a pure C project: void Foo() { HRESULT hres =CoInitializeEx(0, COINIT_MULTITHREADED);

I have the following code in a pure C project:

void Foo()
{
    HRESULT hres =  CoInitializeEx(0, COINIT_MULTITHREADED); 

    hres =  CoInitial开发者_如何学JAVAizeSecurity(
        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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消