Tools Used : VS.NET Final Release
In this article we are going to study how to find the running instances of Internet Explorer on your machine. This article will also provide an insight on the windows API and how to call Win32 API functions from C#. You will also learn how to implement callback functions.
In the code I have extensively used the Win 32 API functions as shown below:
[DllImport("user32.Dll")]
public static extern int EnumWindows(CallBack x, int y);
[DllImport("User32.Dll")]
public static extern void GetWindowText(int h, StringBuilder s, int nMaxCount);
[DllImport("User32.Dll")]
public static extern void GetClassName(int h, StringBuilder s, int nMaxCount);
[DllImport("User32.Dll")]
public static extern IntPtr PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
As we go about, I would explain the need for these different functions. In order to use the Win32 API functions like EnumWindows (..), and the like we need to import the Win32 DLLs that implement these functions.
Win32 API a brief explanation
The Windows API has a number of functions, constants, callback functions, messages and structures written in C/C++ that you can call and use in your programs. They perform lower level services on the OS such as memory management, process switching, disk access etc. They help the application to interface directly with the processor. Win 32 API is created for 32-bit processor. It has a platform independent nature. A large part of the API functions are implemented using some of these libraries like Kernel32.dll (Operating system kernel functions), User32.dll (user interface functions) etc. These DLL (Dynamic Link Library) files are found wherever the Windows System directory is located. These DLL files export functions thus allowing external programs to use their functions. You would notice that I have used the concept of handles in the program. A handle is an internal structure in Windows that keeps track of the properties of any object (like windows, buttons, icons, menus, files etc) in the system. A program is able to manipulate these objects via the handle to the object. A handle itself is just a 32-bit integer and its significance becomes apparent when it is passed as a parameter to an API function from a program calling those functions.
Implementation of Win32 API in C#
That said let us get back to where we had left ourselves. In C#, to access these external API functions we need the namespace System.Runtime.InteropServices. This namespace provides a collection of classes useful for accessing COM objects, and native APIs. One of the important classes present in it is the DllImportAttribute, which is used to define the dll location that contains the implementation of an extern method like EnumWindows (..) of the native system API user32.dll of Win32 API. Shown below is the way the code is written in C#:
[DllImport("user32.Dll")]
public static extern int EnumWindows(CallBack x, int y);
extern keyword indicates that the method is implemented externally. The extern methods must be declared as static. Have a quick look at the screen-shot before we try to understand how we have done it.
