Invoke Win32 Api Method in Window and Web Form

What is Win32 API ?

The Win32 API (also known as the Windows API) is a C-based framework for creating Windows applications.The Windows API can be used in all Windows-based applications. The same functions are generally supported on 32-bit and 64-bit Windows.

The Microsoft Windows application programming interface (API) provides services used by all Windows-based applications. You can provide your application with a graphical user interface; access system resources such as memory and devices; display graphics and formatted text; incorporate audio, video, networking, or security.

This is the library named USER32.DLL in Win32.

This allows managing the entire user interfaces, such as

Windows,Menus,Dialog Boxes,Icons etc.,

How to use Win32/User32 in Window and Webform application?

If you have requirement to use any window function which is not available in any C# namespace. Let's say for example, you want to detect or identify click event of File Menu/Tab in word or excel. How can you do? You can achieve through call Win32 Dll in your code.

Here are few steps to use or call Win32 Dll.

  1. Add Namespace of external dll as like in following code.

    using System.Runtime.InteropServices;
     
  2. Add following line in your class

    [DllImport("User32.dll")]

    public static extern int MessageBox(int h, string m, string c, int type);
     
  3. Add following code in button click event

    // Call our external MessageBox in Button Click event.
    MessageBox(0, "My Web Form Message Box", "MessageBox Demo", 0);
     
  4. Build and Run your application and see output.

Reference link to see more details in Windows API

http://msdn.microsoft.com/en-us/library/vstudio/bb384843.aspx
http://msdn.microsoft.com/en-us/library/vstudio/cc433218.aspx