ReadProcessMemory CheatEngine Value - c++

I want to get a value from a Proccess, here it's a simple test with "Calculator".
First, I get the address with CheatEngine. Secondly I put it in ReadProcessMemory.
But ReadProcessMemory return 0, I think I miss something, I've found something with BaseAddress, but I still have bad results. Google is out of results for me, so I ask you!
#include <windows.h>
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int value;
DWORD pid;
HWND hwnd = FindWindow(NULL,"Calculatrice");
if(!hwnd)
{
cout << "Window not found!";
}
else
{
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_ALL_ACCESS,0,pid);
if(!phandle)
{
cout <<"Could not get handle!";
}
else
{
cout << ReadProcessMemory(phandle,(LPVOID)0xC71657E900,&value,sizeof(value),0) << endl;
cout << value;
getch();
return 0;
}
}
}

Resolved ! When the address is too big, because it's a 64bit address, the program change to a 32bit address, that's why it don't work.
So, in Visual Studio, set to x64.

Related

How to read a memory address' value from an exe?

So I am trying to output how many rings I have when I run SonicAdventureDXPC.exe via reading a memory address. I got the address where it stores the ring count but when I run the code it would always output -1082988699 (even if the ring count was 1 or higher). This is the code I have:
#include <windows.h>
#include <iostream>
DWORD ADDR = 0x370F0E4;
DWORD PID;
int main(int argc, char* argv[]) {
HWND Window = FindWindow(0, "SonicAdventureDXPC");
if (Window == 0) {
std::cout << "Can't find the window"<< std::endl;
}
else {
GetWindowThreadProcessId(Window, &PID);
HANDLE Process = OpenProcess(PROCESS_ALL_ACCESS, FALSE, PID);
if (Process != 0) {
int Read_V;
ReadProcessMemory(Process, (LPVOID)ADDR, &Read_V, sizeof(Read_V), 0);
std::cout << "Rings: " << Read_V << std::endl;
system("PAUSE");
}
}
return 0;
}
Edit: The code is actually correct but me being me made a very dumb tiny mistake. While troubleshooting with Cheat Engine, I found out that I made a typo in the address variable. It was suppose to be 0x03B0F0E4, not 0x03F0F0E4.

Loading DLL file in C++

I want to create a C++ program that loads a dll(a.dll). It should run in
combination with wine in Linux. In the dll a function foo will be called
which takes two strings and an integer. foo returns an integer. The dll
is located in the same directory as the exe. Unfortunately the dll file
is not found and I don't know why. I have also tried "./a.dll" under
Linux. Can anyone give me some advice on this?
#include <iostream>
#include <string>
#include "windows.h"
#include <tchar.h>
typedef int(*func_ptr)(std::string, std::string, int);
int main()
{
func_ptr function1 = NULL;
HMODULE hGetProcIDDLL = LoadLibrary(L"a.dll");
if (hGetProcIDDLL != NULL)
{
std::cout << "Found!";
}
else
{
std::cout << "File not found!";
return 0;
}
function1 = (func_ptr)GetProcAddress(hGetProcIDDLL, "foo");
if (function1 != NULL)
{
std::cout << function1("input-file.txt","output-file.txt",0);
}
else
{
std::cout << "Function not found";
}
FreeLibrary(hGetProcIDDLL);
std::cin.get();
return(0);
}

console system() c++ infinite loop while trying to do ipconfig

I'm currently learning c++ from scratch, I've previously developed apps with C# using Visual Studio but I'm a total noob with C++, I'm trying to make a small console exe that releases and renews the ip while practicing using headers and differents .cpp files.
The issue is that when I run the local windows debugger from visual studio 2015 the code runs perfectly and does everything I'm trying to. But when I build and try to run the .exe file it goes into an infinite loop stating endlessly the output from the std::cout <<"Realizando ipconfig Release", I have localized the issue to when it tries to run the system("ipconfig /release"). Why does this happen? and How can I fix it?
This is the header
#pragma once
#ifndef HeaderIp
#define HeaderIp
int Release();
int Renew();
#endif // !HeaderIp
This is the release.cpp
#include <stdlib.h>
int Release()
{
if (system(nullptr)==0)
{
return 0;
}
else
{
system("ipconfig /release");
return 1;
}
}
This is the renew.cpp
#include <stdlib.h>;
int Renew()
{
if (system(nullptr)==0)
{
return 0;
}
else
{
system("ipconfig /renew");
return 1;
}
}
and finally this is the ipconfig.cpp
#include <iostream>
#include <stdlib.h>
#include "HeaderIp.h"
#include <stdio.h>
int main()
{
std::cout << "Realizando ipconfig Release" << std::endl;
int i = 0; //Bit de informacion de status de CPU
i = Release();
if (i == 0)
{
std::cout << "Error al liberar IP" << std::endl;
system("pause");
exit;
}
else
{
std::cout << "Ip Liberado correctamente" << std::endl;
}
i = Renew();
if (i == 0)
{
std::cout << "Error al renovar IP" << std::endl;
system("pause");
exit;
}
else
{
std::cout << "Ip renovado correctamente" << std::endl;
}
system("pause");
return 0;
}
It's unusual, and indeed generally discouraged to use system in C++. If you were to manage DHCP leases using IpReleaseAddress and IpRenewAddress, instead, you might find your problem disappears.
You can use std::cin.sync() instead of system("pause"), too.
exit; probably doesn't do what you want it to, as you're only referring to the function, you're not calling it, so... it'll do nothing.
The semicolon in #include <stdlib.h>; is an error, and you should be including <cstdlib> and <cstdio> rather than <stdlib.h> and <stdio.h>.

C++: Can not read the value inside a spesific memory address of my program using a different program

I'm currently learning c++ and i decided that i want to give it a shot and try to write a simple program that can read the values of other programs that are stored in memory (and later on be able to modify these values).
To test it i wrote a program that does a simple addition, i then run my other program and I'm trying to read in real time the value of the first program using its address in the memory.
The code will help you to understand better what i mean.
The "target" program that i want to read the value from:
#include <iostream>
#include <windows.h>
#include <conio.h>
using namespace std;
int main()
{
int userValue=0;
int total=0;
int* t;
SetWindowText(NULL, "Memory");
while(1)
{
cin >> userValue;
total += userValue;
t = &total;
cout << *t << endl;
cout <<"Pointer: " << &t <<" Total: " << &total;
}
getch();
return 0;
}
And the program that reads the value from the first one:
#include <windows.h>
#include <iostream>
using namespace std;
int main()
{
DWORD address = 0x28FEF0;
int value = 0;
DWORD pid;
HWND hwnd;
hwnd = FindWindow(NULL,"Memory");
if(!hwnd)
{
cout <<"Window not found!\n";
cin.get();
}
else
{
GetWindowThreadProcessId(hwnd,&pid);
HANDLE phandle = OpenProcess(PROCESS_VM_READ,0,pid);
if(!phandle)
{
cout <<"Could not get handle!\n";
cin.get();
}
else
{
while(1)
{
ReadProcessMemory(phandle,(void*)address,&value,sizeof(value),0);
cout << value << "\n";
Sleep(1000);
}
return 0;
}
}
}
The code from the second one was taken from a forum (after googling) and it works fine if i use it with any other program, but it doesn't want to work at all with my program.
I tried changing the address to both the pointer t and the actual integer total, but nothing worked.
I feel like I've done something wrong in the first program rather than the second one.
I'm using Code:Blocks with GCC compiler.
Thanks in advance for any help!
Oh, and Merry Christmas!
&t = the address of the pointer that points to total
&total = the address of the actual "total value"
In your first program you should output "&total"
In your second program you should use ReadProcessMemory to read the address which is output from the first program.
If it doesn't work, you must run the second program as administrator to get permissions. If you still have trouble, call GetLastError() after each Windows API call, check the return value of ReadProcessMemory() and utilize the last argument of ReadProcessMemory() for error checking.

Obtaining the windows desktop path

This is my code:
#include <Windows.h>
#include <ShlObj.h>
#include <iostream>
using namespace std;
int main()
{
LPTSTR myPath = NULL;
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);
if(myPath != NULL)
cout << "It returns something" << endl;
else
cout << "It returns nothing" << endl;
system("PAUSE");
return 0;
}
But myPath returns nothing. I just want to obtain the Desktop path. I'm on Windows 7 64 bits.
You need to give it room to put the data into:
T_CHAR myPath[ MAX_PATH ];
SHGetSpecialFolderPath(0, myPath, CSIDL_COMMON_DESKTOPDIRECTORY, FALSE);