Code:
// shoreTel.cpp : Defines the entry point for the application.
//
#include "stdafx.h"
#include "shoreTel.h"
#include <shellapi.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <cstdlib>
#define MAX_LOADSTRING 100
#define TOGGABLE_KEYS 10002
#define IDT_TIMER WM_USER + 200
// Forward declarations of functions included in this code module:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
//This renders the vidual content
HWND myDialog = CreateWindowEx(0,WC_DIALOG,"", WS_VISIBLE|WS_OVERLAPPEDWINDOW, 0,0,1,1,NULL,NULL,NULL,NULL);
//Cache Maintenance Window
HWND hWnd1 = FindWindow("TfrmConfirmCacheMaintenance","Confirmation");
//Purge Maintenance Window
HWND hWnd2 = FindWindow("TfrmConfirmPurgeProcess","Confirmation");
DWORD dwLastError = GetLastError();
char* MakeLower( char* strLower )
{
int size = strlen(strLower);
char *newstr = (char*)malloc(size);
for(int loop = 0; loop < strlen(newstr); loop++)
{
newstr[loop] = tolower(strLower[loop]);
}
return newstr;
}
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(strcmp(MakeLower(procA.szExeFile), MakeLower(ProcessName)) == 0)
{
n_PId = procA.th32ProcessID;
}
}
CloseHandle(n_NewSnapshot);
return n_PId;
}
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
//This checks if the process is already running so you don't have multiple instances running.
HANDLE handle = CreateMutex(NULL, true, "MHC-CM");
if(GetLastError() != ERROR_SUCCESS)
{
// MessageBox(0, "Process is already running", "Warning", MB_ICONWARNING);
return false;
}
ShowWindow(myDialog, SW_HIDE);
// TODO: Place code here.
MSG msg;
HACCEL hAccelTable;
// Initialize global strings
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_SHORETEL);
// Main message loop:
while (GetMessage(&msg, NULL, 0, 0))
{
DWORD ProcessID1;
GetWindowThreadProcessId( hWnd1, &ProcessID1 );
HANDLE Process1 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID1 );
DWORD ProcessID2;
GetWindowThreadProcessId( hWnd2, &ProcessID2 );
HANDLE Process2 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID2 );
if(hWnd1)
{
MessageBox(0, "window1 is already running", "Warning", MB_ICONWARNING);
if(Process1)
{
MessageBox(0, "Process1 is already running", "Warning", MB_ICONWARNING);
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process1);
}
}
else if(hWnd2)
{
MessageBox(0, "window2 is already running", "Warning", MB_ICONWARNING);
if(Process2)
{
MessageBox(0, "Process2 is already running", "Warning", MB_ICONWARNING);
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process2);
}
}
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_SHORETEL);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = (LPCTSTR)IDC_SHORETEL;
// wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
LRESULT WINAPI myProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId;
int wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_INITDIALOG:
//** Set timers
SetTimer(hwnd, TOGGABLE_KEYS, 1, NULL);
break;
case WM_PAINT:
{
hdc = BeginPaint(myDialog, &ps);
if(hWnd1)
{
//Cache Maintenance Window
DWORD ProcessID1;
GetWindowThreadProcessId( hWnd1, &ProcessID1 );
HANDLE Process1 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID1 );
if(Process1)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process1);
}
}
else if(hWnd2)
{
//Cache Maintenance Window
DWORD ProcessID2;
GetWindowThreadProcessId( hWnd2, &ProcessID2 );
HANDLE Process2 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID2 );
if(Process2)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process2);
}
}
EndPaint(myDialog, &ps);
}
break;
case WM_COMMAND:
{
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(myDialog);
break;
default:
return DefWindowProc(myDialog, message, wParam, lParam);
}
}
break;
case WM_TIMER:
/*
if(hWnd1 || hWnd2)
{
Sleep(100);
DWORD ProcessID;
GetWindowThreadProcessId( hWnd1, &ProcessID );
HANDLE Process = OpenProcess( PROCESS_VM_READ|PROCESS_VM_OPERATION, FALSE, ProcessID );
DWORD ProcessID2;
GetWindowThreadProcessId( hWnd2, &ProcessID2 );
HANDLE Process2 = OpenProcess( PROCESS_VM_READ|PROCESS_VM_OPERATION, FALSE, ProcessID2 );
if((hWnd1 || hWnd2) && (ProcessID || ProcessID2))
{
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
}
CloseHandle(Process);
CloseHandle(Process2);
}
*/
if(hWnd1)
{
//Cache Maintenance Window
DWORD ProcessID1;
GetWindowThreadProcessId( hWnd1, &ProcessID1 );
HANDLE Process1 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID1 );
if(Process1)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process1);
}
}
else if(hWnd2)
{
//Cache Maintenance Window
DWORD ProcessID2;
GetWindowThreadProcessId( hWnd2, &ProcessID2 );
HANDLE Process2 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID2 );
if(Process2)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process2);
}
}
break;
/*
NOTE: If you comment the below, this will allow the user to move or close out the window (may not be a good idea)
This way the code below stops all user interaction unless they press the "Close Dialog" button.
*/
default:
return DefWindowProc(myDialog, message, wParam, lParam);
}
return 0;
}
BOOL staticDlg(HINSTANCE hInstance, int nCmdShow)
{
DWORD ProcessID1;
GetWindowThreadProcessId( hWnd1, &ProcessID1 );
HANDLE Process1 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID1 );
DWORD ProcessID2;
GetWindowThreadProcessId( hWnd2, &ProcessID2 );
HANDLE Process2 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID2 );
if(hWnd1)
{
if(Process1)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process1);
}
}
else if(hWnd2)
{
if(Process2)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process2);
}
}
ShowWindow(myDialog, SW_HIDE);
SetWindowPos( myDialog, HWND_TOPMOST, 0, 0 , 0 , 0, SWP_NOMOVE | SWP_NOSIZE );
return TRUE;
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
DWORD ProcessID1;
GetWindowThreadProcessId( hWnd1, &ProcessID1 );
HANDLE Process1 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID1 );
DWORD ProcessID2;
GetWindowThreadProcessId( hWnd2, &ProcessID2 );
HANDLE Process2 = OpenProcess( PROCESS_ALL_ACCESS, FALSE, ProcessID2 );
if(hWnd1)
{
if(Process1)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process1);
}
}
else if(hWnd2)
{
if(Process2)
{
Sleep(50);
//Emulated keypresses
keybd_event(VK_RIGHT, NULL, NULL, NULL);
keybd_event(VK_RIGHT, NULL, KEYEVENTF_KEYUP, NULL);
CloseHandle(Process2);
}
}
if (!myDialog)
{
return FALSE;
}
UpdateWindow(myDialog);
SetTimer(myDialog, 1, 1, NULL);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId;
int wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_CLOSE:
PostQuitMessage(0);
break;
case WM_COMMAND:
wmId = LOWORD(wParam);
wmEvent = HIWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
break;
case WM_PAINT:
hdc = BeginPaint(myDialog, &ps);
// TODO: Add any drawing code here...
EndPaint(myDialog, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}