API programming with User32.dll


New member
Mar 1, 2013
Programming Experience
Hi Guys,

A quick view of my background: Just got back into programming two weeks ago. I've decided to go with c# this time around as my prior experience was mainly in VB. I've made some good progress with c# but am hitting a wall when it comes to using API and User32.dll.

Many years ago I used VB to make programs that used API functions to control other applications. I'd like to use API to do the same thing with c# but am having some trouble.

I've posted two code excerpts below and have posted questions at the bottom of the post in regards to the code snippets.

Below is code copied from How to: Simulate Mouse and Keyboard Events in Code.

[COLOR=Green]// Get a handle to an application window.[/COLOR] [DllImport([COLOR=#A31515]"USER32.DLL"[/COLOR], CharSet = CharSet.Unicode)] [COLOR=Blue]public[/COLOR] [COLOR=Blue]static[/COLOR] [COLOR=Blue]extern[/COLOR] IntPtr FindWindow([COLOR=Blue]string[/COLOR] lpClassName,     [COLOR=Blue]string[/COLOR] lpWindowName);  [COLOR=Green]// Activate an application window.[/COLOR] [DllImport([COLOR=#A31515]"USER32.DLL"[/COLOR])] [COLOR=Blue]public[/COLOR] [COLOR=Blue]static[/COLOR] [COLOR=Blue]extern[/COLOR] [COLOR=Blue]bool[/COLOR] SetForegroundWindow(IntPtr hWnd);  [COLOR=Green]// Send a series of key presses to the Calculator application. [/COLOR] [COLOR=Blue]private[/COLOR] [COLOR=Blue]void[/COLOR] button1_Click([COLOR=Blue]object[/COLOR] sender, EventArgs e) {     [COLOR=Green]// Get a handle to the Calculator application. The window class [/COLOR]     [COLOR=Green]// and window name were obtained using the Spy++ tool.[/COLOR]     IntPtr calculatorHandle = FindWindow([COLOR=#A31515]"CalcFrame"[/COLOR],[COLOR=#A31515]"Calculator"[/COLOR]);      [COLOR=Green]// Verify that Calculator is a running process. [/COLOR]     [COLOR=Blue]if[/COLOR] (calculatorHandle == IntPtr.Zero)     {         MessageBox.Show([COLOR=#A31515]"Calculator is not running."[/COLOR]);         [COLOR=Blue]return[/COLOR];     }      [COLOR=Green]// Make Calculator the foreground application and send it  [/COLOR]     [COLOR=Green]// a set of calculations.[/COLOR]     SetForegroundWindow(calculatorHandle);     SendKeys.SendWait([COLOR=#A31515]"111"[/COLOR]);     SendKeys.SendWait([COLOR=#A31515]"*"[/COLOR]);     SendKeys.SendWait([COLOR=#A31515]"11"[/COLOR]);     SendKeys.SendWait([COLOR=#A31515]"="[/COLOR]); }

The following code was taken from Using Windows APIs from C#, again! - CodeProject.

int hwnd=0; IntPtr hwndChild=IntPtr.Zero;  //Get a handle for the Calculator Application main window hwnd=FindWindow(null,"Calculator"); if(hwnd == 0) {     if(MessageBox.Show("Couldn't find the calculator" +                         " application. Do you want to start it?",                         "TestWinAPI",                         MessageBoxButtons.YesNo)== DialogResult.Yes)     {         System.Diagnostics.Process.Start("Calc");     } } else {              //Get a handle for the "1" button     hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","1");          //send BN_CLICKED message     SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);      //Get a handle for the "+" button     hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","+");          //send BN_CLICKED message     SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);      //Get a handle for the "2" button     hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","2");          //send BN_CLICKED message     SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);      //Get a handle for the "=" button     hwndChild = FindWindowEx((IntPtr)hwnd,IntPtr.Zero,"Button","=");          //send BN_CLICKED message     SendMessage((int)hwndChild,BN_CLICKED,0,IntPtr.Zero);  }


In the first snippet of code sendkeys are utilized. I've found, with testing, that sendkeys are totally unreliable so what can I use in place of send keys? There must be another way to control other windows right? I vaguelly recall programmers doing this with VB back in the day though it was mostly sendkeys with timeouts as far as I can remember.

In the second snippet of code you can see FindWindowEx is used to get the handle for a particular button. What happens here is the code is reading the button's caption to determine its handle. The problem I'm seeing is that changes in Windows 7 (maybe Vista too?) have done away with the captions on the windows. My API spy picks up no button caption. So how in the world do I click a button if I cannot identify it with its caption/text? There must be a way, no?

User32.dll, it looks like it has everything I need to build the type of program I have in mind. But... I have no clue how to use it. I've googled the hell out of it and have only found a handful of code snippets for each function with either a vague description on what the function can be used for or no description at all. Can anyone help in this area? I need to learn these functions and how to use them.

Thanks in advance!
Top Bottom