vendredi 24 juin 2016

Program crashes when trying to dereference non null pointer to BYTE aka(unsigned char)

I'm trying to dereference the modBaseAddr (BYTE* aka unsigned char*) member of a MODULEENTRY32 structure in tlhelp32.h. But when I try to dereference it my program crashes. I did an if statement to make sure it wasn't a nullptr. It isn't but my program still crashes. Here's my code:

#include <windows.h>
#include <tlhelp32.h>
#include <iostream>
using namespace std;

int main()
{
    DWORD pid = 0xFFFFFFFF;
    MODULEENTRY32 mod_entry;
    mod_entry.dwSize = sizeof(MODULEENTRY32);
    HANDLE procH;

    cout << "PID: " << flush;
    cin >> pid;

    procH = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE|TH32CS_SNAPMODULE32, pid);
    if(procH == INVALID_HANDLE_VALUE)
    {
        cout << "Failed to retrieve process handle" << endl;
        CloseHandle(procH);
        return 1;
    }
    if(!(Module32First(procH, &mod_entry))){cout << "Mod32 1st failed\n";CloseHandle(procH);return 1;}

    if(mod_entry.modBaseAddr == nullptr){cout << "modBaseAddr is empty\n";CloseHandle(procH);return 1;}
    BYTE mod_baseaddr = *mod_entry.modBaseAddr; //<-------------------Causes program to crash.

    return 0;
}

Aucun commentaire:

Enregistrer un commentaire