15,466
社区成员
发帖
与我相关
我的任务
分享
hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, processEntry.th32ProcessID);
和
WriteProcessMemory(hProcess,
&array,
writeContent,
10,
&realWritten);
#include <stdio.h>
#include <windows.h>
#include <Tlhelp32.h>
#include <stdlib.h> // _MAX_PATH, _wsplitpath
#include <string.h> // wcscmp
int main()
{
char input;
char array[] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
char buf[11];
char *writeContent = "1234567890";
WCHAR currentExeName[_MAX_PATH];
GetModuleFileName(NULL, currentExeName, _MAX_PATH);
wchar_t drive[_MAX_PATH];
wchar_t dir[_MAX_PATH];
wchar_t fname[_MAX_PATH];
wchar_t ext[_MAX_PATH];
_wsplitpath(currentExeName, drive, dir, fname, ext);
wsprintf(currentExeName, L"%s%s", fname, ext);
HANDLE snapShot = NULL;
snapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if (snapShot == INVALID_HANDLE_VALUE)
{
printf("CreateToolhelp32Snapshot fails.\n");
return -1;
}
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(processEntry);
bool ret = FALSE;
ret = Process32First(snapShot, &processEntry);
while(ret)
{
//wprintf(L"Name:%s\tProcessId:%d\tThreads:%d\n",
// processEntry.szExeFile,
// processEntry.th32ProcessID,
// processEntry.cntThreads);
if (0 == wcscmp(processEntry.szExeFile, currentExeName))
{// exe文件名跟自己一样
if (processEntry.th32ProcessID != GetCurrentProcessId())
{// 进程id与自己不同
// 往别的进程里写数据。
HANDLE hProcess = NULL;
hProcess = OpenProcess(PROCESS_ALL_ACCESS, NULL, processEntry.th32ProcessID);
if (NULL == hProcess)
{
printf("This process can't be opened.\n");
}
else
{
SIZE_T realWritten = 0;
WriteProcessMemory(hProcess,
&array,
writeContent,
10,
&realWritten);
}
CloseHandle(hProcess); hProcess = NULL;
// 从别的进程里读数据。
memset(array, 'a', 10); // 先把自己的array设成全a,再看别人的。
SIZE_T realRead = 0;
memset(buf, 0, 11);
Toolhelp32ReadProcessMemory(processEntry.th32ProcessID,
&array,
buf,
sizeof(array),
&realRead);
printf("%s\n", buf);
}
}
ret = Process32Next(snapShot, &processEntry);
}
CloseHandle(snapShot); snapShot = NULL;
system("pause"); // 暂停一下,让进程等着,别死。
return 0;
}