M
M
Mars362020-09-23 10:13:34
C++ / C#
Mars36, 2020-09-23 10:13:34

How to connect dll with c++ code to C# project?

You need to use C++ code in a C# project. The only option that I know of is to pack the code into a dll and include it in a C# project. Created a DLL following this instruction: https://docs.microsoft.com/ru-ru/cpp/build/walkthr... . When connecting a dll in C # through links, it throws an error (screen is attached). Tried using the following option:

[DllImport("Dll2.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern double Add(double a, double b);

But VS also swears at him (the screen with errors is attached). How to fix these errors? Or is it really simple?5f6af587e97c2220444257.png5f6af58eaf2ac900979522.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Korotenko, 2020-09-23
@Mars36

Something you read in the
wrong place https://docs.microsoft.com/ru-ru/dotnet/standard/n...

using System;
using System.Runtime.InteropServices;

public class Program
{
    // Import user32.dll (containing the function we need) and define
    // the method corresponding to the native function.
    [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
    private static extern int MessageBox(IntPtr hWnd, string lpText, string lpCaption, uint uType);

    public static void Main(string[] args)
    {
        // Invoke the function as a regular managed method.
        MessageBox(IntPtr.Zero, "Command-line message box", "Attention!", 0);
    }
}

Farther. There should be Dll2.dll next to your project. Nothing else is needed

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question