|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionIn 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 –a brief explanationThe 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. 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 [DllImport("user32.Dll")]
public static extern int EnumWindows(CallBack x, int y);
Understanding
Callbacks:
Note the use of a delegate in the code as shown below: public delegate bool IECallBack(int hwnd, int lParam); Let us now study how the callback is
actually implemented. When you click on the button private void GetIE_Click(object sender, System.EventArgs e)
{
listBoxHandle = listBox1.Handle; //store a listbox handle here.
EnumWindows (new CallBack (IEInstance.EnumWindowCallBack),
(int)listBoxHandle) ;
label1.Text = "Total Instances of Internet Explorer : "+i;
}
private static bool EnumWindowCallBack(int hwnd, int lParam)
{
windowHandle = (IntPtr)hwnd;
listBoxHandle = (IntPtr)lParam;
// getting an instance of listbox from its handle.
ListBox lb =(ListBox)ListBox.FromHandle(listBoxHandle);
StringBuilder sb = new StringBuilder(1024);
StringBuilder sbc = new StringBuilder(256);
GetClassName(hwnd,sbc,sbc.Capacity);
GetWindowText((int)windowHandle, sb, sb.Capacity);
String xMsg = sb+" "+sbc+" "+windowHandle;
if( sbc.Length > 0 )
{
if( sbc.ToString().Equals("IEFrame"))
{
myAl.Add(windowHandle);
i++;
lb.Items.Add(xMsg); }
}
return true;
}
The
delegate in our example called Enumeration
is just one scenario where callback functions are used. Other common
practice of using them is to create message handler routines for objects
like windows. Hence, an API function will require an associated callback
function whenever it wants the program, calling the API, to do some
necessary tasks. Callback functions return zero to indicate failure and
non-zero values to indicate success. You would notice that we have set
the return value of The function
The
function
You
must have observed the use of the type Closing an instance of IEOn clicking on the private void RemoveIE_Click(object sender, System.EventArgs e)
{
int index=listBox1.SelectedIndex;
listBox1.Items.RemoveAt(index);
int count =0;
IEnumerator myEnumerator = myAl.GetEnumerator();
while ( myEnumerator.MoveNext() ) {
if ( count == index )
{
listBoxHandle = (IntPtr)myEnumerator.Current;
break;
}
count++;
}
PostMessage(listBoxHandle,0x0010/*WM_CLOSE*/,0,0);
myAl.RemoveAt(count );
label1.Text = "Total Instances of Internet Explorer :" +myAl.Count;
}
Note
that we have used an ConclusionHope the above article was useful in explaining the concepts. This concept has a lot of benefits like changing an instance of IE from your windows control at run-time.
|
||||||||||||||||||||||