Sign in to follow this  
NikiTo

Can't CoInitializeEx() inside DLL

Recommended Posts

My main project is a C# console application and it generates an exe. C# calls functions exported from a dll written in C++. I do nothing weird nowhere inside the code. The DllMain inside the dll is empty. When I try to call CoInitializeEx(null, COINIT_APARTMENTTHREADED) i get error: RPC_E_CHANGED_MODE.

I never touch or use threads declarations, neither COM related stuff nowhere in the code. Who is declaring the model as MULTITHREADED and how should I deal with the problem? Should I somehow initialize the dll as single threaded? Do I need CoInitializeEx() at all? I am afraid to simply delete it to not break something on my computer.

Share this post


Link to post
Share on other sites
I'm guessing that the C# thread has already called CoInitializeEx itself. I believe this is what the [STAThread] and [MTAThread] attributes do on the default Program.Main function that a new project template has.

If your C# entry point doesn't have [STAThread] on it, try adding it.

Share this post


Link to post
Share on other sites

Thank you for the answer, @Nypyren! You suggested that the template is setting the threading for me. I was afraid to add STAThread and mess something, that's why I first tried what was easier- I just out-commented the CoInitializeEx in the constructor of the class that use COM and the CoUnitialize in the destructor and it is working without a problem. I hope it will not give me headaches in the future. COM seems enough tricky for me as to add to it the complexity of threading.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this