Jacob
Jacob

Reputation: 34621

Calling a function in a managed DLL from native Visual C++ code through COM

Apologies in advance for the following verbose question ; I am a COM noob.

Scenario: I need to call a managed DLL built with C# from native Visual C++ code. In my native VC++ code, I do the following after registering "SomeDLL.dll" and generating the "SomeDLL.tlb" file with RegAsm.exe.

Everything's great! It compiles, and I can run the code etc. It hits the fan when I try to run this application on a different machine (i.e. not the one I compiled it on). I copy all the required DLLs, and I register the same DLL with RegAsm.exe but it doesn't work.

It specifically fails when it tries to initialize the COM library with CoInitialize(0) and returns the S_FALSE error which means

The COM library is already initialized on this thread.

I can confidently state that I have not called this function anywhere else in my code.

Any suggestions?

Upvotes: 0

Views: 884

Answers (2)

PeeWee2201
PeeWee2201

Reputation: 1524

Maybe you called OleInitialize or another function which calls ComInitialize behind the scenes.

Anyway, it does not matter to call CoInitialize several times per thread if you match each of them with a call to CoUninitialize

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 942000

Hard to help you find that code from here, you're a lot closer. Maybe a DLL that gets injected.

Getting S_FALSE is not an error, getting RPC_E_CHANGED_MODE would be quite bad. Be sure to use the FAILED macro:

HRESULT hr = CoInitialize(0);
if (FAILED(hr)) {
    CallNineOneOne(hr);
    exit(hr);
}

Upvotes: 1

Related Questions