Calling Native Win32 Functions in .NET? PInvoke.NET to the Rescue!

If you’re a .NET developer that needs to call native functions from Windows API, you might be interested in the PInvoke.NET’s website.  According to the front page of the web site,

PInvoke.net is primarily a wiki, allowing developers to find, edit and add PInvoke signatures, user-defined types, and any other information related to calling Win32 and other unmanaged APIs from managed code (written in languages such as C# or VB.NET).

.NET developers worldwide can easily contribute to the community, sharing their valuable knowledge, whenever they have time to do so.

As defined by Wikipedia,

Platform Invocation Services, commonly referred to as just P/Invoke, is a feature of Common Language Infrastructure implementations, like Microsoft’s Common Language Runtime, that enables managed code to call native code in dynamic-linked libraries (DLLs).  The native code is referenced via metadata that describes functions exported from a native DLL.

For instance, if I need to call a function to shutdown or reboot Windows, then I’ll just select the user32.dll under the "Desktop Functions" section, then click on the ExitWindowsEx function.  Here’s a sample of what you might see for this function.

Jump to PInvoke.net

So next time you’re wondering how to call a native Windows function from your C# or VB.NET code, visit www.pinvoke.net, select the DLL where the function is located and follow the guidelines described in the web site.

Another way to access the Windows API is directly from within Visual Studio.  As a matter of fact, the web site offers a free downloadable PInvoke.NET add-in for Visual Studio so that you can look up and easily select the function you need.  This free add-in is provided by Red Gate Software, makers of ANTS Profiler and other great developer tools.  Here are a few snapshots of the add-in in action (the pictures are from PInvoke.NET)

image

image

This post has been viewed: 645 times. kick it on DotNetKicks.com

 

Similar posts you might be interested in reading:

Leave a comment

Powered by WP Hashcash