简体   繁体   中英

(C++) ReadProcessMemory - Problems with reading one byte

The problem I'm having is in the 'Address value:' section. Basically, if I don't put byteSize to 4 (int), it will cout -858993409 as the address value. The 4 bytes at 0x242E1698 are FF 00 00 00. If I put byteSize as 4, it will output 255 (as I want it to). Is there any way to output 255 by only reading one byte?

int byteSize = 1;       
if (!ReadProcessMemory(hProcess, (void *)0x242E1698, (void *)&healthVar, byteSize, NULL))
    cout << "Failed to read " << windowName << "'s memory!\n\n";
else
{
    cout << "Address value: " << healthVar << "\n\n";
}

If you read only one byte, you need to read it into a one-byte integer:

uint8_t healthVar;

Then once you read your value (255) into there, you can print it this way:

cout << unsigned(healthVar) << endl;

Note you cannot simply print healthVar because in C++ a one-byte integer will be printed as a character instead.

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