Code:
// shoreTel.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "MHC-CM.h"
#include <iostream>
#include <shellapi.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <cstdlib>
#include <Commctrl.h>
#define MAX_LOADSTRING 100
#define TOGGABLE_KEYS 10002
#define IDT_TIMER WM_USER + 200
// Global Variables:
HINSTANCE hInst; // current instance
TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
TCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
//Needed for the FindWindowClassContains() & FindWindowTitleContains() Functions
struct result_stru
{
char* text;
UINT (__stdcall*GET)(HWND,LPSTR,UINT);
HWND hRet;
};
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
BOOL staticDlg(HINSTANCE , int );
BOOL WINAPI StruEnumProc( HWND , result_stru* );
HWND FindWindowTitleContains( char* );
HWND FindWindowClassContains( char* );
bool msgbox = true;
DWORD dwLastError = GetLastError();
VOID CenterWindow(HWND hwnd, HWND hwndParent, int Width, int Height)
{
/* Variables */
RECT rc;
/* If Parent Window Is Set As Null, Get The Desktop Window */
if(hwndParent == NULL)
{
hwndParent = GetDesktopWindow();
}
/* Get Parent Client Area Measurements */
GetClientRect(hwndParent, &rc);
/* Center The Window */
MoveWindow(hwnd, (rc.right - rc.left - Width) / 2, (rc.bottom - rc.top - Height) / 2, Width, Height, TRUE);
return;
}
/* These functions allow to find the correct Window & Class Name */
BOOL WINAPI StruEnumProc( HWND hwnd, result_stru* stru )
{
char loc_buf[128];
stru->GET( hwnd, loc_buf, 127 );
if( strstr( loc_buf, stru->text ) )
{
stru->hRet = hwnd;
return FALSE;
}
return TRUE;
}
//case sensitive!
HWND FindWindowTitleContains( char* text )
{
result_stru res = { text, (UINT (__stdcall *)(HWND,LPSTR,UINT))GetWindowTextA, 0 };
EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
return res.hRet;
}
//case sensitive!
HWND FindWindowClassContains( char* text )
{
result_stru res = { text, RealGetWindowClassA, 0 };
EnumWindows( (WNDENUMPROC)StruEnumProc, (LPARAM)&res );
return res.hRet;
}
void GetDesktopResolution(int& width, int& height)
{
RECT desktop;
// Get a handle to the desktop window
const HWND hDesktop = GetDesktopWindow();
// Get the size of screen to the variable desktop
GetWindowRect(hDesktop, &desktop);
// The top left corner will have coordinates (0,0)
// and the bottom right corner will have coordinates
// (horizontal, vertical)
width = desktop.right;
height = desktop.bottom;
}
bool isRunning()
{
//This checks if the process is already running so you don't have multiple instances running.
HANDLE handle = CreateMutex(NULL, true, "desktopOverlay");
if(GetLastError() != ERROR_SUCCESS)
{
MessageBox(0, "Process is already running", "Warning", MB_ICONWARNING);
return false;
}
else
return true;
}
char* MakeLower( char* strLower )
{
size_t size = strlen(strLower);
char *newstr = (char*)malloc(size);
for(int loop = 0; loop < strlen(newstr); loop++)
{
newstr[loop] = tolower(strLower[loop]);
}
return newstr;
}
bool CompareData( const unsigned char* pbData, const unsigned char* pbMask, const char* pszString )
{
for ( ; *pszString; ++pszString, ++pbData, ++pbMask )
{
if ( *pszString == 'x' && *pbData != *pbMask )
return FALSE;
}
return (*pszString) == NULL;
}
unsigned long FindPattern( HANDLE hProcess, unsigned long dwAddress, unsigned long dwLength, unsigned char* pbMask, char* pszString )
{
unsigned long dwResult = NULL;
unsigned char* pbBuffer = (unsigned char*) malloc( dwLength );
if ( pbBuffer )
{
if ( ReadProcessMemory( hProcess, (void*) dwAddress, pbBuffer, dwLength, NULL ) )
{
for ( unsigned long i = 0; i < dwLength; i++ )
{
if ( CompareData( (BYTE*) (pbBuffer + i), pbMask, pszString ) )
{
dwResult = (unsigned long)(dwAddress + i);
break;
}
}
}
free( pbBuffer );
pbBuffer = NULL;
}
return dwResult;
}
unsigned long GetProcessId( char* pszProcessName )
{
unsigned long dwResult = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot ( TH32CS_SNAPPROCESS, 0 );
if ( hSnapshot )
{
PROCESSENTRY32 processEntry = { sizeof( PROCESSENTRY32 ) };
if ( Process32First( hSnapshot, &processEntry ) )
{
do
{
if ( strcmp( processEntry.szExeFile, pszProcessName ) == 0 )
{
dwResult = processEntry.th32ProcessID;
break;
}
}
while ( Process32Next( hSnapshot, &processEntry ) );
}
if ( hSnapshot )
{
CloseHandle( hSnapshot );
hSnapshot = NULL;
}
}
return dwResult;
}
unsigned long GetModuleBase( unsigned long dwPID, char* pszModuleName, unsigned long* pdwSize )
{
unsigned long dwResult = 0;
HANDLE hSnapshot = CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, dwPID );
if ( hSnapshot )
{
MODULEENTRY32 moduleEntry = { sizeof( MODULEENTRY32 ) };
if ( Module32First( hSnapshot, &moduleEntry ) )
{
do
{
if ( strcmp( moduleEntry.szModule, pszModuleName ) == 0 )
{
dwResult = (unsigned long) moduleEntry.modBaseAddr;
if ( pdwSize )
*pdwSize = moduleEntry.modBaseSize;
break;
}
}
while ( Module32Next( hSnapshot, &moduleEntry ) );
}
if ( hSnapshot )
{
CloseHandle( hSnapshot );
hSnapshot = NULL;
}
}
return dwResult;
}
int CheckProcess( char* ProcessName )
{
int n_PId=0;
HANDLE n_NewSnapshot;
PROCESSENTRY32 procA;
n_NewSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,NULL);
if (n_NewSnapshot==INVALID_HANDLE_VALUE)
{
CloseHandle(n_NewSnapshot);
return 0;
}
procA.dwSize = sizeof(procA);
while (Process32Next(n_NewSnapshot,&procA))
{
if(_stricmp(MakeLower(procA.szExeFile), MakeLower(ProcessName)) == 0)
{
n_PId = procA.th32ProcessID;
}
}
CloseHandle(n_NewSnapshot);
return n_PId;
}
void SetEnterKey( BOOL bState )
{
BYTE keyState[256];
GetKeyboardState((LPBYTE)&keyState);
if((bState && !(keyState[VK_RETURN] & 1)) || (!bState && (keyState[VK_RETURN] & 1)))
{
// Simulate a key press
keybd_event(VK_RETURN, 0x45, KEYEVENTF_EXTENDEDKEY | 0, 0);
// Simulate a key release
keybd_event(VK_RETURN, 0x45, KEYEVENTF_EXTENDEDKEY | KEYEVENTF_KEYUP, 0);
}
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
int width = 0;
int height = 0;
LRESULT treeItem = 0;
//Check if this process is running
if(isRunning() == false)
return 0;
char szWindowClass[] = "MHC-CM";
MSG msg;
WNDCLASSEX wcex = {0};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.hCursor = LoadCursor( NULL, IDC_ARROW );
wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wcex.lpszClassName = szWindowClass;
wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
RegisterClassEx( &wcex );
GetDesktopResolution(width, height);
//Start our application "AD Pictures"
ShellExecute(NULL, "open", "c:\\Program Files\\AD Pictures\\AD Pictures.exe", NULL, NULL, SW_SHOWNORMAL);
//Main Application Window
unsigned long dwPID = GetProcessId( "AD Pictures.exe" );
unsigned long dwSize = 0;
unsigned long dwBase = GetModuleBase( dwPID, "AD Pictures.exe", &dwSize );
HANDLE Process = OpenProcess( PROCESS_VM_READ, false, dwPID );
HWND hWnd = CreateWindowEx(0, szWindowClass, "", WS_POPUP | WS_CAPTION | WS_VISIBLE, 100, 100, 1, 1, NULL, NULL, hInstance, NULL);
//Make sure that AD Pictures is running
if(!Process)
{
MessageBox(0, "AD Pictures is not running.", "Warning", MB_ICONWARNING);
return FALSE;
}
//Pause for 5 seconds to make sure the program is running
Sleep(5000);
//Get handle for Application
HWND DialogHandle = FindWindowEx(0, 0, "#32770", "AD Pictures by Exclaimer");
//Get handle for next button
HWND DialogButtonHandle = FindWindowEx(DialogHandle, 0, "Button", "&Next >");
//Get handle for Import Multiple Images radio button
HWND DialogRadioHandle = FindWindowEx(DialogHandle, 0, "Button", "Import Multiple Images");
//Get handle for treeview
HWND TreeView = FindWindowEx(DialogHandle, 0, "SysTreeView32", NULL);
Sleep(1000);
SendMessage(DialogButtonHandle, BM_CLICK, 1, 0);
Sleep(500);
SendMessage(DialogRadioHandle, BM_CLICK, 1, 0);
Sleep(500);
SendMessage(DialogRadioHandle, BM_SETCHECK, BST_CHECKED, 0);
Sleep(500);
SendMessage(DialogRadioHandle, BM_CLICK, 0, 0);
Sleep(500);
SendMessage(DialogRadioHandle, WM_ACTIVATE, WA_ACTIVE, 0);
Sleep(2000);
SendMessage(DialogButtonHandle, BM_CLICK, 1, 0);
Sleep(500);
treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_NEXT, treeItem);
treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_CHILD, treeItem);
SendMessage(TreeView, TVM_SELECTITEM, TVGN_CARET, treeItem);
//Do Stuff
//Application is running
//Main handle to AD Pictures
// HWND myTitle = FindWindowTitleContains("AD Pictures by Exclaimer");
// HWND myClass = FindWindowClassContains("#32770");
// HWND application = FindWindow("#32770", "AD Pictures by Exclaimer");
// HWND application2 = FindWindow("AD Pictures by Exclaimer", "#32770");
//Next button
// HWND nextButton = FindWindow("Button", "&Next >");
//Import Multiple Pictures
// HWND radio1 = FindWindow("Button", "Import Multiple Images");
//Handle for the SysTreeView32 Form
// HWND TreeView = FindWindowEx(myClass, NULL, "SysTreeView32", NULL);
/*
if(application || application2)
{
if(FindWindowEx(application, NULL, "SysTreeView32", NULL))
{
MessageBox(0, "SysTreeView32", "Found Window", MB_OK);
}
if(FindWindow("Button", "&Next >"))
{
MessageBox(0, "&Next", "Found Window", MB_OK);
}
if(FindWindow("&Next >", "Button"))
{
MessageBox(0, "&Next2", "Found Window", MB_OK);
}
if(FindWindow("Button", "Next"))
{
MessageBox(0, "Next", "Found Window", MB_OK);
}
if(FindWindow("Next", "Button"))
{
MessageBox(0, "Next2", "Found Window", MB_OK);
}
/*
MessageBox(0, "App Running", "Found Window", MB_OK);
if(nextButton)
{
MessageBox(0, "Test1", "Found Window", MB_OK);
SendMessage(nextButton, BM_CLICK, 0, 0);
}
else if(radio1)
{
MessageBox(0, "Test2", "Found Window", MB_OK);
SendMessage(radio1, BM_CLICK, 0, 0);
SendMessage(nextButton, BM_CLICK, 0, 0);
}
else if(TreeView)
{
MessageBox(0, "Test3", "Found Window", MB_OK);
treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_ROOT, 0);
treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_NEXT, treeItem);
treeItem = SendMessage(TreeView, TVM_GETNEXTITEM, TVGN_CHILD, treeItem);
SendMessage(TreeView, TVM_SELECTITEM, TVGN_CARET, treeItem);
}
}
*/
/*
//This is the OK button you click after cache maintenance
else if(hWnd5)
{
MessageBox(0, "Test7", "Warning", MB_OK);
if(Process)
{
MessageBox(0, "Test8", "Warning", MB_OK);
//keybd_event(VK_RETURN, NULL, NULL, NULL);
//keybd_event(VK_RETURN, NULL, KEYEVENTF_KEYUP, NULL);
SetEnterKey(TRUE);
CloseHandle(Process);
}
}
*/
//Activate our Main Window
CenterWindow(hWnd, NULL, 100, 100);
ShowWindow(hWnd, SW_HIDE);
UpdateWindow(hWnd);
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage( &msg );
DispatchMessage( &msg );
}
return (int) msg.wParam;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId = 0;
int wmEvent = 0;
switch (message)
{
case WM_CLOSE:
//PostQuitMessage(0);
break;
case WM_DESTROY:
//PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
The first Next button is automated correctly, however this piece: