PInvoker Logo
The PInvoke Interface Generation Tools Site

Importing a Windows SDK Header File and Dll

This walkthrough guides you through the steps necessary to produce a pinvoke based interop assembly for the functions in kernel32.dll.

The example images show the results from a fully licensed version of PInvoker.

Trial version results may differ since they only export the first 10 methods of the specified input dll.

Step 1: Select a Header File

After running PInvoker, set the header file you want to import. Do this by clicking the '...' button to the right hand side of the 'header file' text box.

Add dll file

In this walkthrough we are concentrating on importing a windows API dll, so you need to select the windows.h file in the file/open dialog box which opens after clicking the button.

Windows header files reside in the following locations :

  • Visual Studio 2005:
    In the Program Files/Microsoft Visual Studio 8/VC/PlatformSDK/Include folder.
  • Visual Studio 2008:
    In the Program Files/Microsoft SDKs/Windows/v6.0A/Include folder.

Tip : You can also drag and drop header files from windows explorer into P/Invoker.

Step 2: Add a dll file

Click the '+' button to the right of the dll file list and select the required dll file to import.

Add dll file

In this case you should select kernel32.dll. This is in the Windows/System32 folder.

You can add multiple dll files to import, but only one header. You can create a seperate .h file specifically for PInvoker containing multiple #includes if necessary.

Tip : Again you can drag and drop dlls into PInvoker to add them to the import list.

Step 3: Export

Select the Export/Export interface dll menu item and set the file name for the pinvoke interface dll in the following file/save dialog box.

Export interface

In this example you should set the output file name to windows.interop.dll.

After setting the output dll file name you will be presented with a progress dialog. After a while, when the export is completed, you should get the following message :

Export finished

The warnings which are reported as hidden are either from unsupported C++ features in the windows headers (templated classes or methods, classes with instance methods etc) or because a method was found in the dll but not in the header file.

If you examine the output folder you'll see that not only has your pinvoke interop dll been created but also a copy of PInvoker.Marshal.Dll. The interface dlls produced by PInvoker depend on this assembly which contains the marshaling classes such as ArrayPtr and StructPtr.

This dll is verifyable and freely distributable. It is placed in the export folder to make the job of adding references to Visual Studio projects easier

Step 4: Add to a Visual Studio Project

Create a new C# console application in visual studio. To use the exported pinvoke interop assembly you need to add it to the references to the project along with the PInvoker.Marshal.Dll assembly.

You do this by right clicking the project's references node in the solution explorer tree.

Add references

You can now use the functionality inside the interface assembly to access the native dll.

Please see the PInvoker FAQ  for details of how the exported assembly is structured and used in code.

Or go to the downloads page to download the Visual Studio C# example projects demonstrating how to use exported assembly.

Step 5: Viewing the contents of the exported dll

To view the contents of the exported dll it is recommended you use an IL disassembler such as ILDASM  or Reflector.

By default methods in the dll appear in a class named NativeFunctions in a namespace of the same name as the header file name.

In our above example, since we imported the windows.h file then the exported methods will appear in the Windows.NativeFunctions namespace.

If you are using a trial version the number of methods in this class will be small : trial versions of PInvoker only export the first 10 methods exported by the dll.

However if you are using a full version and you examine the root namespace of the assembly in Reflector  you will see the following.

Reflector image of root namespace

Examining the NativeFunctions class will show you all the methods in the imported dll for which PInvoker has generated an interface.

Reflector image of NativeFunctions class

Alternatively looking at the NativeConstants class will show you the Windows constants which PInvoker has exported to the pinvoke interop assembly.

Reflector image of NativeConstants class

To see the output of the Windows SDK kernel32.dll using a licensed version of PInvoker download the C# Sample Projects from the downloads page.

Copyright © Kilapara Limited 2019
Contact us