How to load a C library into C#?

Asked

Viewed 1,330 times

0

I have an extension file lib, done in C, which I need to use in an application that was programmed in C#. When searching for the subject, I read about Wrappers, but I didn’t understand it very well. Is there any way to use this library with the language resources of my application?

4 answers

1

Follow the steps below:

  1. Make a DLL that serves as a wrapper for your .lib. This DLL is written in C or C++ and should expose each function of the library. lib.
  2. Call each wrapper function built in step 1, through your C application#.

That one blog explains how to do this in detail.

0

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.

0

Some tips that might help.
To mount the module Interop signature written in C/C++ you can use this tool: Pinvoke Interop Assistant You paste the routine signature you want to call, via managed code, and it mounts the corresponding build that should be used.
Another important detail, to pass strings in calls give preference to use the Stringbuilder class, because it makes it much easier in case of resource release.

0

The best way for you to do this, since you have a lib is to use a library C++/CLI in Visual Studio and link with your lib.

With C++/CLI you can create a DLL that exports classes Managed that can be used without problem in C#/. net.

It is much simpler than generating a DLL and using Pinvoke.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.