I know the program reads the handle because it doesn't pop up an error when I run it.
This ain't necessarily true. You should verify that all handles are valid and check the return code from OpenProcess and ReadProcessMemory. Please note the "remote" app will display the hex address of the data in the window. This is the address to be used in your "utility".
Your utility to access the remote app:
Code:
// util.rc
#include <windows.h>
#define IDD_LISTBOX 100
#define IDC_PLAYERLIST 101
IDD_LISTBOX DIALOGEX 0, 0, 497, 171
STYLE DS_SETFONT | DS_MODALFRAME | DS_FIXEDSYS | WS_POPUP | WS_CAPTION |
WS_SYSMENU
CAPTION "Just a test list box"
FONT 8, "MS Shell Dlg", 400, 0, 0x1
BEGIN
DEFPUSHBUTTON "OK",IDOK,137,138,50,14
PUSHBUTTON "Cancel",IDCANCEL,220,140,50,14
LISTBOX IDC_PLAYERLIST,79,22,279,91,LBS_SORT | LBS_NOINTEGRALHEIGHT |
WS_VSCROLL | WS_TABSTOP
END
Code:
// util.cpp
#include <windows.h>
#include <commctrl.h>
#include <stdio.h>
#include <string.h>
#define IDD_LISTBOX 100
#define IDC_PLAYERLIST 101
BOOL CALLBACK DoListboxProcess(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
HWND hwnd;
DWORD pid;
HANDLE process;
int x;
// Hex address from "remote" app
DWORD iAddress = 0x408088;
DWORD dummy = 0;
char value[33], outvalue[22];
switch (message) {
case WM_INITDIALOG:
hwnd=FindWindow(NULL, "The Hello Program");
SendDlgItemMessage(hDlg, IDC_PLAYERLIST, LB_RESETCONTENT, 0, 0);
GetWindowThreadProcessId(hwnd, &pid);
process = OpenProcess(PROCESS_VM_READ
|PROCESS_VM_WRITE|
PROCESS_VM_OPERATION
|PROCESS_QUERY_INFORMATION,
FALSE, pid);
for(x = 0; x < 3;x++)
{
ReadProcessMemory(process // handle to the process
,(void*) iAddress, // address to start reading
(void*) &value, // address of buffer to place read data
sizeof(value) // number of bytes to read
,&dummy); // number of bytes read
strncpy(outvalue, value,22);
outvalue[21] = '\0';
SendDlgItemMessage(hDlg, IDC_PLAYERLIST, LB_ADDSTRING, 0, (LPARAM)outvalue);
iAddress += sizeof value;
}
return (TRUE);
case WM_COMMAND:
if (LOWORD(wParam) == IDOK ||LOWORD(wParam)== IDCANCEL ) {
EndDialog(hDlg, TRUE);
return (TRUE);
}
break;
}
return FALSE;
}
int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpszArgs,
int nWinMode)
{
InitCommonControls();
DialogBox(hInstance ,MAKEINTRESOURCE(IDD_LISTBOX) , 0, DoListboxProcess);
return 0;
}
The "remote" app:
Code:
// remote.cpp
#include <windows.h>
#include <stdio.h>
#include <string.h>
LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
static TCHAR szAppName[] = TEXT ("HelloWin") ;
HWND hwnd ;
MSG msg ;
WNDCLASS wndclass ;
wndclass.style = CS_HREDRAW | CS_VREDRAW ;
wndclass.lpfnWndProc = WndProc ;
wndclass.cbClsExtra = 0 ;
wndclass.cbWndExtra = 0 ;
wndclass.hInstance = hInstance ;
wndclass.hIcon = LoadIcon (NULL, IDI_APPLICATION) ;
wndclass.hCursor = LoadCursor (NULL, IDC_ARROW) ;
wndclass.hbrBackground = (HBRUSH) GetStockObject (WHITE_BRUSH) ;
wndclass.lpszMenuName = NULL ;
wndclass.lpszClassName = szAppName ;
if (!RegisterClass (&wndclass))
{
MessageBox (NULL, TEXT ("This program requires Windows NT!"),
szAppName, MB_ICONERROR) ;
return 0 ;
}
hwnd = CreateWindow (szAppName, // window class name
TEXT ("The Hello Program"), // window caption
WS_OVERLAPPEDWINDOW, // window style
CW_USEDEFAULT, // initial x position
CW_USEDEFAULT, // initial y position
CW_USEDEFAULT, // initial x size
CW_USEDEFAULT, // initial y size
NULL, // parent window handle
NULL, // window menu handle
hInstance, // program instance handle
NULL) ; // creation parameters
ShowWindow (hwnd, iCmdShow) ;
UpdateWindow (hwnd) ;
while (GetMessage (&msg, NULL, 0, 0))
{
TranslateMessage (&msg) ;
DispatchMessage (&msg) ;
}
return msg.wParam ;
}
LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
// This is the array we are reading remotely
static char array[3][33]= {"Ted GoodPlayer","Bobby BadPlayer " , "Joey OKPlayer "};
HDC hdc ;
PAINTSTRUCT ps ;
RECT rect ;
char szMessage[128] = {0};
switch (message)
{
case WM_CREATE:
return 0 ;
case WM_PAINT:
hdc = BeginPaint (hwnd, &ps) ;
GetClientRect (hwnd, &rect) ;
sprintf(szMessage,"Address of array is HEX %02x", array);
DrawText (hdc, TEXT (szMessage), -1, &rect,
DT_SINGLELINE | DT_CENTER | DT_VCENTER) ;
EndPaint (hwnd, &ps) ;
return 0 ;
case WM_DESTROY:
PostQuitMessage (0) ;
return 0 ;
}
return DefWindowProc (hwnd, message, wParam, lParam) ;
}