简体   繁体   中英

C++ Pointers, Unable To Read Memory

Why, when I initiate my pointer, I can read the assigned value:

DWORD *pBytesReturned = new DWORD[0];
_result =  EnumProcesses(pProcessIds, 1000, pBytesReturned);

在此处输入图片说明

But when I initialize it like this I can't read the assigned value:

DWORD *pBytesReturned = 0;
_result =  EnumProcesses(pProcessIds, 1000, pBytesReturned);

在此处输入图片说明

Here's the complete code if needed:

#include "stdafx.h"
#include <Psapi.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR pCmdLine, int iCmdShow)
{
    bool _result;
    DWORD *pProcessIds = new DWORD[1000];
    DWORD cb;
    DWORD *pBytesReturned =0;
    _result =  EnumProcesses(pProcessIds, 1000, pBytesReturned);
}

The intent of the function

BOOL
WINAPI
EnumProcesses (
    _Out_writes_bytes_(cb) DWORD * lpidProcess,
    _In_ DWORD cb,
    _Out_ LPDWORD lpcbNeeded
    );

is to have a pointer as a third parameter, and this pointer must point to a valid memory location where the function will be storing a dword. The expected function call should look like this

DWORD *pProcessIds = new DWORD[1000];
DWORD bytesReturned = 0;
bool _result = EnumProcesses(pProcessIds, 1000, &bytesReturned);

or like this

DWORD *pProcessIds = new DWORD[1000];
DWORD *pBytesReturned = new DWORD[1];
bool _result = EnumProcesses(pProcessIds, 1000, pBytesReturned);

but you should not be using a NULL pointer and expect the debugger to dereference it.

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