-3

I noticed that someone asked a similar question on how to consume C# COM component in C++

But that thread is marked as duplicate because the title of question is too generic and does not specify C#.

Since I am not able to answer that question, I am creating this new question which I'll answer and others can also put forward there suggestions

5
  • Possible duplicate: stackoverflow.com/q/1482131/50447 Commented Aug 21, 2014 at 7:56
  • That thread(which Rowland Shaw thinks is duplicate) is about calling C# COM component from another C# COM component to test whether its a valid COM component or not. My thread is about consuming C#.NET COM component from C++ code. Commented Aug 21, 2014 at 8:30
  • In which case it's exactly the same as stackoverflow.com/questions/3648103/using-com-object-in-c? Commented Aug 21, 2014 at 8:33
  • yes Rowland Shaw you are correct now, and i have already mentioned the same URL as an HREF for "Similar Question" in the description of my question. And why I replied here is because there was no editable box to put the answer there. This also i mentioned in my original question. Commented Aug 21, 2014 at 10:13
  • When something is closed as a duplicate, you can add your answer to the original question, in this case, over at stackoverflow.com/q/410005/50447 Commented Aug 21, 2014 at 11:07

1 Answer 1

-1

Here is the answer of this situation from my side:

Referring a C++ COM component is easy in .NET; all we have to do is to

  • register (regsvr32 )
  • Add a reference to the COM dll in the Visual Studio Solution
  • An Interop file will be created in the bin directory of .NET project and we are done

But the reverse is not as simple as above.
So this time I have come up with Steps we need to follow in order to refer a COM visible .NET dll in C++ code.

  • Register the COM Visible .NET assambly with regasm like this:
    regasm <dll_name_with_full_path> /tlb
    e.g.

    regasm "C:\SampleProject\bin\Release\Atreya.Anugrah.ComVisibleComponent.dll" /tlb
    
  • Refer .NET component in .h file like this:
    #import "<generated_tlb_name_with_full_path>" named_guids raw_interfaces_only
    e.g.

    #import "C:\SampleProject\bin\Release\Atreya.Anugrah.ComVisibleComponent.tlb"  named_guids raw_interfaces_only
    
  • Create a class in .h file, and declare member variable like this:
    ComVisibleComponent::* memberVariablePointer;
    e.g.

    Atreya_Anugrah_ComVisibleComponent::IMyInterface* m_pIMyInterface;
    
  • Now, Initialize the declared member variable pointer in .cpp file like this:

    hr = CoCreateInstance( 
    __uuidof(Atreya_Anugrah_ComVisibleComponent::<Name_of_CoClass_Implementing_Interface>), // COM class id 
    NULL, // Outer unknown
    CLSCTX_ALL, // Server INFO 
    Atreya_Anugrah_ComVisibleComponent::IID_<Name_of_Interface>, // interface id
    (void**) &m_pIMyInterface);  // Returned Interface                                                                                                                   
    
    if (FAILED(hr))
    {
    _com_error e(hr);
    strErrorMsg.Format("Error: Unable to create COM Visible .NET component. hr = %d. Message = %s",hr, e.ErrorMessage());
    return hr;
    }
    
    //you are good to use your initialized pointer here :)   
    m_pIMyInterface->foo()
    

P.S. This post does not explain that how can we make our .NET component as COM visible. Please refer other post/article for that as it is quite simple activity.

Namaste! (Greetings)
Anugrah Atreya

Sign up to request clarification or add additional context in comments.

1 Comment

I agree this is duplicate, Admin can delete if possible.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.