Consuming C # Functions (dll) in a C / C ++ project

8

I need to call / reuse some functions I have in a DLL, developed in C #, within a project made in C.

I've already looked for some things and what I managed to make legal work was this project: link

This step-by-step article describes how to call a native C ++ DLL. It works cool.

My problem is that this process uses Windows registry and the generation of the .tlb file. This is causing me a lot of trouble, because when I make any changes to my DLL, the C program stops working, even recompiling everything.

Then looking for I found this other method: link

It explains how to consume a C # library in C / C ++ without the use of exporting and importing .tlb (which would give me less headache).

I believe this second method is more efficient and has a much better interface.

The problem is that this second method is much more complicated and I do not have a very large domain in C / C ++. I would like to know if anyone has already had this problem and could give me some help.

    
asked by anonymous 30.04.2014 / 13:24

2 answers

5

I already had to do this, and I took a slightly different approach (I tested this procedure in Visual Studio 2012 and 2013).

For the test, I created a C # DLL, called the LibraryCSharp.dll , with the following class and namespace:

using System;
using System.Collections.Generic;
using System.Text;

namespace BibliotecaCSharp
{
    public class Teste
    {
        public int Soma(int a, int b)
        {
            return a + b;
        }
    }
}

Now, on the C ++ side, in another project, to run a code from a library in .Net (be it C #, VB etc), the C ++ project must be configured to support the CLR, as shown in the figure: p>

Oncetheprojectisconfigured,itisnecessarytoimporttherequiredlibraries,whichisdonewithinthestdafx.hfileofyourprojectinC++,justaddtheselinestoit:

//Includesebibliotecasespecíficasdoruntimepara.Net#include<mscoree.h>#include<vcclr.h>#pragmacomment(lib,"mscoree.lib")

From now on, you can instantiate classes written in C #, or VB.Net directly from within the C ++ code.

This example project in C ++ is a simple console application, with only one cpp file, but it serves to demonstrate the technique:

#include "stdafx.h"

//Em todos os arquivos que forem acessar a classe, deve-se
//importar as bibliotecas e namespaces
#using <mscorlib.dll>
#using "<CAMINHO DA DLL EM C#>\BibliotecaCSharp.dll"

using namespace System;
using namespace BibliotecaCSharp;

int SomaCSharp(int a, int b)
{
    //Cria uma instância da classe desejada
    Teste^ t = gcnew Teste();

    return t->Soma(a, b);
}

int _tmain(int argc, _TCHAR* argv[])
{
    int s = SomaCSharp(1, 2);

    _tprintf(TEXT("Soma em C#: %d\n"), s);

    return 0;
}

However, as it is, the code in C ++ will give error during execution, because #using "<CAMINHO DA DLL EM C#>\BibliotecaCSharp.dll" is only for the compiler to find the library, and not for the program to find the library when is running.

To this end, I always copy the dll in C # to the same folder as the executable in C ++, as shown in the figure below:

ItwouldbepossibletocopytoanotherfolderaslongasitisinthePATHsystem,butIfindthesolutiontousethesamefoldersimpler.

Justasacuriosity,thiscopycanbeautomatedthroughthePost-buildeventcommandline,asshowninthefigure:

    
01.05.2014 / 16:26
1

To prevent changes in your C # written DLL from having an effect on your C ++ program, preventing it from working properly, try to fix the global identifier of your module by using the instructions contained in this link .

See:

// 
// declare FilgraphManager as a COM coclass 
// 
[ComImport, Guid("E436EBB3-524F-11CE-9F53-0020AF0BA770")] 
class FilgraphManager
{ 
}

Public interface names should also not be changed.

    
02.05.2014 / 05:59