Call a C ++ library from C #

This question may seem to be a repetition of the previous ones. I read a series of messages, but did not fully understand my situation.

I have a C ++ library that is created using the flashic IDE. I have to use this library in a C # project.

Someone worked on this project before they handed me over. There are currently 2 layers to make this possible. First, the C ++ project includes a complete library with a C ++ wrapper. This project creates a dll as an output. This C ++ dll is then passed to the C # project, which has dllimport calls in the C ++ dll. This C # project creates a dll again. Finally, in order to use the library in a C # application, I have to include a link to both of these libraries.

Is this the right way to make it work? I thought there probably should be a way to simplify the process.

Can someone please help me with this question?

+5
source share
3 answers

Given that you are using a C ++ library, I assume that it uses C ++ semantics, such as classes, and not just expansion procedures. If this is the case, then, as a rule, this is done using a manually created C ++ managed library of interactions.

Basically, you create a C ++ managed library in Visual Studio, reference an existing C ++ library, and create a managed wrapper around existing C ++ classes. Then you reference this (managed) C ++ assembly in your C # project and include the original (unmanaged) C ++ library in your C # assembly in the same way as the file that is placed in the assembly directory.

, , ++, P/Invoke (DllImport).

, # P/Invoke.

, ( , ++, ++ #, , -, ++ #) , . .

+7

, , , , - - - . DllImport "" , , ++/CLI-, -. ++/CLI, ( ++) ( # .NET ), , ++ .

, , , . 20 , .

+2

:

DllImport

class Example
{
    // Use DllImport to import the Win32 MessageBox function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode)]
    public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

    static void Main()
    {
        // Call the MessageBox function using platform invoke.
        MessageBox(new IntPtr(0), "Hello World!", "Hello Dialog", 0);
    }
}
0

All Articles