Important Update
The Guide Feature will be discontinued after December 15th, 2023. Until then, you can continue to access and refer to the existing guides.
Author avatar

Dániel Szabó

Invoking the Unmanaged

Dániel Szabó

  • Aug 23, 2019
  • 11 Min read
  • Aug 23, 2019
  • 11 Min read
Languages Frameworks and Tools


Every code that we write that relies on the .NET framework is called managed code. It is managed code as there are facilities provided by the framework which provide security and garbage collection. However, C# provides us with the possibility to call unmanaged code which is either hidden in a DLL or written entirely in another programming language like C or C++. COM components and the Win32 API also fall into the unmanaged code category.

When the .NET framework came to life, it was a requirement to provide support for calling and integrating unmanaged code into C# applications. This was not solely for C# but every language that relies on the .NET framework.

This support comes in the form of two main services. The first one is InteropServices and the second is Platform Invoke Services.

There are four criteria, one of which you need to meet to consume unmanaged code.

  1. Identify functions in DLLs (specify the function name and the DLL which holds it)
  2. Create a class to holds/group DLLs
  3. Create prototypes in managed code
  4. Call a DLL function.

Simple messagebox

The messagebox will come from the user32.dll. It allows you to show a small message box with a caption and a message. This DLL provides facilities and functions that are far more capable than the messagebox but, for this demonstration, it should be fine.

In order to use the DLL, we need to start our application with the following line.

1using System.Runtime.InteropServices;

This gives us the DllImport function that is able to load a specified DLL.


This statement loads the DLL to our disposal. our namespace is almost ready to manipulate the messagebox and show our messages.

We need to define the structure of the messagebox which needs to be exactly the same as in the DLL.

1static extern int MessageBoxA(int hWnd, string strMsg,
2                              string strCaption,uint iType);

The hWnd is going to give us a handle for the owner of the window. The strMsg will be the message we want to show in our window; it's of the type LPCTSTR that comes from the DLL. The strCaption is the caption of our message box which is of the same type as the strMsg. Finally, we have the iType which is an unsigned integer, this controls the contents and behavior of our dialog box.

Some examples of iType variants are:

  1. 0x00000002L -> gives us Abort,Retry,Ignore buttons.
  2. 0x00000006L -> gives us Cancel, Try Again, Ignore buttons.
  3. 0x00000004L -> gives us Yes, No buttons.

After this is done, all that is left to do is to complete our Main function and call the external code.

1static void Main(string[] args)
3	MessageBoxA(0,
4				"Hello, Pluralsight!",
5				"Welcome to the written guides!",
6				Convert.ToUInt32(0x00000002L));

The result looks like this.

The maps

As you recall from the enlist, the value 0x00000002L results in three buttons: the Abort the Retry, and the Ignore. Since this is a demonstration we don't really care about what happens at these button clicks. But you need to know that you should handle each event appropriately, otherwise it can result in an application crash. The messagebox returns an integer when a button is clicked and this is how you handle the associated events based on the clicked button. The Convert.ToUInt32() function is necessary because the argument type is unit but the documentation holds the sample values in long datatype, which are not directly convertible.

Let's look at an example of the return value of these buttons. We need to modify the code to look like this:

1int buttonPushed = MessageBoxA(0,
2	"Hello, Pluralsight!",
3	"Welcome to the written guides!",
4	Convert.ToUInt32(0x00000002L));
6    Console.WriteLine($"The button push resulted in value: {buttonPushed}");

Pushing the Abort button gives us the following output.

1The button push resulted in value: 3

Pushing the Retry button gives us the following output.

1The button push resulted in value: 4

Last, but not least, the Ignore gives us the following output.

1The button push resulted in value: 5

Note a common mistake that developers make: they don't read the API documentation and they pass either incompatible or not implicitly compatible arguments to the functions or classes coming from unmanaged code. This results in application crashes and lots of headaches. Save yourself some time and ALWAYS read the documentation.

C++ Unmanaged Code Wrapper

This part of the guide will show you how you can invoke unmanaged C++ code and build a project around it. You will need to have the CLI package installed for C++ in Visual Studio and, please note, this example was only tested Visual Studio 2017.

We need to create a new project called Core; this should be an empty Visual C++ project. Once the project is generated, you need to right-click on the project go to properties. On the General tab, change the Configuration Type to Static Library (.lib). Then, under the C/C++ - Precompiled Headers, set the Precompiled Header to Not Using Precompiled Headers.

Now, right-click on the Core in the solution explorer and add a new header file called Worker.h. This is, basically, a worker with a salary.

1#pragma once
2namespace Core
4	class Worker
5	{
6	public:
7		const char* m_Name;
8	private:
9		float m_Salary;
10	public:
11		Worker(const char* name, float salary);
13		void raiseSalary(float amount);
14		inline float GetSalary() const { return m_Salary; };
15	};

We need another header file called Core.h. This is for further extensibility and you can skip creating this file without repercussions. But, if you do create it, make sure you include the following two lines in it.

1#pragma once
2#include "Worker.h"

Last, but not least, we need real code to power our app.

1#include "Worker.h"
2#include <iostream>
3namespace Core
5	Worker::Worker(const char* name, float salary)
6		: m_Name(name), m_Salary(salary)
7	{
8		std::cout << "Successfully created Worker object!" << std::endl;
9	}
10	void Worker::raiseSalary(float amount)
11	{
12		m_Salary *= amount;
13		std::cout << "Raised salary of Worker" << m_Name << " to " << m_Salary << std::endl;
14	}

Now we need a new project. Right-Click on our project and add a new project called Wrapper. This should be a Class Library under Visual C++ - CLR.

First, we define our Worker.h header file.

1#pragma once
2#include "ManagedObject.h"
3#include "../Core/Core.h"
4using namespace System;
5namespace CLI
7	public ref class Worker : public ManagedObject<Core::Worker>
8	{
9	public:
11		Worker(String^ name, float salary);
12		void raiseSalary(float amount);
13		property float Salary
14		{
15		public:
16			float get()
17			{
18				return m_Salary->GetSalary();
19			}
20		private:
21			void set(float value)
22			{
23			}
24		}
25	};

Our ManagedObject.h gives us the flexibility to further extend the app. It also provides a means for the destructor to the Garbage Collector to clean up after the app execution. This is a template that can be found on the internet, as it is very common to use it.

1#pragma once
2using namespace System;
3namespace CLI {
5	template<class T>
6	public ref class ManagedObject
7	{
8	protected:
9		T* m_Instance;
10	public:
11		ManagedObject(T* instance)
12			: m_Instance(instance)
13		{
14		}
15		virtual ~ManagedObject()
16		{
17			if (m_Instance != nullptr)
18			{
19				delete m_Instance;
20			}
21		}
22		!ManagedObject()
23		{
24			if (m_Instance != nullptr)
25			{
26				delete m_Instance;
27			}
28		}
29		T* GetInstance()
30		{
31			return m_Instance;
32		}
33	};
36using namespace System::Runtime::InteropServices;
37static const char* string_to_char_array(String^ string)
39	const char* str = (const char*)(Marshal::StringToHGlobalAnsi(string)).ToPointer();
40	return str;

In order to link the Core application with the Wrapper, we need to add it as a reference. We need to right-click onto the Wrapper project, then Add/Refernce, select the Core, and then click apply.

The last thing we need to do is to create an application in C# which will invoke the unmanaged code.

We need to create our HR project, which is a C# project, from the (.NET Framework) type.

This is all the code we need to have in there:

1using System;
2using CLI;
3namespace HR
5    class Program
6    {
7        static void Main(string[] args)
8        {
9            Worker daniel = new Worker("Dániel Szabó", 100000);
10            Console.WriteLine($"The worker: {daniel.Name} salary before the raise is: {daniel.Salary}");
11            daniel.raiseSalary(1.1);
12            Console.WriteLine($"The worker: {daniel.Name} salary after the raise is: {daniel.Salary}");
14            Console.Read();
15        }
16    }

Before we can run this application, we need to build the Wrapper project. This is done by right-clicking on the project in the solution and then clicking Build. Now, the last thing we need to do is to set HR to be the default startup project. This is done in the solution explorer too. Just right-click on the project and then click the Set as StartUp Project.

Now we can start our app and, if the instructions were followed thoroughly, we should see the following:

1Successfully created Worker object!
2The worker: Dániel Szabó salary before the raise is: 100000
3Raised salary of Worker Dániel Szabó to 110000
4The worker: Dániel Szabó salary after the raise is: 110000


As there are as many programming languages as there are stars in the sky, it's hard to tell you a general rule of thumb which answers the question, "How to call unmanaged code from C#?" The internet is a wonderful thing, full of examples, and I encourage you to try and find the solution for your questions. The two examples I have shown you will only get you started.

As you saw, calling existing DLLs and integrating them to your app is easier than building your own DLLs and mapping them to your C# to be called as unmanaged code. The beauty of the latter is the control it gives you over the application. Since you provide the DLLs, you are able to shape them to your needs. I honestly hope you found what you were looking for and the time you spent reading this was something one might call worthwhile!