Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoInitializeSecurity error

I have an .exe that loads 1.dll and 1.dll loads 1_1.dll. In the .exe I create multiple threads, from one of them I call a function that calls upon a 1.dll function that between other things .. calls a function from 1_1.dll that fails in doing this:

//  Initialize COM.
    HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
    if( FAILED(hr) )
    {
        //m_iStatus = ERROR_COINITIALIZE_EX;
        return;
    }
    //  Set general COM security levels.
    hr = CoInitializeSecurity(
        NULL,
        -1,
        NULL,
        NULL,
        RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
        RPC_C_IMP_LEVEL_IMPERSONATE,
        NULL,
        0,
        NULL);
    if( FAILED(hr) )
    {
        CoUninitialize();
        //m_iStatus = ERROR_COINITIALIZE_SEC;
        return;
    }

It falis on the call to CoInitializeSecurity with this message:

Security must be initialized before any interfaces are marshalled or unmarshalled. It cannot be changed once initialized.

Can anyone expalin me what is happening here, what am I doing wrong and how should I fix this?

like image 738
AlexandruC Avatar asked May 29 '26 05:05

AlexandruC


1 Answers

CoInitializeSecurity function...

... is called exactly once per process, either explicitly or implicitly

The only case DLL might need to call CoInitializeSecurity is when it is loaded into process, which is known to not initialize COM on its own. An the process is at all basically a thin host for the DLL. That is, almost never.

It is .EXE's task to do CoInitializeSecurity.

like image 195
Roman R. Avatar answered May 30 '26 17:05

Roman R.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!