Reputation: 549
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.
Upvotes: 4
Views: 1507
Reputation: 74530
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.
Upvotes: 3