How to load a C library in C #?

0

I have a lib extension file, done in C, which I need to use in an application that has been programmed in C #. When looking for the subject, I read about wrappers , but I did not quite understand it. Is there any way to use this library with the language features of my application?

    
asked by anonymous 14.07.2014 / 20:10

5 answers

1

Follow the steps below:

  • 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 .lib library.
  • Call each wrapper function built in step 1 through your C # application.
  • This blog explains how to do this in detail.

        
    14.07.2014 / 20:45
    0

    As @EduardoFernandes said, the only way is to use a DLL. But you have to follow other steps. Let's say 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 GNU GCC g ++ : 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 .o file to append 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 deployment library", which I think is the most logical thing to do. p>

    When it will be used 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 good to remember that this is not valid for DLLs that have C ++ classes. It is necessary that you use some tool for this. One that I know of is the PInvoke Interop SDK .

        
    14.07.2014 / 22:30
    0

    Some tips that may help.
    To mount the interop signature for modules written in C / C ++ you can use this tool: PInvoke Interop Assistant You paste the routine signature that you want to call, via managed code, and it mounts the corresponding build that should be used. Another important detail, to pass strings on calls give preference to use the class StringBuilder, because it facilitates a lot in case of release of resources.

        
    15.07.2014 / 02:59
    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 will be able to create a DLL that exports managed classes that can be used without problem in C # / net.

    It's much simpler than generating a DLL and using PInvoke.

        
    15.07.2014 / 03:06
    -3

    A .lib directly, I've never seen it. In general, a C DLL is written and consumed in the .NET via P / Invoke.

    The process comes down to picking up the headers (.h file) that describe DLL function calls and writing an equivalent C # class. If you do not understand much about C types, it can be very laborious.

    In my blog, link has two articles explaining the subject. Search for "how to access unmanaged C # DLLs" and you will find them.

        
    14.07.2014 / 21:29