3

I have a dll file which is created in the c# language.And I wanted to use the functions which are written in c# language in that dll file from a c++ project. I know it's possible to use c# dll with c# project. But no idea of what is the best way of doing c# dll in c++ project. I'm using visual studio 2013. All your guidance are highly appreciate.

Termininja
  • 6,620
  • 12
  • 48
  • 49
Anushka Ekanayake
  • 977
  • 2
  • 13
  • 33
  • 1
    You could either use C++/CLI instead of C++, or you could create a C# COM object and use that in C++. Both ways are... not perfect. – nvoigt Jul 16 '15 at 05:12
  • @ the person who down voted the question : please share your knowledge without just down voting others questions if you know it. – Anushka Ekanayake Jul 16 '15 at 05:13
  • 1
    http://stackoverflow.com/q/19144006/2706918 – Nitinkumar Ambekar Jul 16 '15 at 05:13
  • 1
    With COM interoperability it's very easy. See https://msdn.microsoft.com/en-us/library/zsfww439(v=vs.80).aspx – user1610015 Jul 16 '15 at 05:14
  • if I use c# com objects to create dll then will there be any performance issue? – Anushka Ekanayake Jul 16 '15 at 05:18
  • @user1610015 I guess you never worked with COM. COM and "very easy" should not go into the same sentence. Ever. – nvoigt Jul 16 '15 at 05:21
  • @nvoigt COM is very easy - you just need to implement your own IDispatch - based scriptable object, and OLE server and client to get to that state :).. DCOM is where it gets hard :) – Alexei Levenkov Jul 16 '15 at 05:28
  • 1
    @AlexeiLevenkov And you think *that* (and the pain of COM in C++) is actually something the OP (Who cannot find this answer on Google) will accept as "very easy"? – nvoigt Jul 16 '15 at 05:29
  • Some reasons for downvote - if search for title (http://www.bing.com/search?q=How+to+call+dll+file+created+witth+c%23+from+c%2B%2B+project) brings good references on first page (or even worse on first/second position) the question deserves downvote due to lack of research. – Alexei Levenkov Jul 16 '15 at 05:31

1 Answers1

2

I'll summarize the basic steps to take to expose a C# class to C++ through COM. Let's say you have the following C# class:

public class Number
{
    public Number()
    {
    }

    public int Value
    {
        get;
        set;
    }
}

First you need to create an interface for the class to implement. Only the methods and properties exposed through that interface are visible to COM (and therefore C++). Typically this interface has the same name as the class, but with an "I" prefix:

public interface INumber
{
    int Value
    {
        get;
        set;
    }
}

Next you need to add the ComVisible and Guid attributes to both the interface and the class. It's also recommended to add [ClassInterface(ClassInterfaceType.None)] to the class:

[ComVisible(true)]
[Guid("71CACDF6-B6CD-4A46-B951-02E5C542852C")]
public interface INumber
{
    ...

[ComVisible(true)]
[Guid("B5809A32-A066-42E3-96D7-09FE622BC994")]
[ClassInterface(ClassInterfaceType.None)]
public class Number : INumber
{
    ...

(I got the GUIDs by using the GUID utility that comes with Visual Studio. You can find it at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools)

That's it for the C# side. After you build the C# DLL, you need to register it with COM by using the Regasm.exe command-line utility. Start the command prompt and type the following commands:

"C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\vsvars32"

Regasm ExampleDLL.dll /codebase /tlb

(You may need to modify the first one if your VS installation path is different.)

Now you can use the DLL from C++ like this:

#include <iostream>
#import "ExampleDLL.tlb" // This is a file that should have been generated by Regasm.exe

using namespace std;
using namespace ExampleDLL;

int main()
{
    CoInitialize(NULL);

    INumberPtr pNumber;
    pNumber.CreateInstace(__uuidof(Number));
    pNumber->Value = 5;
    cout << pNumber->Value;

    return 0;
}

For more details, see Exposing .NET Framework Components to COM.

Community
  • 1
  • 1
user1610015
  • 6,561
  • 2
  • 15
  • 18
  • thanks for your answer. Can you tell what is the use of [Guid("71CACDF6-B6CD-4A46-B951-02E5C542852C")] part? is that just a id value? – Anushka Ekanayake Jul 16 '15 at 09:19
  • should we add the generated .tlb file to the project properties? – Anushka Ekanayake Jul 16 '15 at 09:22
  • Yes, GUID stands for Globally Unique Identifier. Every class and interface in COM has a GUID associated with it. You can create new GUIDs with the utility located at C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\Tools\guidgen.exe. – user1610015 Jul 16 '15 at 09:29
  • As for the .tlb file, no, you don't need to add it to project properties. – user1610015 Jul 16 '15 at 09:30
  • 1
    ok thanks for that. another question is should we add the dll file or tlb file to the project properties or just use as #import is enough? – Anushka Ekanayake Jul 16 '15 at 09:32
  • Just #import is enough. Just make sure that when the program is run, both the C++ .EXE and the C# DLL are on the same folder. – user1610015 Jul 16 '15 at 09:37
  • ok I'll try. Thanks for your valuable guidance – Anushka Ekanayake Jul 16 '15 at 10:06
  • I got an error when I'm try to generate tlb with the above given command. I got this error message. "RegAsm : error RA0000 : Unable to locate input assembly 'ExampleDLLNew.dll' or one of its dependencies." But I dont have used any dependencies. How can I resolve this issue – Anushka Ekanayake Jul 21 '15 at 06:27
  • I don't think it's because of dependencies, you just specified the name of the DLL wrong, or the DLL is in a folder other than the command prompt's current folder. Use the "cd" command to change the command prompt's current folder, or specify the full path of the DLL (like "Regasm C:\example\ExampleDLL.dll /codebase /tlb") – user1610015 Jul 21 '15 at 07:18
  • ok. thanks. I'll check it aganin – Anushka Ekanayake Jul 21 '15 at 08:53
  • it's working. perfect. (y) – Anushka Ekanayake Jul 21 '15 at 14:29
  • If you have VS 2015 and above, you can use the Tools->Create GUID and choose the GUID attribute format – Ofir Nov 02 '17 at 10:14