Calling C++ from .Net Core

Vladimir Akopyan
Quickbird
Published in
3 min readDec 4, 2016

--

I was wondering what it takes to call native code from C# in .Net Core. Fortunately the P\Invoke APi is the same as it was in the old .net framework, and basically the same code works fine. The older tutorials contain quite a bit of information on the subject. But what does it take to create a DLL that you can use from C#?

Invoking native code

Guidance from microsoft

You can follow those guides, and withing 3 lines of code you’ll be able to summon a UI element using the native Win32 API

//Define external function to call
[DllImport("user32.dll")]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, int options);

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

Note that the [DllImport("user32.dll")] part can be used as a normal filepath, to point directly at any dll you want to use.

Creating the DLL

I was looking around for the simplest way to create a DLL that can be used this way. Choose Empty general C++ project in visual studio.

Go to project properties and change the output type to a dynamic link library

We will imagine our DLL is just a collection of functions, so let’s remove an entry point. That way compiler won’t frow a hissie fit when it does not find the main() function

We will be quick and dirty, and define the function in a header file. Add a header file with the following content:

#ifndef MATH_HPP
#define MATH_HPP
extern "C"
{
__declspec(dllexport) int __stdcall math_add(int a, int b) {
return a + b;
}
}
#endif

Add a .cpp file with #include "math.h" Without it, the compiler will ignore the header file.

Compile the DLL in release mode. It is important to choose x64 if your computer supports it. .Net Core will run in 64 bit mode, and if the dll is 32 bit, using it will fail! Note the location where the dll got saved.

Using the DLL

using the DLL is quite straight-forward. Create a .Net Core console application, and insert this code:

using System.Runtime.InteropServices;namespace NetCore
{
public class Program
{
// Insert correct filePath
[DllImport(@"C:\...\WindowsNative.dll", EntryPoint =
"math_add", CallingConvention = CallingConvention.StdCall)]
public static extern int Add(int a, int b);
public static void Main(string[] args)
{
int result = Add(1 ,2);
Console.WriteLine("result is {0}", result);
//Halts the program
Console.ReadKey();
}
}
}

Next Steps

I am yet to get around to testing this with linux. There is a fairly in-depth guide to writing ddl’s for linux by microsoft. Note that the DLLs will have a different file extension — .so instead of .dll, and that needs to be managed.

--

--

Making Internets great again. Slicing crystal balls with Occam's razor.