Call an c# dll in c++

.net , csharp Austria
  • 15 years ago
    Hi guys,

    I tried several times to create a c# dll and call it in c++, but it never works and i don't know why.
    Here is my code:

    This is the code from the c# dll :

    using System;
    using System.Runtime.InteropServices;
    using System.Windows.Forms;

    namespace MyInterop
       interface IMyDotNetInterface
           void ShowCOMDialog();
       class MyDotNetClass : IMyDotNetInterface
           // Need a public default constructor for COM Interop.
           public MyDotNetClass()
           public void ShowCOMDialog()
               System.Windows.Forms.MessageBox.Show(“I am a" +
                     "  Managed DotNET C# COM Object Dialog”);

    After that I create a strong name key pair
    sn -k TestKeyPair.snk

    and write it  in the AssemblyInfo.cs like this:
    [assembly: AssemblyKeyFile("TestKeyPair.snk")]

    THen i add it to the Gac:
    gacutil /i MyInterop.dll

    At last I Register my assembly for COM
    REGASM MyInterop.dll /tlb:com.MyInterop.tlb

    Now the c++ Code:

    #import “<Full Path>\com.MyInterop.tlb" named_guids raw_interfaces_only
    CoInitialize(NULL);   //Initialize all COM Components
    // <namespace>::<InterfaceName>
    MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;

    // CreateInstance parameters
    // e.g. CreateInstance (<namespace::CLSID_<ClassName>
    HRESULT hRes =
    if (hRes == S_OK)
       BSTR str;
       pDotNetCOMPtr->ShowCOMDialog ();
       //call .NET COM exported function ShowDialog ()

    CoUninitialize ();   //DeInitialize all COM Components

    I can load the tlb into the c++ Project, but my compiler says that  

    MyInterop::IMyDotNetInterfacePtr pDotNetCOMPtr;

    is not a Member of MyInterop and he dowsn't know any Methods of this class.
    PLeases help me or give me an example how to call an c# dll in a c++ project.

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).


Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Programs must be written for people to read, and only incidentally for machines to execute.”