Click here to Skip to main content
15,849,907 members
Articles / Programming Languages / C#

Integrating an unmanaged C++ .lib with C#

Rate me:
Please Sign up or sign in to vote.
4.11/5 (8 votes)
6 Mar 2011CPOL3 min read 54.8K   2.3K   34   7
Demonstrates integrating Port95NT's unmanaged DLPortIO.lib with C#.


In the C++ world, many DLLs offer a static .lib that simplifies the use of the DLL - you just link in the .lib and you don't need to mess with all the LoadLibrary/GetProcAddress stuff. The managed equivalent is accomplished - very simply - by adding a reference. And, of course, unmanaged DLLs' functionality can be imported on a function-by-function basis via the DllImport attribute. However, I was curious to see if it were possible to use a C .lib in the C# world; it is, and this article demonstrates how to do it. I used Port95NT for my demo, but of course you can do this for any unmanaged .lib. Certainly, some readers will immediately point out that using the .lib isn't necessary, that the DllImport attribute does this for you and with a lot less complexity. And they're correct, of course, but I was simply curious to explore the use of a static .lib in the managed world and couldn't gauge the relative complexity without having done it.


I've been using Port95NT's DlPortIO.dll to read/write bits to my parallel port for many years with C++ by using the associated DlPortIO.lib and a custom wrapper class. Since I've moved to (mostly) C#, I wanted to create a managed assembly to standardize the interface to DlPortIO.dll. I started by using the DllImport attribute, then for kicks took a left turn into exploring the use of the .lib that comes with the DLL.

Using the Code

The attached zip contains two VS2008 projects:

  • A managed C++ wrapper DLL (DlPortIOWrapper) that presents the unmanaged .lib's functions as managed methods.
  • A C# console app that demos the use of the wrapper DLL.

DlPortIOWrapper is brought into any managed project by simply adding a reference to it.

DlPortIOWrapper exposes a few managed C++ classes containing static methods, and their use is pretty simple. I learned the technique from approach #3 in this post.

namespace DaveChambers_DlPortIOWrapper
    public ref class U8 
        U8 () 

        static UCHAR Read(ULONG port)
            return DlPortReadPortUchar(port);

Pretty straightforward stuff. Your C# code then simply calls:

DaveChambers_DlPortIOWrapper.U8.Write(addr, value);

Again, nothing special there. However, I was intrigued by the use of the ref keyword in the C++ class - I had never seen it before (caveat: I haven't done much managed C++... I jumped directly from unmanaged C++ to C#). You can learn about ref here.

Points of Interest

As mentioned, this was really just an exercise in figuring out if and how one uses a static .lib in the managed world. In the end, it wasn't really all that useful in my project because it would be simpler to just use the DllImport attribute to pull the functions directly from the DLL. But I thought that someone might have a more useful need for using a .lib, so I wrote this up with the hope that someone might find it useful or at least interesting.

If you have a need for controlling hardware via the parallel port, Port95NT continues to be a useful tool. Visual Studio is a really great "control" environment for developing firmware via the parallel port... it's really nice to have a super-powerful IDE (e.g.: Edit & Continue) when the other end of the wire is controlled by an ICE (they're never friendly). Though Port95NT was originally created to solve WinNT's hardware abstraction and has "been out of print" for over a decade, copies of it have been cached all over the Web and it remains easy to find. Installation on WinXP is straightforward, but installation on Win7 requires an interesting trick to make it install properly - see this link for details. Full details, plus a downloadable Port95NT installer, plus the sample code attached here can be found at my Port95NT page here.


  • 6th March, 2011: Initial version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer
United States United States
I can type "while" and "for" very quickly

Comments and Discussions

GeneralMy vote of 4 Pin
0olong9-Jul-13 8:30
0olong9-Jul-13 8:30 
GeneralGood and continue. Pin
Shawn-USA5-Jun-13 10:25
Shawn-USA5-Jun-13 10:25 
Thanks for sharing.

Generalhelpful Pin
BillW3311-Apr-11 6:20
professionalBillW3311-Apr-11 6:20 
GeneralMy vote of 2 Pin
DenisK14-Mar-11 8:27
professionalDenisK14-Mar-11 8:27 
GeneralA little confusion of terms - Static vs. Import libraries Pin
Gordon Brandly11-Mar-11 8:44
Gordon Brandly11-Mar-11 8:44 
GeneralRe: A little confusion of terms - Static vs. Import libraries Pin
DLChambers11-Mar-11 9:46
DLChambers11-Mar-11 9:46 
GeneralMy vote of 5 Pin
Tage Lejon7-Mar-11 1:37
Tage Lejon7-Mar-11 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.