Page 1 of 1

Win32 API Interop in C#

#1 raziel_  Icon User is offline

  • Like a lollipop
  • member icon

Reputation: 464
  • View blog
  • Posts: 4,255
  • Joined: 25-March 09

Posted 12 July 2011 - 03:08 PM

Hi there :)

In this tutorial we will talk about Win32 API Interop. First lets see what API is. API(application programming interfaces) is sum of functions that usually are in a .dll files. There are lots of APIs out there for example there is free API for Skype that you can use to have control on elements of Skype and do mods for example.

But in this tutorial we will talk about Windows API. Win32 API for short is used to have control on elements of Windows for example processes, buttons, toolbars etc. Windows API resides in .dll files like
  • User32.dll- Provide access to elements like Windows forms, buttons etc.
  • Kernel32.dll- Provide access to elements like file system, processes etc.
  • GDI.dll - Provide access to elements like monitors and other output devices

And other .dlls you can check Wikipedia for more information.
So how do we use Windows API in C#? First you need to use System.Runtime.InteropServices namespace which allow you to use DLL Import Attribute which allow you to use unmanaged APIs in your programs. For more information MSDN

So lets begin with the coding. We will use Win32 API to disable the minimize button of a windows form. So create new Windows Forms Application (WindowsFormsApplication1 is mine). Import the namespace:
using System.Runtime.InteropServices;


Now lets Declare the functions that we will use in our program. To disable the minimize button we will use two functions
SetWindowLongA and
GetWindowLongA like so:

    public partial class Form1 : Form
    {
        //Importing Windows API functions
        [DllImport("user32")]
        public static extern int SetWindowLongA(int hwnd, int nIndex, int dwNew);
        [DllImport("user32")]
        public static extern int GetWindowLongA(int hwnd, int nIndex);



So here is how to use them first add the constants to use in our functions:
        //Constants for minimize box and style
        private const Int32 WS_MINIMIZE=0x20000; //0x20000 is for minimize button 0x10000 is for maximize button 

0x30000 is for both etc.
        private const Int32  GWL_STYLE = -16;        



Now add button (button1 is mine) and lets disable the button. In the button click event add this code:
        private void button1_Click(object sender, EventArgs e)
        {
            //Disable minimize button
            int intStyle;
            intStyle = GetWindowLongA(this.Handle.ToInt32(), GWL_STYLE);// Get the form current style
            intStyle = intStyle & ~ (WS_MINIMIZE); //Disable the button 
            intStyle = SetWindowLongA(this.Handle.ToInt32(), GWL_STYLE, intStyle); //Send the new style to the form
        }



As you can se using the API function GetWindowLongA we get the current style of the form then we disable the button using boolean algebra and then send the new style to the form using SetWindowLongA API function. Here is the full code in the form:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using System.Runtime.InteropServices;
namespace WindowsFormsApplication1
{

    public partial class Form1 : Form
    {
        //Importing Windows API functions
        [DllImport("user32")]
        public static extern int SetWindowLongA(int hwnd, int nIndex, int dwNew);
        [DllImport("user32")]
        public static extern int GetWindowLongA(int hwnd, int nIndex);

        //Constants for minimize box and style
        private const int WS_MINIMIZE=0x20000;
        private const int  GWL_STYLE = -16;

        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Disable minimize button
            int intStyle;
            intStyle = GetWindowLongA(this.Handle.ToInt32(), GWL_STYLE);// Get the form current style
            intStyle = intStyle & ~ (WS_MINIMIZE); //Disable the button 
            intStyle = SetWindowLongA(this.Handle.ToInt32(), GWL_STYLE, intStyle); //Send the new style to the form
        }

    }
}



So here is how to use Win32 API in C# i hope you find it helpful :)

Is This A Good Question/Topic? 3
  • +

Replies To: Win32 API Interop in C#

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,957
  • Joined: 21-March 08

Posted 21 July 2011 - 01:30 PM

Also, don't forget about PInvoke.Net for the signatures of just about every Win32 API call and code to call them.
Was This Post Helpful? 2
  • +
  • -

Page 1 of 1