Share via


Platform Invoke

Someone asked me, "Why you are so unmanaged?" I said, "Because I work in unmanaged code." and burst into laughter. The gentleman could not understand why I am laughing, but said, "Sometimes you are managed." I could not control myself to laugh and said, "Because sometimes I work in managed code." The gentleman continued to ask, "How do you do that from unmanaged to managed?" Oh My God, "Platform Invoke" I said and keep laughing for few minutes. The gentleman was from Finance domain, so later I explained him the meaning of unmanaged, managed and its conversion in an abstract way. I got an idea to write a blog post about what I know about Platform Invoke.

Before the advent of Microsoft .NET Framework, the code we used to write in C/C++ language was unmanaged and with .NET Framework the code we write in .NET language ( C#/VB/VC++) is managed. Why the code written in .NET is managed? Because, as a simplest example, when we allocate a memory for any object in .NET, programmer is not responsible to de-allocate/release it, it will be taken care by CLR/GC, so managed code, but in legacy/native programming, if we do the same, we are leaking memory. Programmer is responsible to do this, there is no CLR/GC, hence unmanaged code.

The post is not to discuss any of the .NET feature and compare it with unmanaged stuffs. The article is about Platform Invoke. What is it and how do we do it?

.NET Framework class library provides a set of classes to the developers of .NET applications to use it and build applications around it. The class library, internally uses native Win32 API calls to perform the actual task. Sometimes, you will be stuck If you want to do a particular task in your .NET application and there is no class that provides this functionality, It can only be done by a native Win32 API call, e.g. LogonUser(). There is no equivalent class/method to LogonUser() in .NET Framework class library. What will you do? Platform Invoke.

How do I convert a Win32 API to its equivalent P/Invoke signature?

There are couple of ways to do this. First is, Learn P/Invoke, write code and convert API to its .NET equivalent and then call from .NET managed code.

There has been large amount of text available to learn P/Invoke. Microsoft and MSDN sites have many documents available, several websites offers tutorials. There are several blogs that talk about the various aspects of P/Invoke. I will try to give references to some of the articles at the end of this post.

There are tools available for this purpose. Selecting the name of Win32 API or structure, the tool gives the P/Invoke signature of the API.

Tool from CLR team - BCL Team Blog : P/Invoke Interop Assistant [Justin Van Patten] and download - Managed, Native, and COM Interop Team - Release: PInvoke Interop Assistant

Links of interest--

BCL Team Blog

Comments on: The P-Invoke Interop Assistant

CLR Inside Out: Marshaling between Managed and Unmanaged Code

CLR Inside Out: Best Practices For Managed And Native Code Interoperability

MSDN Magazine: CLR Inside Out

Managed And Native Code Interoperability - Bing

Platform Invoke Tutorials - Bing

 Nitin Dhawan

Windows SDK - Microsoft

Comments

  • Anonymous
    August 22, 2009
    There is also the tool PInvoker (http://www.pinvoker.com) that can be used to generate .NET signature from the unmanaged methods :)