Click here to Skip to main content
15,868,043 members
Articles / Desktop Programming / Windows Forms
Article

Using Windows APIs from C#, again!

Rate me:
Please Sign up or sign in to vote.
4.15/5 (28 votes)
20 Jun 2006CPOL 186.1K   15.3K   55   19
How to trigger events for controls on another window running in another process.

Sample Image

Introduction

This article shows how to control other windows and trigger events for their controls using Windows APIs.

In this sample, I simply get a handle for the Calculator window using the FindWindow API, get a handle for the Calculator buttons using FindWindowEx, and trigger the Button Click event for any required buttons, using the SendMessage API.

Background

The main idea I want to demonstrate here is that any form/dialog in Windows must have a window handle; with this handle and using the windows related APIs, you can control the form/dialog and trigger events for its controls.

Here's the code. I think it is well commented and needs no more explanation:

C#
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);

}

Points of interest

I think controlling windows in other processes can be very helpful in many situations like:

  1. Ensuring that a main application is running before starting a dependant application.
  2. Automating some tasks.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) HP Enterprise Services (Formerly named EDS)
Egypt Egypt
I'm a senior software engineer experienced in C/C++ programming under different platforms. I like to get acquainted with new technologies that appear so fast day after day like the .NET framework, SQL 2008, etc.
Programming is both my profession and my main hobby.

I enjoy tickling with code using different programming languages on different platforms.

Comments and Discussions

 
QuestionNot working in Win11 Pin
revitarkitek18-Oct-23 8:17
revitarkitek18-Oct-23 8:17 
GeneralMy vote of 5 Pin
Joshua Tristancho19-Mar-23 17:52
professionalJoshua Tristancho19-Mar-23 17:52 
QuestionHelp using this tool Pin
Member 109950716-Aug-14 0:22
Member 109950716-Aug-14 0:22 
AnswerRe: Help using this tool Pin
Rami Helmy26-Jan-15 12:09
Rami Helmy26-Jan-15 12:09 
GeneralMy vote of 5 Pin
Thomas Daniels22-Dec-12 3:14
mentorThomas Daniels22-Dec-12 3:14 
GeneralRe: My vote of 5 Pin
Rami Helmy10-Feb-13 21:28
Rami Helmy10-Feb-13 21:28 
GeneralMy vote of 5 Pin
Keith Barrow6-May-12 9:15
professionalKeith Barrow6-May-12 9:15 
GeneralRe: My vote of 5 Pin
Rami Helmy10-Feb-13 21:29
Rami Helmy10-Feb-13 21:29 
GeneralFinding the title Pin
Sam Shiles28-Aug-09 4:13
Sam Shiles28-Aug-09 4:13 
GeneralHelp reqd for adding a button to a different app Pin
aditya.jk@gmail.com19-Feb-09 10:24
aditya.jk@gmail.com19-Feb-09 10:24 
GeneralRe: Help reqd for adding a button to a different app Pin
Rami Helmy6-Apr-09 9:44
Rami Helmy6-Apr-09 9:44 
GeneralRe: Help reqd for adding a button to a different app Pin
Rami Helmy18-Apr-09 1:37
Rami Helmy18-Apr-09 1:37 
GeneralHelp reqd for adding a button to a different app Pin
aditya.jk@gmail.com19-Feb-09 10:23
aditya.jk@gmail.com19-Feb-09 10:23 
GeneralInvoking on Remote Machine Pin
kpb11-Aug-08 6:27
kpb11-Aug-08 6:27 
GeneralControl WindowsForm application from a console application Pin
Talinu30-Oct-07 9:51
Talinu30-Oct-07 9:51 
GeneralC# .Net - handle issue Pin
sounakbanerjee23-Jan-07 1:48
sounakbanerjee23-Jan-07 1:48 
GeneralRe: C# .Net - handle issue Pin
Michael Sterk23-Jan-07 1:56
Michael Sterk23-Jan-07 1:56 
GeneralMultiple Instances Pin
MrEyes21-Jun-06 1:28
MrEyes21-Jun-06 1:28 
GeneralRe: Multiple Instances Pin
Rami Helmy25-Jun-06 1:41
Rami Helmy25-Jun-06 1:41 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.