Search code examples
c++.netcomcom-interop

How to create a HelloWorld COM Interop in Visual Studio 2012


First off, I am new to COM, and currently quite confused. I've read a lot of documentation on COM on MSDN and the general web, but a lot of it seems outdated and overly complex.

Here's what I believe to be necessary to get it to work. It doesn't yet, so I am sure I am missing something, but by giving my recipe, I hope someone can spot the problem:

  1. Create a C# console app project. I name it CSharpApp
  2. Create a C++ ATL project. I call it ComLib.Interop.
    • Add class (template: ATL Simple Object), which I call "InteropDemo"
    • In class view, right-click IInteropDemo and add a method HelloWorld.
  3. (Removed, kept to keep numbering in answers correct.)
  4. Compile.
  5. Add reference to ComLib.Interop.dll to CSharpApp.
    • Call regsrv32.exe on the compiled COM dll, then select the DLL in the COM tab in 'Add references...'
  6. In Program.cs, Main, create an InteropDemo class and call HelloWorld.
  7. Profit.

Thanks to the answers, I updated the question to reflect the combined solution.


Solution

  • Try following these steps:

    1. Make sure both projects, unmanaged C++ and managed C# have the same bitness, either x86 or x64. Let's say it's x86, for clarity.
    2. Open Admin Command Prompt and register your COM DLL: C:\Windows\SysWOW64\regsvr32.exe c:\full-path\ComLib.Interop.dll
    3. Run Visual Studio as Admin. Do steps 1,2,4,5,6. Don't do 3.

    See if you get to 7. I think that should work.

    Note you only need the registration on the Development machine. Isolated COM should work everywhere else.