DllImport Attribute
This page was last reviewed on Sep 29, 2022.
Dot Net Perls
DllImport. This C# attribute, along with dllexport, help enable interop with DLL files in C# programs. We can use a C++ DLL dynamic link library, or a custom legacy DLL.
An intro. For this article, we are not using the CLR in the C++ DLL. If you use CLR, you will have managed C++ code and you can use better interfaces between your C++ code and your C# code.
Settings. We have the DLL set up with the following properties. In Visual Studio's Solution Explorer, you can right-click on the DLL project and look at the Property Pages.
Configuration Type: Dynamic Library (.dll) Use of MFC: Use Standard Windows Libraries Use of ATL: Not Using ATL Common Language Runtime Support: No Common Language Runtime Support Whole Program Optimization: Use Link Time Code Generation
Dllexport, C++. The DLL and the C# EXE need to communicate. You do this by defining extern "C" functions in your C++ DLL. There is a special syntax for doing this that you must use.
Info The next block of code shows an extern function you put in your DLL—this is done in the C++ code.
// This code should be put in a header file for your C++ DLL. It declares // an extern C function that receives two parameters and is called SimulateGameDLL. // I suggest putting it at the top of a header file. extern "C" { __declspec(dllexport) void __cdecl SimulateGameDLL (int a, int b); }
Extern, continued. The __declspec(dllexport) part is standard and not required to be referenced. Our C# GUI will call this function from an UnsafeNativeMethods class.
Next Let us look at the extern function in the same header file in the C++ DLL.
// The keywords and parameter types must match // ... the above extern declaration. extern void __cdecl SimulateGameDLL (int num_games, int rand_in) { // This is part of the DLL, so we can call any function we want in the C++. // The parameters can have any names we want to give them and they don't need to match the extern declaration. }
DllImport, C#. First compile your DLL and make sure it is in the same directory as the C# Windows Forms GUI. Let's look at the code we need to write in the C# interop code.
Info We use a class for the extern declaration in the C# program. The function name is the same as the one in the C++ DLL.
Detail We can use a const string for filename of the DLL. The DllImport("name.dll") must have the name of the C++ you are building.
/// <summary> /// A C-Sharp interface to the DLL, written in C++. /// </summary> static class GameSharp { /// <summary> /// The native methods in the DLL's unmanaged code. /// </summary> internal static class UnsafeNativeMethods { const string _dllLocation = "CoreDLL.dll"; [DllImport(_dllLocation)] public static extern void SimulateGameDLL(int a, int b); } }
Call C++ methods. Look at the class GameSharp from the previous example. We need to add another static method to that class so that the rest of the C# program can use the native method.
static class GameSharp { // [code omitted, see above example] /// <summary> /// Simulate N games in the DLL. /// </summary> public static void SimulateGameCall(int num) { UnsafeNativeMethods.SimulateGameDLL(num, new Random().Next()); } }
Discussion. There are many problems that can happen, and many of them probably will. Next in this document we list some possible errors and hint at yet more problems.
Warning Avoid extern DLLs unless it is necessary. A fully managed C# program is safer and not too much slower.
DLL name incorrect. Obviously, if your DLL name is not the same in both places, the code won't work. Errors often occur in places we cannot detect them.
Also The DLL export method must have the same name, although its parameter names aren't important.
Detail You must use MarshalAs for when you have a char* pointer, although there are other options.
A summary. We implemented simple DLL interoperation using the DllImport and dllexport keywords in the C# language and the C++ language. This can make an old C++ DLL work with a C# GUI.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Sep 29, 2022 (edit).
© 2007-2024 Sam Allen.