C++ handleProcess Error

C++ handleProcess Error

本文关键字:Error handleProcess C++      更新时间:2023-10-16

我一直在尝试编写一个c++内存编辑器,尽管我包含了#include <Windows.h>库,但它仍然给了我一个错误"handleprocess未声明!"。下面是代码:

#include iostream
#include Windows.h
using namespace std;
int newScore;
int main()
{
HWND windowProgram = FindWindow(NULL,"Calculator");
cout << "Enter A new value to write:";
cin>>::newScore;
if(windowProgram == 0) {
    cerr << "Unable To Locate Window" <<endl;
}else {
    DWORD processID;
    GetWindowThreadProcessId(windowProgram,&processID);
    HANDLE handleProcess = OpenProcess(PROCESS_ALL_ACCESS,FALSE,processID);
}
    if(!handleProcess){
        cerr << "Unable to handle process: " <<handleProcess<< " ! " << endl;
    }else {
        int memoryHack = WriteProcessMemory(
        handleProcess,
        (LPVOID)0XA18803B1CC,
        &newScore,
        (DWORD)sizeof(newScore),NULL);
        if(memoryHack > 0){
            clog<< " Memory Written" <<endl;
        }else{
            cerr<<"Failed to write to memory"<<endl;
        }
        CloseHandle(handleProcess);
    }
    cin.sync(),
    cin.ignore();
    return (0);
}

您需要在其他地方声明handleProcess,以便在else作用域之外可见。例如:

// ...
HANDLE handleProcess = 0; // Declare and initialize here.
if (windowProgram == 0) {
    cerr << "Unable To Locate Window" <<endl;
} else {
    DWORD processID;
    GetWindowThreadProcessId(windowProgram, &processID);
    handleProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processID);
}
if (!handleProcess) {
// ...

注意,这不仅仅是标识符可见性问题。当退出作用域时,在堆栈上和作用域内创建的非静态变量将被销毁,并且不再存在。

相关文章: