Wednesday, 6 June 2007

Using an unmanaged C++ DLL in C# in Windows

First of all create a C++ library project, and add tools.cpp: extern "C" __declspec(dllexport) int test() { return 42; } Call from C# like this: using System; using System.Runtime.InteropServices; namespace ToolsTest { public class Tools { [DllImport("tools.dll")] public static extern int test(); }

class Program { static void Main(string[] args) { Console.WriteLine(Tools.test()); } } }