[英]How can I implement a handle to use in another class as well as the class it was made in? C++
我正在開發一個程序,我需要程序的句柄才能在其他類中使用並且不更改值,我嘗試將 class 更改為 struct,沒有用,我嘗試將句柄復制到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;
};
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;
}
}
}
};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.