zoukankan      html  css  js  c++  java
  • C++内存读写例子

    /*************************************************************
    //		声明头文件
    #ifndef LyShark__h
    #define LyShark__h
    
    #pragma comment(lib,"LyShark.lib");
    
    extern "C"__declspec(dllexport) int GetProcessID(char *Name);
    
    extern "C"__declspec(dllexport) int GetIntMemory_One(int Pid,long Base,int One);
    extern "C"__declspec(dllexport) int GetIntMemory_Two(int Pid,long Base,int One,int Two);
    extern "C"__declspec(dllexport) int GetIntMemory_Three(int Pid,long Base,int One,int Two,int Three);
    extern "C"__declspec(dllexport) int GetIntMemory_Four(int Pid,long Base,int One,int Two,int Three,int Four);
    extern "C"__declspec(dllexport) int GetIntMemory_Five(int Pid,long Base,int One,int Two,int Three,int Four,int Five);
    extern "C"__declspec(dllexport) int GetIntMemory_Six(int Pid,long Base,int One,int Two,int Three,int Four,int Five,int Six);
    
    
    extern "C"__declspec(dllexport) int SetIntMemory_One(int Pid,long Base,int One,int WriteValue);
    extern "C"__declspec(dllexport) int SetIntMemory_Two(int Pid,long Base,int One,int Two,int WriteValue);
    extern "C"__declspec(dllexport) int SetIntMemory_Three(int Pid,long Base,int One,int Two,int Three,int WriteValue);
    extern "C"__declspec(dllexport) int SetIntMemory_Four(int Pid,long Base,int One,int Two,int Three,int Four,int WriteValue);
    extern "C"__declspec(dllexport) int SetIntMemory_Five(int Pid,long Base,int One,int Two,int Three,int Four,int Five,int WriteValue);
    extern "C"__declspec(dllexport) int SetIntMemory_Six(int Pid,long Base,int One,int Two,int Three,int Four,int Five,int Six,int WriteValue);
    
    #endif LyShark__h
    
    */
    //*************************************************************
    #include <windows.h>
    
    bool APIENTRY DllMain(HANDLE handle,DWORD dword,LPVOID lpvoid)
    {
    	return true;
    }
    
    //*************************************************************
    //		打开进程,取进程PID
    extern "C"__declspec(dllexport) int GetProcessID(char *Name)
    {
    	HWND Pid=::FindWindow(NULL,Name);
    	DWORD Retn;
    	::GetWindowThreadProcessId(Pid,&Retn);
    	return Retn;
    }
    
    //*************************************************************
    //		读取内存偏移整数型
    extern "C"__declspec(dllexport) int GetIntMemory_One(int Pid,long Base,int One)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	return Value;	
    }
    
    extern "C"__declspec(dllexport) int GetIntMemory_Two(int Pid,long Base,int One,int Two)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	return Value;	
    }
    
    extern "C"__declspec(dllexport) int GetIntMemory_Three(int Pid,long Base,int One,int Two,int Three)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	return Value;	
    	
    }
    
    extern "C"__declspec(dllexport) int GetIntMemory_Four(int Pid,long Base,int One,int Two,int Three,int Four)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Four);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	return Value;
    
    }
    
    extern "C"__declspec(dllexport) int GetIntMemory_Five(int Pid,long Base,int One,int Two,int Three,int Four,int Five)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Five);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Four);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	return Value;
    	
    }
    
    extern "C"__declspec(dllexport) int GetIntMemory_Six(int Pid,long Base,int One,int Two,int Three,int Four,int Five,int Six)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Six);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Five);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Four);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	return Value;
    	
    }
    
    //*************************************************************
    //		写入内存偏移整数型
    
    extern "C"__declspec(dllexport) int SetIntMemory_One(int Pid,long Base,int One,int WriteValue)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    
    	DWORD bywrite;
    	LPVOID wbuffer = (LPVOID)&WriteValue;
    	WriteProcessMemory(proce,(LPVOID)pbase,wbuffer,4,&bywrite);
    
    	return 1;
    	
    }
    
    extern "C"__declspec(dllexport) int SetIntMemory_Two(int Pid,long Base,int One,int Two,int WriteValue)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    
    	DWORD bywrite;
    	LPVOID wbuffer = (LPVOID)&WriteValue;
    	WriteProcessMemory(proce,(LPVOID)pbase,wbuffer,4,&bywrite);
    
    	return 1;
    	
    }
    extern "C"__declspec(dllexport) int SetIntMemory_Three(int Pid,long Base,int One,int Two,int Three,int WriteValue)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	DWORD bywrite;
    	LPVOID wbuffer = (LPVOID)&WriteValue;
    	WriteProcessMemory(proce,(LPVOID)pbase,wbuffer,4,&bywrite);
    
    	return 1;
    }
    
    extern "C"__declspec(dllexport) int SetIntMemory_Four(int Pid,long Base,int One,int Two,int Three,int Four,int WriteValue)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Four);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    	DWORD bywrite;
    	LPVOID wbuffer = (LPVOID)&WriteValue;
    	WriteProcessMemory(proce,(LPVOID)pbase,wbuffer,4,&bywrite);
    
    	return 1;
    }
    
    extern "C"__declspec(dllexport) int SetIntMemory_Five(int Pid,long Base,int One,int Two,int Three,int Four,int Five,int WriteValue)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Five);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Four);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    
    	DWORD bywrite;
    	LPVOID wbuffer = (LPVOID)&WriteValue;
    	WriteProcessMemory(proce,(LPVOID)pbase,wbuffer,4,&bywrite);
    
    	return 1;
    	
    }
    
    extern "C"__declspec(dllexport) int SetIntMemory_Six(int Pid,long Base,int One,int Two,int Three,int Four,int Five,int Six,int WriteValue)
    {
    	DWORD byread;
    	int Value=0;
    	HANDLE proce=::OpenProcess(PROCESS_ALL_ACCESS,false,Pid);
    	LPCVOID pbase=(LPCVOID)Base;
    	LPVOID rbuffer=(LPVOID)&Value;
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Six);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Five);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Four);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Three);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + Two);
    
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    	pbase = (LPCVOID)(Value + One);
    	
    	::ReadProcessMemory(proce,pbase,rbuffer,4,&byread);
    
    
    	DWORD bywrite;
    	LPVOID wbuffer = (LPVOID)&WriteValue;
    	WriteProcessMemory(proce,(LPVOID)pbase,wbuffer,4,&bywrite);
    
    	return 1;
    	
    }
    
    
    
    
  • 相关阅读:
    C# 复制文件夹,移动文件夹
    让Base64适合在URL中使用
    修复FIREBIRD数据库
    Image.FromFile 锁文件的解决办法
    Powerbuilder 12.5 下载地址
    C# 给程序添加许可
    WIN FORM 多线程更新UI(界面控件)
    .Net WinForm 拖动控件
    SQL Server 自动增长清零
    C# 一次生成多个相同的字符
  • 原文地址:https://www.cnblogs.com/LyShark/p/10775440.html
Copyright © 2011-2022 走看看