tags:

views:

141

answers:

2

I have been writing DLL on C++, that will be use in C#. DLL have some function, where I call

hres =  CoInitializeEx(NULL, COINIT_MULTITHREADED); 

and next call

hres =  CoInitializeSecurity(
        NULL, 
        -1,                          // COM authentication
        NULL,                        // Authentication services
        NULL,                        // Reserved
        RPC_C_AUTHN_LEVEL_PKT,   // Default authentication 
        RPC_C_IMP_LEVEL_IMPERSONATE, // Default Impersonation  
        NULL,                        // Authentication info
        EOAC_NONE,                   // Additional capabilities 
        NULL                         // Reserved
        );

There are no error then I trying to use this dll in C++. But if I call function from DLL via C# application I see Error (80010106) Cannot change thread mode after it is set. I changed

hres =  CoInitializeEx(NULL, COINIT_MULTITHREADED);

to

hres = CoInitialize(NULL);

After this changes error appear after CoInitializeSecurity:

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

How resolve this trouble?

A: 

You could remove the call to CoInitializeEx and CoInitializeSecurity from your DLL. They have already been called on the thread - that's why you get these errors.

However, if your DLL is trying to initialize a COM thread as a multithreaded apartment, and your C# application is calling the DLL on a single-threaded apartment, then you may have a basic incompatibility. It depends whether the line of code that specified COINIT_MULTITHREADED was a deliberate choice with a reason behind it, or just something that seemed to work at the time it was originally written.

Daniel Earwicker
A: 

I have been removed CoInitializeSecurity and everythings work correctly. Thanks

chekalin-v
chekalin-v - to use this site correctly, you don't put further information into answers. You add the information to your original question, by editing it. Also you upvote the answers you found helpful, and you can accept (press the green check/tick mark) on the answer you found most helpful.
Daniel Earwicker