0

我正在开发一个程序,我需要程序的句柄才能在其他类中使用并且不更改值,我尝试将类更改为结构,但没有用,我试图将句柄复制到attach 的受保护部分。那么我如何实现其他类的变量句柄。

struct attach
{
//Tried this HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, NULL, ProcID);
void Attach(char* name)
{
    HWND hwnd = FindWindowA(NULL, name);
        if (hwnd == NULL)
        {
            MessageBoxA(NULL, "Failed to attach Window", "attention", MB_OK | MB_ICONERROR);
            exit(0);
        }
        else
        {
            cout << hwnd << endl;
            DWORD ProcID;
            GetWindowThreadProcessId(hwnd, &ProcID);
           if(handle == NULL)
            {
                MessageBoxA(NULL, "Failed to obtain Process ID", "attention", MB_OK | MB_ICONERROR);
                HANDLE handle = OpenProcess(PROCESS_ALL_ACCESS, NULL, ProcID);
            }
            else
            {
                cout << ProcID << endl;;
                cout << handle;
            }
        }
}
protected:
  };

class Sonic_Heroes : protected attach
{
protected:
void cp()
{
    ReadProcessMemory(handle, (PBYTE*)RINGS, &NEWRINGS, sizeof(int), 0);
    ReadProcessMemory(handle, (PBYTE*)LIVES, &NEWLIVES, sizeof(int), 0);
    ReadProcessMemory(handle, (PBYTE*)TEAMBLAST, &NEWTEAMBLAST, sizeof(int), 0);
    ReadProcessMemory(handle, (PBYTE*)FLIGHTHEIGHT, &NEWFLIGHTHEIGHT, sizeof(float), 0);
}
char name[18]= "SONIC HEROES(TM)";
const DWORD RINGS = 0x009DD70C;
const DWORD LIVES = 0x009DD74C;
const DWORD TEAMBLAST = 0x009DD72C;
const DWORD FLIGHTHEIGHT = 0x00789FA4;
int NEWRINGS;
int NEWLIVES;
int NEWTEAMBLAST;
float NEWFLIGHTHEIGHT;
};
 class Sonic_Mania : protected attach
 {
 protected:
void CP()
{
    ReadProcessMemory(handle, (PBYTE*)RINGS, &NEWRINGS, sizeof(int), 0);
    ReadProcessMemory(handle, (PBYTE*)SCORE, &NEWSCORE, sizeof(int), 0);
    ReadProcessMemory(handle, (PBYTE*)LIVES, &NEWLIVES, sizeof(int), 0);
}
char name[13]= "Sonic mania";
const DWORD RINGS = 0x00A4D644;
const DWORD SCORE = 0x00A4D654;
const DWORD LIVES = 0x00A4D650;
int NEWRINGS;
int NEWSCORE;
int NEWLIVES;
};
4

1 回答 1

0

HANDLE handle是方法中的局部变量Attach,而不是类/结构的一部分。在结构中创建一个HANDLE成员,它可以工作(不能尝试):

struct attach
{
HANDLE handle; // if handle has no default constructor you need to use pointers

void Attach(char* name)
{
    HWND hwnd = FindWindowA(NULL, name);
    if (hwnd == NULL)
    {
        MessageBoxA(NULL, "Failed to attach Window", "attention", MB_OK | MB_ICONERROR);
        exit(0);
    }
    else
    {
        cout << hwnd << endl;
        DWORD ProcID;
        GetWindowThreadProcessId(hwnd, &ProcID); // here an if-statement is missing i think?
        if(handle == NULL) 
        {
            MessageBoxA(NULL, "Failed to obtain Process ID", "attention", MB_OK | MB_ICONERROR);
            handle = OpenProcess(PROCESS_ALL_ACCESS, NULL, ProcID);
        }
        else // this won't compile
        {
            cout << ProcID << endl;;
            cout << handle;
        }
    }
}
};
于 2020-04-22T13:41:21.517 回答