Share via


Managed C++ Wrapper For Unmanaged Code


This article was originally published at c-sharpcorner and at http://simplesamples.info.

Introduction

It is sometimes necessary or preferred to use C++ to create a managed wrapper around unmanaged code. This article is a simple sample of a managed C++ Class Library that calls unmanaged code.

This project creates a C++ CLR (managed) Class Library called UnmanagedWrap that uses an unmanaged class called Unmanaged. For the purposes of this article, the Unmanaged class has a function called "Hello" that simply calls the Windows API MessageBox function. Normally the MessageBox managed class would be used to show a MessageBox or if the Windows API version were to be used Platform Invoke would be used to call it, but this project uses it in this manner for purposes of this article.

* Note that usually, it is possible to use Platform Invoke (DllImport) to call native DLLs. That should usually be the solution chosen for calling native DLLs from managed code. 

For Who, Why, and When this can be useful

This article should help C/C++ programmers unfamiliar with C# to create a wrapper around C and/or C++ code, so the unmanaged code can be used from managed code. At the same time it should also help C# programmers to use unmanaged code.

One reason it is necessary to use unmanaged code is when software exists as a static library. Another good reason to use unmanaged code is when an API is intended to be used from C++ and is too complicated and technical to be conveniently used from managed code.

There are many details about C++ and unmanaged code that can be difficult for C# programmers to solve, so if you are unfamiliar with C++ this article can help but there might be more details to conquer.

1. Create a C++ CLR Class Library Project

In Visual Studio use "File" | "New" | "Project..." to create the project.

Then in the New Project dialog in "Installed" (at the left) expand "Visual C++" then select "CLR". In the list of templates to the right, select "Class Library". Give the project a name (this article uses the name "UnmanagedWrap"). The "Add New Project" dialog should look as in the following:

Click "OK". When the project has been generated, build it before making any changes to verify that it builds successfully, at least initially.

2. Add a Class Called Unmanaged

There are at least a couple of ways to add a class to the project. One way is to use "Project" | "Add Class....".  Just enter "Unmanaged" into the "Class name" box. The wizard will automatically generate file names for .h and .cpp files. Leave the base class empty (assuming you are not deriving from another class) and leave the "Access" as "public". Leave the "Virtual Destructor", "Inline" and "Managed" checkboxes unchecked. The "Add Class" window should look as in the following:

Click "Ok".

Open the Unmanaged.h file if it is not open. Note that there is not a namespace in the file for the class. At the end of the class (before the "};") add the following line:

int Hello(void);

Then open the Unmanaged.cpp file and add the following three lines:

int Unmanaged::Hello(void) {
    return MessageBox(NULL, L"Hello", L"Unmanaged", MB_OK);
}

3. Update stdafx.h (Add windows.h)

Since the Windows API is being called windows.h must be included. In the Solution Explorer expand the "Header Files" folder and then open the "stdafx.h" file. (Alternatively, you can open the "stdafx.h" file by right-clicking "stdafx.h" in the #include at the top of every cpp file and selecting "Open Document".) Add the following two lines to it:

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>

The first of those lines is not important; it will just make the compile slightly faster.

4. Update UnmanagedWrap.h and UnmanagedWrap.cpp (Add the Hello Function)

Open the UnmanagedWrap.h file. Three things need to be added to the Class1 class; an unmanaged pointer to the Unmanaged class, a constructor that creates an object of the Unmanaged class and a method that calls the unmanaged function. The Class1 class will then look like:

public ref  class Class1
{
public:
    Unmanaged *pu;  // pointer to the Unmanaged class
 
    // the constructor will allocate the pointer pu
    Class1() : pu(new Unmanaged()) {};
 
    int Hello() {
        return pu->Hello();
    };
};

It is extremely normal for unmanaged C++ code (the majority of it) to separate class definitions into a header (h) and an implementation (cpp) file. C++ programmers consider that to be important. Microsoft, however, has designed managed code to be incompatible with the way C++ is normally used. Therefore managed C++ classes are usually implemented entirely in the header (h) file. We do however need to add one line to the UnmanagedWrap.cpp file. Add the following line to it:

#include "Unmanaged.h"

That line, however, must precede the "#include "UnmanagedWrap.h"" line.

5. Build Again and Fix Errors LNK2028 and LNK2019 If Present

Build the project again. If you do not get the following two errors (slightly reformatted here) then skip the following and go directly to testing.

LNK2028 unresolved token (0A00000A) "extern "C" int __stdcall MessageBoxW(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int __cdecl MessageBox(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)

LNK2019 unresolved external symbol "extern "C" int __stdcall MessageBoxW(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBoxW@@$$J216YGHPAUHWND__@@PB_W1I@Z) referenced in function "extern "C" int __cdecl MessageBox(struct HWND__ *,wchar_t const *,wchar_t const *,unsigned int)" (?MessageBox@@$$J0YAHPAUHWND__@@PB_W1I@Z)

If you get those errors then you must modify one more of the project's properties. In the project's properties (if needed) expand the "Configuration Properties" node, then expand the "Linker" node, then select the "Input" node. Then be sure that the "Configuration" in the top left is "All Configurations". Then at the top of the properties is "Additional Dependencies". Assuming that the value is blank, click in the box for the value and an arrow will appear at the right end of the box. Click it and in the drop-down list choose "<inherit from parent or project defaults>". This should tell the linker to look in the user32.dll file for the MessageBox function. The window should then look as in the following:

6. Test

Create or use a separate project to test with. To that project, add a reference to the project created above. Then create an instance of the wrapper class and call the Hello function in the class as in the following (C# code):

UnmanagedWrap.Class1 test = new UnmanagedWrap.Class1();
test.Hello();

See Also

Source Code

Download a complete project with source code from this article including the test program from the gallery at Managed C++ Wrapper For Unmanaged Code.