EDN Admin
Well-known member
Hi Guys,
A quick view of my background: Just got back into programming two weeks ago. Ive decided to go with c# this time around as my prior experience was mainly in <acronym title="Visual Basic VB</acronym>. Ive 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 <acronym title="Visual Basic VB</acronym> to make programs that used API functions to control other applications. Id like to use API to do the same thing with c# but am having some trouble.
Ive 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.
// Get a handle to an application window. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); // Send a series of key presses to the Calculator application. private void button1_Click(object sender, EventArgs e) { // Get a handle to the Calculator application. The window class // and window name were obtained using the Spy++ tool. IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator"); // Verify that Calculator is a running process. if (calculatorHandle == IntPtr.Zero) { MessageBox.Show("Calculator is not running."); return; } // Make Calculator the foreground application and send it // a set of calculations. SetForegroundWindow(calculatorHandle); SendKeys.SendWait("111"); SendKeys.SendWait("*"); SendKeys.SendWait("11"); SendKeys.SendWait("="); }
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("Couldnt 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); }Questions:
In the first snippet of code sendkeys are utilized. Ive found, with testing, that sendkeys are totally unreliable sowhat 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 <acronym title="Visual Basic VB</acronym> 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 buttons caption to determine its handle. The problem Im 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. Ive 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!
View the full article
A quick view of my background: Just got back into programming two weeks ago. Ive decided to go with c# this time around as my prior experience was mainly in <acronym title="Visual Basic VB</acronym>. Ive 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 <acronym title="Visual Basic VB</acronym> to make programs that used API functions to control other applications. Id like to use API to do the same thing with c# but am having some trouble.
Ive 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.
// Get a handle to an application window. [DllImport("USER32.DLL", CharSet = CharSet.Unicode)] public static extern IntPtr FindWindow(string lpClassName, string lpWindowName); // Activate an application window. [DllImport("USER32.DLL")] public static extern bool SetForegroundWindow(IntPtr hWnd); // Send a series of key presses to the Calculator application. private void button1_Click(object sender, EventArgs e) { // Get a handle to the Calculator application. The window class // and window name were obtained using the Spy++ tool. IntPtr calculatorHandle = FindWindow("CalcFrame","Calculator"); // Verify that Calculator is a running process. if (calculatorHandle == IntPtr.Zero) { MessageBox.Show("Calculator is not running."); return; } // Make Calculator the foreground application and send it // a set of calculations. SetForegroundWindow(calculatorHandle); SendKeys.SendWait("111"); SendKeys.SendWait("*"); SendKeys.SendWait("11"); SendKeys.SendWait("="); }
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("Couldnt 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); }Questions:
In the first snippet of code sendkeys are utilized. Ive found, with testing, that sendkeys are totally unreliable sowhat 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 <acronym title="Visual Basic VB</acronym> 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 buttons caption to determine its handle. The problem Im 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. Ive 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!
View the full article