I am currently working on writing a simple program to automatically update Firefox using Firefox's built in update service (accessed from menu -> help -> about). I am opening Firefox using ShellExecute and sending the keystrokes 'Alt + h' and 'a' to the open Firefox window using sendInput. My code currently works at opening the update menu, but I am using a system sleep method call to give enough time for Firefox to open before calling sendInput. Is there any way for me to check to see whether or not Firefox has opened and is ready to receive keyboard input (like in the case of a slower machine taking 10 seconds vs 1 second on my machine)? I am unsure whether sendInput has any associated methods that would provide this functionality and I have not been able to find any information yet on MSDN about this topic. Thanks, and please see attached code.
#include <Windows.h>
#include <exception>
const int ERROR_VALUE_RANGE = 32;
using namespace std;
//Method Declarations
void openProgram(char filePath[]);
void openUpdateMenu();
/**
* exception class for throwing ShellExecute Exceptions
* throws char* "Error opening program" if any issues arise
*/
class runException: public exception {
virtual const char* what() const throw() {
return "Error opening program";
}
} runEx;
int main (void) {
// Path to Mozilla Firefox
char filePath[] = "C:\\Program Files (x86)\\Mozilla Firefox\\firefox.exe";
// Start Firefox using openProgram Method
try {
openProgram(filePath);
}
catch (exception& e) {
//Insert code to open dialog box displaying error message;
}
Sleep(5000); //sleep for one second while we wait for Windows Update to open
openUpdateMenu();
return 0;
}
/**
* method openProgram opens a file using the ShellExecute method
* @param filePath - a char array of the complete filepath
*/
void openProgram(char filePath[]) { // throws (runEx)
HINSTANCE exReturn = ShellExecute(
HWND_DESKTOP, // Parent is desktop
"open", // Opens file (explorer double click behavior)
filePath, // Path to Windows Update program
NULL, // Parameters
NULL, // Default Directory
SW_SHOW); // Show the program once it opens
//if there are any issues opening the file the return value will be less than 32
if ((int)exReturn <= ERROR_VALUE_RANGE) {
throw runEx; //throws runException
}
return;
}
/**
* method openUpdateMenu sends key commands to Firefox to open the about menu with the update service
*/
void openUpdateMenu() {
//set up keyboard emulation
INPUT Input; //declare input object
Input.type = INPUT_KEYBOARD;
Input.ki.time = 0; //let system provide timestamp
Input.ki.dwExtraInfo = 0;
//simulate pressing alt down
Input.ki.wVk = VK_LMENU;
Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
SendInput(1, &Input, sizeof(INPUT));
//simulate pressing 'h' down
Input.ki.wVk = 0x48;
Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
SendInput(1, &Input, sizeof(INPUT));
//release alt key
Input.ki.wVk = VK_LMENU;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &Input, sizeof(INPUT));
//release 'h' key
Input.ki.wVk = 0x48;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &Input, sizeof(INPUT));
Sleep(500); // wait for menu to open
//simulate pressing 'a' down
Input.ki.wVk = 0x41 ;
Input.ki.dwFlags = KEYEVENTF_EXTENDEDKEY;
SendInput(1, &Input, sizeof(INPUT));
//release 'a' key
Input.ki.wVk = 0x41 ;
Input.ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(1, &Input, sizeof(INPUT));
//wait a second so user can see the program work
Sleep(1000);
}