As @Eduardofernandes said, the only way is by using a DLL. But you have to follow other steps. Suppose you downloaded Mingw (or TDM) GCC for Windows. Create the file in C++:
#ifdef BUILDING_DLL
#define __DLL__ __declspec(dllexport)
#else
#define __DLL__ __declspec(dllimport)
#endif
//Totalmente necessário (não sei por que, mas sei que funções em C++ causam crash). Só com wrappers você pode usar classes e outras coisas de C++ em sua aplicação C#.
extern "C"
{
/* coloque as funções aqui. Sempre use o seu prefixo __DLL__ (os nomes que eu escrevi são como FOO e BAR, pode escolher o que você quiser. */
int __DLL__ func() { return 0; }
}
Compile with the g++ of GNU GCC:
g++ -c -DBUILDING_DLL -LC:\CaminhodaLib -lSuaLibParaLinkar arquivo_dll.cpp -o arquivo_dll.o
-D specifies our Preprocessor, -c specifies the use to generate a non-executable file and -o is the name of the output file. Soon after a file .o
for attach in applications or be compiled. Then proceed with the following command:
g++ -shared arquivo_dll.o -o sua_dll.dll -mwindows --out-implib
. -Shared specifies to generate a dll, -mwindows is required (of course, C#...), --out-implib means "compile to an external implementation library", which I think is the most logical to do.
When using in your application, use it as follows:
[DllImport("sua_dll.dll")]
public class Implementacao
{
public extern int func();
public void uso_da_dll()
{
func();
}
}
It is worth remembering that this is not valid for Dlls that have C++ classes. It is necessary that you use some tool for this. One I know is the Pinvoke Interop SDK.