-
Member
Getting 299 error when trying to read data
I am trying to do some basic memory reads and got hit by 299 error. Not sure in which direction i should dig. Any suggestions?
Code:
using namespace std;
#include <iostream>
#include <Windows.h>
#include <stdio.h>
#include <Psapi.h>
#include <string.h>
HMODULE getModule(HANDLE processHandle) {
HMODULE hMods[1024];
DWORD cbNeeded;
unsigned int i;
cout << "Finding module for " << processHandle << endl;
if (!EnumProcessModules(processHandle, hMods, sizeof(hMods), &cbNeeded)) {
cout << "Failed to list process modules: " << GetLastError() << endl;
return nullptr;
}
for (i = 0; i < (cbNeeded / sizeof(HMODULE)); i++) {
TCHAR szModName[MAX_PATH];
if (GetModuleFileNameEx(processHandle, hMods[i], szModName, sizeof(szModName) / sizeof(TCHAR))) {
wstring wstrModName = szModName;
wstring wstrModContain = L"WowClassic.exe";
if (wstrModName.find(wstrModContain) != string::npos) {
return hMods[i];
}
}
}
return nullptr;
}
int main() {
cout << "Trying to hook in World of Warcraft!" << endl;
HWND hwnd = FindWindow(NULL, L"World of Warcraft");
if (hwnd == 0) {
cout << "Window was not found" << endl;
return 1;
}
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
if (processId == 0) {
cout << "Failed to get process id" << endl;
return 1;
}
cout << "World of Wacraft was found with process id " << processId << endl;
HANDLE processHandle = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, false, processId);
if (processHandle == 0) {
cout << "Failed to open process" << endl;
return 1;
}
cout << "Open process with handle id " << processHandle << endl;
HMODULE module = getModule(processHandle);
if (module == nullptr) {
cout << "Failed to find module" << endl;
CloseHandle(processHandle);
return 1;
}
DWORD baseAddress = (DWORD) module;
DWORD offset = 0x258ACF8; // 1.13.3 .33526 InGameFlag
DWORD address = baseAddress + offset ;
cout << "Base address are " << uppercase << hex << baseAddress << endl;
cout << "Offset address " << baseAddress << "+" << offset << " (" << address << ")" << dec << endl;
uint32_t value;
if (!ReadProcessMemory(processHandle, (LPVOID) address, &value, sizeof(value), NULL)) {
cout << "Failed to read process memory: " << GetLastError() << endl;
CloseHandle(processHandle);
return 1;
}
cout << "Value " << value << endl;
CloseHandle(processHandle);
return 0;
}
-
Contributor
Try this, also make sure to run as admin and compile as x64
Code:
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>
uintptr_t GetModuleBaseAddress(const DWORD procId, const wchar_t* modName)
{
uintptr_t modBaseAddr = 0;
const auto hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, procId);
if (hSnap != INVALID_HANDLE_VALUE)
{
MODULEENTRY32 modEntry;
modEntry.dwSize = sizeof(modEntry);
if (Module32First(hSnap, &modEntry))
{
do
{
if (!_wcsicmp(modEntry.szModule, modName))
{
modBaseAddr = reinterpret_cast<uintptr_t>(modEntry.modBaseAddr);
break;
}
} while (Module32Next(hSnap, &modEntry));
}
}
CloseHandle(hSnap);
return modBaseAddr;
}
template<typename T>
T ReadMemory(HANDLE hProcess, const uintptr_t address) {
T buffer;
return ReadProcessMemory(hProcess, reinterpret_cast<BYTE*>(address), &buffer, sizeof T, nullptr) ? buffer : 0;
}
template<typename T>
bool WriteMemory(HANDLE hProcess, const uintptr_t address, T buffer) {
return WriteProcessMemory(hProcess, reinterpret_cast<BYTE*>(address), &buffer, sizeof T, nullptr);
}
int main()
{
std::cout << "Trying to find World of Warcraft!\n";
const auto hwnd = FindWindow(nullptr, L"World of Warcraft");
DWORD processId;
GetWindowThreadProcessId(hwnd, &processId);
if (hwnd && processId)
{
std::cout << "World of Wacraft was found with process id " << processId << "\n";
const auto base = GetModuleBaseAddress(processId, L"WowClassic.exe");
const uintptr_t inGame = 0x258ACF8;
std::cout << "Base address is 0x" << std::hex << base << "\n";
const auto processHandle = OpenProcess(PROCESS_ALL_ACCESS, false, processId);
std::cout << "InGame offset value is " << static_cast<int32_t>(ReadMemory<uint8_t>(processHandle, base + inGame)) << "\n";
CloseHandle(processHandle);
std::cin.get();
return 0;
}
std::cout << "Window was not found\n";
return 1;
}
also note that the ingame offset is a byte not an int so we read as a byte then cast it to an int just to print it
-
Post Thanks / Like - 1 Thanks
ensirius (1 members gave Thanks to Icesythe7 for this useful post)
-
Member