Read bytes of loaded executable from memory : I am trying to make an executable which can read itself from memory using **ReadProcessMemory** api of windows.

Then, I will use this to calculate the checksum of executable.

This is my code : 


    #define PSAPI_VERSION 1
    #include <string>
    #include <windows.h>
    #include <tchar.h>
    #include <stdio.h>
    #include <psapi.h>
    #include <Wincrypt.h>
    
    #define BUFSIZE 1024
    #define MD5LEN  16
    
    // To ensure correct resolution of symbols, add Psapi.lib to TARGETLIBS
    #pragma comment(lib, "psapi.lib")
    
    
    int main(void)
    {
    
        HWND hMyProcess = (HWND)(GetCurrentProcess());
        HMODULE hModule = (HMODULE)GetModuleHandle(NULL);
        TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>");
        MODULEINFO moduleInfo;
        if(hModule != NULL && hMyProcess != NULL){
            // if (GetModuleInformation())
            GetModuleBaseName(hMyProcess, hModule, szProcessName, MAX_PATH);
            printf("%s\n", szProcessName);
            if (GetModuleInformation(hMyProcess, hModule, &moduleInfo, sizeof(moduleInfo))){
                printf("lpBaseOfDLL : %x\n", moduleInfo.lpBaseOfDll);
                printf("Entry Point : %x\n", moduleInfo.EntryPoint);
                printf("SizeOfImage : %x\n", moduleInfo.SizeOfImage);
            }
        }
        // Till here working fine, problem lies below
    
        // read process memory
        TCHAR *hexEXE;
        SIZE_T *lpNumberOfBytesRead;
        if(ReadProcessMemory(hMyProcess, moduleInfo.lpBaseOfDll,
            &hexEXE, moduleInfo.SizeOfImage, 0)){
            //printf("%s\n", hexEXE);
            printf("Read memory\n");
            printf("%d \n",strlen(hexEXE));
        }

        // will be implemented later, taken from --> https://msdn.microsoft.com/en-us/library/aa382380(VS.85).aspx
        DWORD dwStatus = 0;
        BOOL bResult = FALSE;
        HCRYPTPROV hProv = 0;
        HCRYPTHASH hHash = 0;
    
        /*if (!CryptAcquireContext(&hProv,NULL,NULL,PROV_RSA_FULL,CRYPT_VERIFYCONTEXT)){
            dwStatus = GetLastError();
            printf("CryptAcquireContext failed: %d\n", dwStatus);
            //CloseHandle(hFile);
            return dwStatus;
        }
    
        if (!CryptCreateHash(hProv, CALG_MD5, 0, 0, &hHash)){
            dwStatus = GetLastError();
            printf("CryptAcquireContext failed: %d\n", dwStatus);
            //CloseHandle(hFile);
            CryptReleaseContext(hProv, 0);
            return dwStatus;
        }*/


    
        return 0;
    }


## Problem :

I am not able to read the my own process's memory, it's the first time I'm using **WinAPI**, so perhaps I am using the function in some wrong way.

The program just hangs and it shows "Windows has encountered some problem..."

Thanks.
