我刚刚开始学习 Windows API,我想制作一个简单的程序来读取给定进程中的值。
这是我的代码
#include <stdio.h>
#include <Windows.h>
void printError();
int main()
{
int *buffer;
// process input
unsigned int pid = 0;
printf("process ID : ");
scanf("%d", &pid);
getchar();
HANDLE authorisation = OpenProcess(PROCESS_VM_READ, FALSE, pid);
if(authorisation == NULL)
{
printf("OpenProcess Failed. GetLastError = %d", GetLastError());
getchar();
return EXIT_FAILURE;
}
// adress memory input
int *memoryAddress = 0x0;
printf("memory address to read (in hexadecimal) : ");
scanf("%p", &memoryAddress);
getchar();
printf("Reading from address : %p", memoryAddress);
if(ReadProcessMemory(authorisation, (LPCVOID)memoryAddress, &buffer, 8,
NULL))
{
printf("\nptr2int = %p\n", buffer);
}
else
{
printError();
}
int varInt = 0;
// HERE IS THE PROBLEM
// GetLastError return 6 : Invalid Handle
if(ReadProcessMemory(authorisation, (LPCVOID)buffer, &varInt,
sizeof(int), NULL))
{
printf("varInt = %d\n", varInt);
}
else
{
printError();
}
printf("Press ENTER to quit");
getchar();
return 0;
}
void printError()
{
printf("ReadProcessMemory Failed. GetLastError = %d", GetLastError());
getchar();
return EXIT_FAILURE;
}
但是,如果我为 RPM 的第二次调用创建一个新的句柄,它就可以完美地工作。
我在 MSDN 上读到:
OpenProcess 函数返回的句柄可用于任何需要进程句柄的函数”
我究竟做错了什么 ?