Code:
#include "stdafx.h"
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
#include <commctrl.h>
#include <iostream>
#include <tlhelp32.h>
#define process L"AD Pictures.exe"
#define procWindow L"AD Pictures by Exclaimer"
#define sysTreeView L"Tree View"
#define editButton L"Edit..."
#define nextButton L"&Next >"
#define multImages L"Import Multiple Images"
#define MAXTEXTLEN 256
using namespace std;
WCHAR winname1[MAX_PATH];
WCHAR winname2[MAX_PATH];
DWORD GetProcessId(const std::wstring& processName)
{
PROCESSENTRY32 processInfo;
processInfo.dwSize = sizeof(processInfo);
HANDLE processesSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if(processesSnapshot == INVALID_HANDLE_VALUE)
return 0;
Process32First(processesSnapshot, &processInfo);
if(!processName.compare(processInfo.szExeFile))
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
while(Process32Next(processesSnapshot, &processInfo))
{
if(!processName.compare(processInfo.szExeFile))
{
CloseHandle(processesSnapshot);
return processInfo.th32ProcessID;
}
}
CloseHandle(processesSnapshot);
return 0;
}
HTREEITEM GetItemByName(HWND hWnd, HTREEITEM hItem, LPCWSTR szItemName)
{
unsigned long dwPID1 = GetProcessId( process );
GetWindowThreadProcessId(hWnd, &dwPID1);
if(dwPID1)
{
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, false, dwPID1);
if(hProcess)
{
DWORD remoteBuffer = (DWORD)::VirtualAllocEx(hProcess, NULL, 335, MEM_COMMIT, PAGE_READWRITE);
if(remoteBuffer)
{
HTREEITEM Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_ROOT, 0);
while(Root)
{
TVITEM tvItem;
tvItem.mask = TVIF_TEXT | TVIF_CHILDREN | TVIF_HANDLE;
tvItem.hItem = Root;
tvItem.pszText = (LPWSTR)remoteBuffer + sizeof(TVITEM);
tvItem.cchTextMax = 255;
//SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)Root);
if(WriteProcessMemory(hProcess, (LPVOID)remoteBuffer, (LPCVOID)&tvItem, sizeof(TVITEM), 0))
{
SendMessage(hWnd, TVM_GETITEM, 0, remoteBuffer);
wchar_t bytRtn[255];
int rAdd = remoteBuffer + sizeof(TVITEM)*2;
if(ReadProcessMemory(hProcess, (LPCVOID)rAdd, (LPVOID)&bytRtn, 255, 0))
{
//wcout << bytRtn << endl;
if(wcscmp(bytRtn, szItemName) == 0)
{
SendMessage(hWnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)Root);
Sleep(500);
return Root;
}
}
Root = (HTREEITEM)::SendMessage(hWnd, TVM_GETNEXTITEM, TVGN_NEXTVISIBLE, (LPARAM)Root);
}
}
VirtualFreeEx(hProcess, (LPVOID)remoteBuffer, 335, MEM_RELEASE);
}
CloseHandle(hProcess);
}
}
return 0;
}
BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
HWND ret = GetAncestor(hwnd, GA_ROOTOWNER);
//This gets the Window Name
SendMessage(ret, WM_GETTEXT, sizeof(winname2), (LPARAM)winname2);
SendMessage(hwnd, WM_GETTEXT, sizeof(winname1), (LPARAM)winname1);
//Checks to see if we can view the tree view
if(wcscmp(winname2, procWindow) == 0)
{
//This gets the Window Name
SendMessage(hwnd, WM_GETTEXT, sizeof(winname1), (LPARAM)winname1);
if(wcscmp(winname1, sysTreeView) == 0)
{
HTREEITEM hItem = GetItemByName(hwnd, NULL, L"Computer");
if(hItem != NULL)
{
SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem);
HTREEITEM hItem2 = GetItemByName(hwnd, NULL, L"Corp (H:)");
if(hItem2 != NULL)
{
SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem2);
HTREEITEM hItem3 = GetItemByName(hwnd, NULL, L"Pics");
if(hItem3 != NULL)
{
SendMessage(hwnd, TVM_SELECTITEM, TVGN_CARET, (LPARAM)hItem3);
}
}
}
//return FALSE; // end enumeration
}
}
//Checks to see if we can view the edit button
if(wcscmp(winname1, editButton) == 0)
{
if(IsWindowVisible(hwnd))
{
//Get Owner Window
unsigned long dwPID1 = GetProcessId( process );
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwPID1);
if(hProcess != NULL)
{
TerminateProcess(hProcess, 0);
CloseHandle(hProcess);
}
//return FALSE; // end enumeration
}
}
//Checks to see if we can view the next button
//We don't want the next button triggered if the systreeview is on
if((wcscmp(winname1, nextButton) == 0) && (wcscmp(winname1, sysTreeView) != 0))
{
SendMessage(hwnd, BM_CLICK, 1, 0);
Sleep(500);
//return FALSE; // end enumeration
}
//Checks to see if we can view the next button
if((wcscmp(winname1, nextButton) == 0) && (wcscmp(winname1, sysTreeView) == 0))
{
SendMessage(hwnd, BM_CLICK, 1, 0);
Sleep(500);
//return FALSE; // end enumeration
}
//Checks to see if we can view the mult images radio button
if(wcscmp(winname1, multImages) == 0)
{
SendMessage(hwnd, BM_CLICK, 1, 0);
Sleep(500);
//return FALSE; // end enumeration
}
// must return TRUE; If return is FALSE it stops the recursion
return TRUE;
Sleep(1000);
}
BOOL CALLBACK WindowHandle(HWND hwnd, LPARAM lParam)
{
char String[255];
DWORD dwID;
GetWindowThreadProcessId(hwnd, &dwID) ;
SendMessage(hwnd, WM_GETTEXT, sizeof(String), (LPARAM)String);
EnumChildWindows(hwnd, &EnumChildProc, NULL);
return TRUE ;
}
int _tmain(int argc, _TCHAR* argv[])
{
//Hide Console Window
//FreeConsole();
std::wstring processName = process;
//Start our Application
ShellExecute(NULL, L"open", L"C:\\Program Files (x86)\\AD Pictures\\AD Pictures.exe", NULL, NULL, SW_SHOWNORMAL);
//Wait 3 seconds to make sure the application is launched
Sleep(3000);
//Get Parent Handle of Application
unsigned long dwPID1 = GetProcessId( processName );
HANDLE hProcess = OpenProcess(PROCESS_VM_READ|PROCESS_VM_OPERATION, FALSE, dwPID1);
if(!hProcess)
{
wcout << "Could not find " << processName.c_str() << endl;
return 0;
}
while(hProcess)
{
EnumWindows((WNDENUMPROC)WindowHandle, (LPARAM)dwPID1);
}
CloseHandle(hProcess);
return 0;
}
Many thanks to everyone who helped me out!