Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
SQL Server

Calling Win32 API Calls from C# using Platform Invoke

Author: Aadil

In this article I’ll try to explain how you can call Win32 API functions from within C# using PInvoke and .NET Interpretability

Although the Microsoft .NET framework provides many API’s to use such as for stream based IO it provides the System.IO namespace and if you want to work with windows, forms or dialogs the System.Forms and System.Drawing namespaces contain lots of use full classes, there are times when you need to go beyond these capabilities.


Microsoft Windows contains lots of functions available to be used in the form of Win32 API. There are functions for managing windows, working with sockets, directly playing with the waveform API and more. If you want to use these functions in your c# program then there is a way called PInvoke or platform invokes.


In this you re declare the win32 function in your c# program in a little different way and just calls the function as usual. You need to be careful in defining the parameter types and return types of the win32 procedures in your c# program. Pointers must be declared with IntPtr type or using the UInt32 class.


I’ve created a simple small program that uses the AnimateWindow win32 API function to do some animation effect. Here is the code


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Runtime.InteropServices;


namespace WindowsApplication1


  public partial class Form1 : Form


    // Wi32 API Calls


    const uint AW_HOR_POSITIVE = 0x1;

    const uint AW_HOR_NEGATIVE = 0x2;

    const uint AW_VER_POSITIVE = 0x4;

    const uint AW_VER_NEGATIVE = 0x8;

    const uint AW_CENTER = 0x10;

    const uint AW_HIDE = 0x10000;



    static extern bool AnimateWindow(IntPtr hWnd, UInt32 time, UInt32 flags);



    public Form1()





    private void button1_Click(object sender, EventArgs e)


      AnimateWindow(this.Handle, 1000, AW_VER_POSITIVE | AW_HOR_NEGATIVE | AW_HIDE);





Be careful in declaring the constants, in win32 they usually are in hexadecimal numbers so you need to write them accordingly.

Article Comments
very good and useful article

Posted on 1/30/2007 12:24:24 AM by srilata

Thank you

Posted on 2/3/2008 2:58:18 PM by Rami shareef

Thank you.
it's very useful article for using API
functions in .net code.

Posted on 9/17/2008 8:09:06 AM by Madhukar

It is first article, I read for win32 API .
It is very Nice..

Posted on 3/9/2009 8:15:23 AM by Akshay Rampure

It's very good assignment

Posted on 1/2/2010 8:49:43 AM by Jaysingh

Its very charming and usefull,but after clicking the button form dissappear. How to get back the form....

Posted on 5/18/2010 11:40:04 AM by Madhu

Add Article Comment:
Name :
Email Address :
Comments :
<< Symmetric Encryption and Decryption in .NET 2.0 using C#

Disclaimer - Privacy
© 2002-2017