Click here to Skip to main content
13,763,663 members
Click here to Skip to main content
Add your own
alternative version


57 bookmarked
Posted 19 Feb 2008
Licenced CPOL

P/Invoke Library - VS2005 & 2008 Add-in

, 26 Feb 2008
Rate this:
Please Sign up or sign in to vote.
The article describes the P/Invoke Library Visual Studio add-in.

PInvokeLib Module

PInvokeLib Define


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 outdated since it provides several features not found in other tools.


One website that I visit regularly is This is a 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 a tool by letting you organize your own library as well. In fact, you can almost cut and paste functions from the 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

The 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 modules only. Search can also use the wildcard character (*).

Inserting an API Function

  • Select or add a module name
  • Type the function name
  • Type or paste in the function prototype (simply remove the extra semi-colon (;))
  • Click the 'Update' button to add your library

    Note: Most C types are already defined, and can be reused as is. C pointers other than character strings 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 a Structure

  • Select or add a module name
  • Type the structure name
  • Type or paste in the 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 prototypes.

PInvokeLib supports the following extensions for parameters:

  • [In] - This specifies that the input parameter is being passed by value (this is default if none is present).
  • [Out] - This specifies that the 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:

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 a full example code using this function:

using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;

namespace PInvokeLib
    class PInvoke
        public static extern String PathAddBackslashW([In,Out]StringBuilder lpszPath);

        public static void Main(string[] args)
           StringBuilder sb = new StringBuilder(@"c:\temp\temp", 200);
           Console.WriteLine("Path with extra slash:"+ sb);


The PInvokeLib add-in is fully supported by Visual Studio 2005 and 2008. Extract all the files in 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 add-in from the Add-in Manager (Tools->Add-in Manager...). Start the add-in from the context menu (right click in the text editor).

Running PInvokeLib Add-in


Last Updated [02/26/08]

This update will accept #defines (integer). You can barely copy and paste the 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 the proper signature __cdecl or __stdcall to import your function.

The SpecImporter class library is available - a parser class based on CocoR. Feel free to reuse this library in your own program.

As always, enjoy!


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


About the Author

Ernest Laurentin
Software Developer (Senior)
United States United States
Ernest is a multi-discipline software engineer.
Skilled at software design and development for all Windows platforms.
Interests: User Interface, GDI/GDI+, Scripting, Android, iOS, Windows Mobile.
Programming Skills: C/C++, C#, Java (Android), VB and ASP.NET.

I hope you will enjoy my contributions.

You may also be interested in...

Comments and Discussions

GeneralNice Pin
Shawn-USA5-Jun-13 11:00
memberShawn-USA5-Jun-13 11:00 
QuestionSpecImporter Pin
Member 810089618-Feb-13 4:49
memberMember 810089618-Feb-13 4:49 
AnswerRe: SpecImporter Pin
Ernest Laurentin18-Feb-13 14:05
memberErnest Laurentin18-Feb-13 14:05 
QuestionIs this supposed to connect to the wiki to get definitions? Pin
TonyM7120-May-08 12:58
memberTonyM7120-May-08 12:58 
AnswerRe: Is this supposed to connect to the wiki to get definitions? Pin
Ernest Laurentin22-May-08 14:31
memberErnest Laurentin22-May-08 14:31 
QuestionWhere are u man? Pin
samMaster3-Mar-08 2:06
membersamMaster3-Mar-08 2:06 
AnswerRe: Where are u man? Pin
Ernest Laurentin3-Mar-08 5:19
memberErnest Laurentin3-Mar-08 5:19 
GeneralI can`t see it on menu bar Pin
samMaster1-Mar-08 0:15
membersamMaster1-Mar-08 0:15 
GeneralRe: I can`t see it on menu bar Pin
Ernest Laurentin3-Mar-08 5:17
memberErnest Laurentin3-Mar-08 5:17 
GeneralRe: I can`t see it on menu bar Pin
samMaster5-Mar-08 19:39
membersamMaster5-Mar-08 19:39 
GeneralCopyrights Pin
Zorbek29-Feb-08 6:16
memberZorbek29-Feb-08 6:16 
AnswerRe: Copyrights Pin
Ernest Laurentin29-Feb-08 6:52
memberErnest Laurentin29-Feb-08 6:52 
GeneralThe Add-in 'PInvokeLib' failed to load or caused an exception Pin
Graham Downs25-Feb-08 20:27
memberGraham Downs25-Feb-08 20:27 
AnswerRe: The Add-in 'PInvokeLib' failed to load or caused an exception [modified] Pin
Ernest Laurentin25-Feb-08 20:53
memberErnest Laurentin25-Feb-08 20:53 
GeneralRe: The Add-in 'PInvokeLib' failed to load or caused an exception Pin
Graham Downs25-Feb-08 21:46
memberGraham Downs25-Feb-08 21:46 
AnswerRe: The Add-in 'PInvokeLib' failed to load or caused an exception Pin
Ernest Laurentin26-Feb-08 4:24
memberErnest Laurentin26-Feb-08 4:24 
NewsRe: The Add-in 'PInvokeLib' failed to load or caused an exception Pin
Ernest Laurentin26-Feb-08 18:09
memberErnest Laurentin26-Feb-08 18:09 
GeneralVista UAC Pin
ermancetin21-Aug-08 9:27
memberermancetin21-Aug-08 9:27 
GeneralWishes Pin
Marcus.Sonntag21-Feb-08 0:22
memberMarcus.Sonntag21-Feb-08 0:22 
GeneralRe: Wishes [modified] Pin
Ernest Laurentin24-Feb-08 10:25
memberErnest Laurentin24-Feb-08 10:25 
GeneralCLR Inside Out Pin
Ernest Laurentin20-Feb-08 6:03
memberErnest Laurentin20-Feb-08 6:03 

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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web06-2016 | 2.8.181112.3 | Last Updated 26 Feb 2008
Article Copyright 2008 by Ernest Laurentin
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid