P/Invoke Library - Visual Studio Add-in
Introduction
This article describes the PInvokeLib Visual Studio Add-in. This tool will help you organize P/Invoke signatures that you use regularly in your application. Hopefully you will not find this tool to be outdated since it provides several features that you will not find in other tools.
Background
One website that I visit regularly is pInvoke.net. This is pretty cool website with a web service back-end. Similarly, it provides a Visual Studio add-in to communicate with their service and let you insert P/Invoke signatures in your program. I'm sure this website will continue to serve the .NET community for years to come. PInvokeLib is intended to complement such tool by letting you organize your own library as well. In fact, you can almost cut and paste functions from MSDN or your own header files. Only minor modifications may be required. PInvokeLib supports the following programming languages: C#, VB .NET and MC++.
Getting started
PInvokeLib Manager has two tabs. The Module tab is where you can insert P/Invoke signatures. Currently, only functions and structures can be added. Use the Define Tab to define your functions and structures. You can Add, Update, Delete and Search a definition. Search is limited to selected module only. Search can also use wildcard character (*).
Inserting API Function
- Select or Add module name
- Type function name
- Type or Paste in function prototype (simply remove extra semi-colon (;))
- Click 'Update' button to add your library
Note: Most C types are already defined and can be reused as is. C pointers other than character string are not supported.
PInvokeLib will convert any unknown types to uppercase and make them easier to find. I believe this will make life easier for you in case you need to define your own struct later.
Inserting Structure
- Select or Add module name
- Type structure name
- Type or Paste in structure definition
Advanced Features
Function argument direction support P/Invoke signatures can be specified to have function arguments passed by value (default), by reference or output only. This is supported only for function prototype.
PInvokeLib supports the following extension for parameter:
- [In] - This specifies that input parameter is being passed by value (this is default if none is present).
- [Out] - This specifies that output parameter is used.
- [In,Out] - This specifies in/out parameter.
For example, let's import LPWSTR PathAddBackslashW( LPWSTR lpszPath)
of the ShlwApi.dll:
To do so, the correct definition would be: LPWSTR PathAddBackslashW( [In,Out] LPWSTR lpszPath)
since the string argument is passed by reference. PInvokeLib suggests:
[DllImport("ShlwAPI.dll")] public static extern String PathAddBackslashW([In,Out]String lpszPath)
But for you to get correct results, the input parameter should be a StringBuilder
.
The following is full example code using this function.
using System; using System.IO; using System.Text; using System.Runtime.InteropServices; namespace PInvokeLib { class PInvoke { [DllImport("ShlwAPI.dll")] public static extern String PathAddBackslashW([In,Out]StringBuilder lpszPath); public static void Main(string[] args) { StringBuilder sb = new StringBuilder(@"c:\temp\temp", 200); PathAddBackslashW(sb); Console.WriteLine("Path with extra slash:"+ sb); } } }
Installation
PInvokeLib addin is fully supported by Visual Studio 2005 and 2008.
Extract all the files in PInvokeAddin_Bin.zip to:
- Visual Studio 2005: <drive:>\Documents and Settings\<username>\My Documents\Visual Studio 2005\Addins
- Visual Studio 2008: <drive:>\Documents and Settings\<username>\My Documents\Visual Studio 2008\Addins
Then make sure you enable the addin from the Addin Manager (Tools->Add-in Manager...)
Start the add-in from context menu (right click in text editor)
Last Updated [10/20/08]
This update will accept #define (integer). You can barely copy and paste full header file. See TestFile.h for all support features.
You may use C or C++ style comments to remove lines that are causing problems.
You may use proper signature __cdecl
or __stdcall
to import your function.
SpecImporter class library - Parser class based on CocoR.
Feel free to use it in your own project.
As always, enjoy!