Total Anime Immersion
Total Anime Immersion

Reputation: 469

Error while trying to run the code

Whenever I try to run the following bit of code, I get an error.Image_Error

Image2_Error Added another error image from Visual Studio. Image3_error Added the exact source of error.

The following bit of code here is : 

void update_memblock(MEMBLOCK *mb)
{
    static unsigned char tempbuf[128 * 1024];
    unsigned int bytes_left;
    unsigned int total_read;
    unsigned int bytes_to_read;
    unsigned int bytes_read;


        bytes_left = mb->size;
        total_read = 0;


        while (bytes_left)
        {
            bytes_to_read = (bytes_left > sizeof(tempbuf)) ? sizeof(tempbuf) : bytes_left;
            ReadProcessMemory(mb->hProc, mb->addr + total_read, tempbuf, bytes_to_read, (DWORD*)&bytes_read);
            if (bytes_read != bytes_to_read) break;

            memcpy(mb->buffer + total_read, tempbuf, bytes_read);

            bytes_left -= bytes_read;
            total_read += bytes_read;
        }

        mb->size = total_read;

}



The Structure looks as follows : 
typedef struct _MEMBLOCK { 
    HANDLE hProc;   
unsigned char *addr;    
int size;   
unsigned char *buffer;
    struct _MEMBLOCK *next;

} MEMBLOCK;

Tried switching around quite a bit, from changing the array size to removing the variable and switching it with another, apparently it still throws this error. Kindly help me figure out where the problem is. Thanks.

Upvotes: 0

Views: 48

Answers (1)

If you read the documentation, you'd notice that the arguments to ReadProcessMemory are SIZE_Ts as they should. DWORD type is 32 bits and sizes 64 bits on 64-bit platform. While it doesn't matter for bytes_to_read as it is passed in by value, 8 bytes will be written to the pointer pointed to by bytes_read whose sizeof is 4.

Furthermore, you should get a pointer type mismatch for this, as DWORD * is not compatible with SIZE_T * in that case.

Upvotes: 2

Related Questions