Build an C++ dll and use it with C#

At the beginning we will create a DLL file written in C++ . Open Visual Studio which help us do it really quckly. Create the Win32Project and give any name, e.g. “MyCppDLL”:

File -> New -> Project -> Win32Project


Click “OK”

The next window will be an Application Wizard – Overview section -> Just click “Next>” button

It will move you to the Application Setting section:


Select DLL radiobutton.

Check “Empty project”

Click Finish button.

In your new created project add a new file:

File -> New -> File.. 0> Choose “C++ File (.cpp)

Name file e.g: “MainProcedure”. Paste code below into your new opened file:

extern “C”
__declspec(dllexport) double sumFromDLL(double var_x, double var_y)
double result;
result = var_x + var_y;

return result;

Build your project: type on keyboard shortcut: Shift + Ctrl + B.

Now should be create .dll file with the same name as your project. To get location to this file the best way will be if you check the output window (VIEW -> Output) because this file can be located in different places. It is dependent of your build option (Release or Debug). By default this file is located in  Release/Debug directory, but this directory is created for solution and project separatelly (two folders with the same names). I know that it is a little confuset so the easier way will be when you just copy the path for your .dll file from Output window.

Now we have a C++ .dll file. So we need C# application. Create a new C# Console Application project. The steps are similar like for previous project.

In source file paste following code:

using System;
using System.Runtime.InteropServices;

class Program
    [DllImport("MyCppDLL.dll", CallingConvention = CallingConvention.Cdecl)]
    static extern double sumFromDLL(double var_x, double var_y);

    static void Main(string[] args)
        double a = 4.0;
        double b = 5.5;
        double result;

        result = sumFromDLL(a, b);

        Console.WriteLine("{0} + {1} = {2}", a, b, result);

Build this project and run. If solution has been built successfully and project launched without any problems this console should show for your eyes:


I could say that we have finished but let me explain code we have written. Start from C++:

extern "C" – information for compiler that this is external function
__declspec(dllexport) – this Microsoft function allow you to import your own function
__stdcall(dllexport) – function calling conversion, puts the parameters on stack

And here are peaces of C# code:

using System.Runtime.InteropServices; - it allow us to use DllImport attribute.

[DllImport("MyCppDLL.dll", CallingConvention = CallingConvention.Cdecl)]

This line of code above is an C# attribute that import a dll file. First parameter is a DLL file name, note the extension .dll. Second parameter is important when you use .NET 4.0 or higer. IF you don’t use it you will get an exception:

A call to PInvoke function ‘ConsoleApplication1!Program::sumFromDLL’ has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

static extern double sumFromDLL(double var_x, double var_y); - This is our C++ dll function declaration. Simply it is an signature of the external C++ function. Note that the names of the parameters must be the same.

Summarizing I tell you when we should use the DLL libraries. The most important aspect is a security. For example when you write a function which encode a passwords better solution is an C++ language. Programs compiled in C# can be easily decompiled! Another reason is really fast memset C++ function which doesn’t have an .NET equivalent.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s