Click here to Skip to main content
Click here to Skip to main content
Go to top

Using Windows APIs from C#, again!

, 20 Jun 2006
Rate this:
Please Sign up or sign in to vote.
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:

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)

Share

About the Author

Rami Helmy
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

 
QuestionHelp using this tool PinmemberMember 109950716-Aug-14 0:22 
GeneralMy vote of 5 PinmemberProgramFOX22-Dec-12 3:14 
GeneralRe: My vote of 5 PinmemberRami Helmy10-Feb-13 21:28 
GeneralMy vote of 5 PinmentorKeith Barrow6-May-12 9:15 
GeneralRe: My vote of 5 PinmemberRami Helmy10-Feb-13 21:29 
GeneralFinding the title PinmemberSam Shiles28-Aug-09 4:13 
GeneralHelp reqd for adding a button to a different app Pinmemberaditya.jk@gmail.com19-Feb-09 10:24 
GeneralRe: Help reqd for adding a button to a different app PinmemberRami Helmy6-Apr-09 9:44 
GeneralRe: Help reqd for adding a button to a different app PinmemberRami Helmy18-Apr-09 1:37 
GeneralHelp reqd for adding a button to a different app Pinmemberaditya.jk@gmail.com19-Feb-09 10:23 
GeneralControl WindowsForm application from a console application PinmemberTalinu30-Oct-07 9:51 
GeneralC# .Net - handle issue Pinmembersounakbanerjee23-Jan-07 1:48 
GeneralRe: C# .Net - handle issue PinmemberMichael Sterk23-Jan-07 1:56 
GeneralMultiple Instances PinmemberMrEyes21-Jun-06 1:28 
GeneralRe: Multiple Instances PinmemberRami Helmy25-Jun-06 1:41 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 20 Jun 2006
Article Copyright 2006 by Rami Helmy
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid