Tom’s Blog

December 11, 2008

Creating a Managed Wrapper for a Lib File

Filed under: General .NET — Tom Shelton @ 12:51 pm

So, you have a lib file and it’s corresponding header file, and you need to use it from your managed application.  Now what?  Well, the simplest answer is to create a C++/CLR wrapper DLL to expose the functionality you need.  Here’s a step-by-step walk through of the process.

 

1.

The first step in this walk-through is to create lib file that we want to expose in our managed code.  To do this, create a blank Visual Studio Solution and give it a name.

  001
   

2.

Once we have a solution, lets add a C++ static library project to the solution.  To do this, go to Solution Explorer and right click on the solution.  On the context menu, select “Add -> New Project”.   This should bring up the “Add New Project” dialog.  Add a new C++ Win32 project.  Give it a name, and select OK.

  002
   

3.

When the “Win32 Application Wizard” comes up, select the “Next” button.  Under Application Type, select “Static Library”.  Also, for this example I turned off “Precompiled header”.  And select “Finish”.

  003
   

4.

Go to the Solution Explorer again, and right click on your static library project, and select “Add -> Class…”.  This will bring up the “Add Class” dialog, select “Add”.

  005
   

5.

Fill in the information in the “Generic C++ Class Wizard”.  Select “Finish”.

  006
   

6.

Modify the resulting AddClass.h file to look like this:

 
   1: #pragma once
   2:  
   3: namespace AddTwoNumbersLib
   4: {
   5:     class AddClass
   6:     {
   7:     public:
   8:         static double Add (double x, double y);
   9:     };
  10: }
   

7.

Let’s provide the implementation in AddClass.cpp:

 
   1: #include "AddClass.h"
   2:  
   3: namespace AddTwoNumbersLib
   4: {
   5:     double AddClass::Add(double x, double y)
   6:     {
   7:         return x + y;
   8:     }
   9: }
 

At this point, you should be able to successfully compile our example lib file.

   

8.

OK – we are now setup for the rest of this example.  We have a lib file and a header file that we will expose to our managed application.  To do this, we will create a new CLR class library, using C++/CLI.  So, go back to the Solution Explorer and right click on the solution.  Select “Add -> New Project…”.   In the Project Types treeview, select “Visual C++ -> CLR”.  In the Templates pane, select “Class Library”, and give it a name and select OK.

  007
   

9.

Now we need to set a couple of project properties to be able to use the lib file.  So, in Solution Explorer, right click on the ClrWrapper project and select Properties.  In the “ClrWrapper Property Pages”,  select “All Configurations” in the Configuration drop down.  Then, select “Configuration Properties -> C/C++ -> General”.  Modify the “Additional Include Directories” property, to point to the directory containing the header file for the .lib.

  008
 

Selecting the browse button will bring up the “Additional Include Directories” dialog.

  009
 

Clicking in the list at the top, will again give you a browse button that will bring up a folder browser dialog.  Use the folder browser to point to the directory where your lib files header is in.

  010
 

Select OK, then Apply in the “ClrWrapper Property Pages”.

   

10.

Now, we need to tell the linker where the lib file is.  We could do this by adding a project reference to the AddTwoNumbersLib project – but, since we are trying to simulate us only having the lib, we’ll do it the hard way!

With the “ClrWrapper Property Pages” dialog still open, “Configuration Properties -> Linker -> Input”.

  011
 

Select, “Additional Dependencies” and then the browse button that appears.  This will bring up the “Additional Dependencies” dialog.

  012
 

In the list at the top, insert the full path to the lib file (make sure it’s in quotes).

  013
 

Select OK.  And OK again on the “ClrWrapper Property Pages”.

   

11.

Now, lets create our wrapper.  Modify the following files

 

stdafx.h

 
   1: // stdafx.h : include file for standard system include files,
   2: // or project specific include files that are used frequently,
   3: // but are changed infrequently
   4:  
   5: #pragma once
   6:  
   7: #include "AddClass.h"
 

ClrWrapper.h

 
   1: // ClrWrapper.h
   2:  
   3: #pragma once
   4:  
   5: using namespace System;
   6:  
   7: namespace ClrWrapper {
   8:  
   9:     public ref class AddClass
  10:     {
  11:     public:
  12:         double Add (double x, double y);
  13:     };
  14: }
 

ClrWrapper.cpp

 
   1: // This is the main DLL file.
   2:  
   3: #include "stdafx.h"
   4:  
   5: #include "ClrWrapper.h"
   6: using namespace ClrWrapper;
   7:  
   8: double AddClass::Add(double x, double y)
   9: {
  10:     return AddTwoNumbersLib::AddClass::Add (x, y);
  11: }
 

With that done, you should be able to compile the solution.

   

12.

Now, create a managed project in the language of your choice – C# or VB, and add a reference to your the ClrWrapper.dll.  Once done, you should be able to write, compile, and run code like the following:

 

C#

 
   1: using System;
   2: using ClrWrapper;
   3:  
   4: namespace CSharpClient
   5: {
   6:     class Program
   7:     {
   8:         static void Main ( string[] args )
   9:         {
  10:             AddClass addClass = new AddClass ();
  11:  
  12:             Console.WriteLine ( addClass.Add ( 1, 2 ) );
  13:             Console.WriteLine ( addClass.Add ( 2, 2 ) );
  14:             Console.WriteLine ( addClass.Add ( 649, 1 ) );
  15:         }
  16:     }
  17: }
 

VB

 
   1: Option Explicit On
   2: Option Strict On
   3:  
   4: Imports System
   5: Imports ClrWrapper
   6:  
   7: Module Program
   8:  
   9:     Sub Main()
  10:         Dim a As New AddClass()
  11:  
  12:         Console.WriteLine(a.Add(1, 2))
  13:         Console.WriteLine(a.Add(2, 2))
  14:         Console.WriteLine(a.Add(649, 1))
  15:     End Sub
  16:  
  17: End Module

Hopefully, someone will find this useful.

6 Comments »

  1. Excellent!!! It surely worked for me!

    Comment by Daniel — December 7, 2009 @ 1:14 pm

  2. Hi,

    We would like to launch few flash based application (Presentation with swf or exe)Any idea how this wrapper can help?

    Comment by Harish — March 10, 2010 @ 2:36 am

  3. Hi, I have a .lib file and its corresponding .h file ( from a third party developer and I don’t have the source code). I want to use them in c# application. I tried the method you talked about in this page. If I create a lib file as you said in this article, everything works fine and I can work with the class and methods inside the library. but when I replace my library with the main library that I want to work with, I get this error:
    “Error 1 error C2059: syntax error : ‘__declspec(dllimport)’ “. I don’t know anything about c++ and don’t have access to any one who knows it now.

    Comment by Sahar — July 20, 2010 @ 2:06 pm

  4. I really, really want to thank you, Tom Shelton! From probably a week I’m trying to do exactly the same and this is the first tutorial that is so detailed and helpful! Thanks again!!!

    Comment by Evgeni Krustev — September 14, 2010 @ 4:51 pm

  5. Thanks so much for this, it was very helpful. Exactly what I needed.

    Comment by Andy — January 28, 2014 @ 3:09 pm

  6. Thanks a whole bunch. This has saved me hours of unnecessary work.

    Comment by Anon — March 18, 2014 @ 7:18 am

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress