HID functions and name mangling

While setupapi.lib comes with Platform SDK package the HID library (hid.lib) is distributed within Windows DDK.
In order to use these two static link libraries in one project you need to specify Additional Library Directories for Linker settings.

In my case I used the following string:

	"C:\\Program Files\\Microsoft Visual Studio .NET 2003\\Vc7\\lib";\
             C:\\WINDDK\\2600.1106\\lib\\wxp\\i386

So this will enable you to use static libraries instead of dynamically loading HID.DLL and SETUPAPI.DLL.

To use functions which implementations are contained in LIB files the proper header files are needed.
Some of those header files are also contained in DDK package.
To enable them for your project you must set Additional Include Directories for C++ compiler settings.

In my case it was

	C:\\WINDDK\\2600.1106\\inc\\wxp\\


However if you try to include header files in this way

	#include "hidsdi.h"
	#include "hidpi.h"
	#include "setupapi.h"


you will get a linker error LNK2019 saying that there are some unresolved external symbols.

This happens because statically linked lib files (hid.lib, setupapi.lib) contain “C” style function names.
There are also C++ style naming which is called “mangled names” or “decorated names“.
When C++ compiler is used to create a project it uses some name decorating algorithm.
This algorithm allows to embed some valuable information (like types of the function arguments) into names of
functions so linker will be able to find correct matches for all used names.
For more information on name mangling see Wikipedia on name mangling.

So we need to find a way to tell the compiler that there are some “C” style headers.
You can do it like this:

extern "C" {
	#include "hidsdi.h"
	#include "hidpi.h"
	#include "setupapi.h"
}


Now if all was set up correctly you will be able to use HID functions directly from your code, without dynamically loading HID.DLL like it is made in several examples.

One Response to “HID functions and name mangling”

  1. Cem says:

    I didn’t have the DDK and been pulling my hair on this. Never thought that, mangling would be the problem to the linker error. -till I found your tip.

    Even though, I didn’t use HID stuff, I am glad I learned this.

    Thanks
    Cem

RSS feed for comments on this post. And trackBack URL.

Leave a Reply