简体   繁体   中英

How can I implement a handle to use in another class as well as the class it was made in? C++

i'm working on a program where i need the handle of the program to be able to be used in the other classes and without changing the value, ive tried changing class to struct, didn't work, i tried to copy the handle into the protected part of attach. So how do i implement the varible handle to other classes.

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 is a local variable in the method Attach , not part of the class/struct. Create a HANDLE member in the struct and it could work (cannot try it):

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;
        }
    }
}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM