0

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.

  • Import the TLB file with #import "SomeDLL.tlb"
  • Use the class MyClass defined in the DLL with CComPtr<MyClass>.

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?

2 Answers 2

1

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);
}
Sign up to request clarification or add additional context in comments.

2 Comments

What would recompiling accomplish? Yes.
OK, instead of checking if hr == S_OK, I'm using the FAILED macro.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.