title: Virtual Memory Sample code WinApi C++
body: <p>I have some problems with tutorial code about Memory allocation.
  Below there is a code which do:
  1) print virtal memory data
  2) memory allocation
  3) print virtal memory data
  4) memory releasing
  5) print virtal memory data</p>
  
  <p>I am only starting learning C++ for WinApi. Can somebody help me with code?
  I cannot compile it.I am getting some errors(are shown below).
  As I know this code worked correctly in earlier versions of MS Visual Studio.
  Currently I am working with 2010 version.
  Thanks in advance.</p>
  
  <pre><code> #include "stdafx.h"
  
  LPVOID myblock[5];
  BOOL alloc = FALSE;
  
  PCTSTR GetProtectText(MEMORY_BASIC_INFORMATION mbi)
  {
      PCTSTR p = "Unknown";
      if(mbi.State == MEM_FREE) mbi.Protect = PAGE_NOACCESS;
      if(mbi.State == MEM_RESERVE) mbi.Protect = mbi.AllocationProtect;
      switch (mbi.Protect &amp; ~(PAGE_GUARD | PAGE_NOCACHE | PAGE_WRITECOMBINE))
      {
          case PAGE_READONLY:          p = "-R--"; break;
          case PAGE_READWRITE:         p = "-RW-"; break;
          case PAGE_WRITECOPY:         p = "-RWC"; break;
          case PAGE_EXECUTE:           p = "E---"; break;
          case PAGE_EXECUTE_READ:      p = "ER--"; break;
          case PAGE_EXECUTE_READWRITE: p = "ERW-"; break;
          case PAGE_EXECUTE_WRITECOPY: p = "ERWC"; break;
          case PAGE_NOACCESS:          p = "----"; break;
      }
      return(p);
  }
  
  PCTSTR GetMemStorageText(MEMORY_BASIC_INFORMATION mbi)
  {
     PCTSTR p = TEXT("Unknown");
  
     switch (mbi.State)
     {
          case MEM_FREE:      p = "Free";     break;
          case MEM_RESERVE:       p = "Reserve";  break;
          case MEM_COMMIT:
              switch (mbi.Type)
              {
                  case MEM_FREE:  p = "Free";     break;
                  case MEM_RESERVE:   p = "Reserve";  break;
                  case MEM_IMAGE: p = "Image";    break;
                  case MEM_MAPPED:    p = "Mapped";   break;
                  case MEM_PRIVATE:   p = "Private";  break;
              }
              break;
     }
     return(p);
  }
  
  VOID MemoryMap()
  {
      DWORD dwProcessId = GetCurrentProcessId(),
      dwNeeded = 0;
      HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwProcessId);
      HMODULE lpModule[40];  //handle to a module
      LPVOID lpModuleAddress[40];
      MODULEINFO ModuleInfo; //structure contains the module load address, size, and entry point. 
  
      EnumProcessModules(hProcess, lpModule, sizeof(HMODULE)*40, &amp;dwNeeded);
  
      dwNeeded /= sizeof(HMODULE);
  
      for(UINT i = 0;i &lt; dwNeeded;i++)
      {
          GetModuleInformation(hProcess, lpModule[i], &amp;ModuleInfo, sizeof(ModuleInfo));
          lpModuleAddress[i] = ModuleInfo.lpBaseOfDll;
      }
  
      MEMORY_BASIC_INFORMATION mbi; //structure contains information about a range of                       //pages in the virtual address space of a process
      PVOID lpAddress = NULL;
  
      printf("  Address\tBlok Size\tStorage\t\tProtect\n");
      CHAR str[255];
  
      while(VirtualQueryEx(hProcess, lpAddress, &amp;mbi, sizeof(mbi)))
      {
          if(!(((UINT)lpAddress) % 0x10000))
          {
              printf("0x%p\t\t\t%s", lpAddress, GetMemStorageText(mbi));
              for(int i = 0;i &lt; 5;i++)
                  if(myblock[i] == lpAddress &amp;&amp; alloc) printf("\n(My Al-location Block)");
  
              for(int i = 0;i &lt; dwNeeded;i++)
                  if(lpAddress == lpModuleAddress[i])
                  {
                      GetModuleFileName(lpModule[i], str, 255);
                      printf("\n(Image: %s)", str);
                  }
              printf("\n");
          }
          printf("  0x%p\t0x%p\t%s\t\t%s\n", lpAddress, (DWORD)mbi.RegionSize,
              GetMemStorageText(mbi), GetProtectText(mbi));
          lpAddress = ((PBYTE)lpAddress + mbi.RegionSize);
      };
  }
  
  VOID Allocation()
  {
      alloc = TRUE;
      myblock[0] = VirtualAlloc(NULL, 1024, MEM_RESERVE, PAGE_READONLY);
      myblock[1] = VirtualAlloc(NULL, 1024*2, MEM_RESERVE, PAGE_READWRITE);
      myblock[2] = VirtualAlloc(NULL, 1024*8, MEM_RESERVE, PAGE_EXECUTE);
      myblock[3] = VirtualAlloc(NULL, 1024*16, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READ);
      myblock[4] = VirtualAlloc(NULL, 1024*16, MEM_RESERVE|MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  }
  
  VOID Release()
  {
      for(int i = 0;i &lt; 5;i++)
          VirtualFree(myblock[i], 0, MEM_RELEASE);
      alloc = FALSE;
  }
  
  
  
  INT _tmain(INT argc, _TCHAR* argv[])
  {
      printf("\nProcess Virtual Memory Before Allocation Memory:\n");
      MemoryMap();
      printf("\nProcess Virtual Memory After Allocation Memory:\n");
      Allocation();
      MemoryMap();
      printf("\nProcess Virtual Memory After Release Memory:\n");
      Release();
      MemoryMap();
      getch();
      return 0;
  }
  
  1&gt;------ Build started: Project: VirtMemory, Configuration: Debug Win32 ------
  1&gt;Build started 06.10.2011 23:51:01.
  1&gt;InitializeBuildStatus:
  1&gt;  Touching ".\Debug\VirtMemory.unsuccessfulbuild".
  1&gt;ClCompile:
  1&gt;  VirtMemory.cpp
  1&gt;e:\виртуальная память\kod1\virtmemory.cpp(80): warning C4018: '&lt;' : signed/unsigned mismatch
  1&gt;e:\виртуальная память\kod1\virtmemory.cpp(123): warning C4996: 'getch': The POSIX name for this item is deprecated. Instead, use the ISO C++ conformant name: _getch. See online help for details.
  1&gt;          d:\program files\microsoft visual studio 10.0\vc\include\conio.h(128) : see declaration of 'getch'
  1&gt;ManifestResourceCompile:
  1&gt;  All outputs are up-to-date.
  1&gt;VirtMemory.obj : error LNK2019: unresolved external symbol _GetModuleInformation@16 referenced in function "void __cdecl MemoryMap(void)" (?MemoryMap@@YAXXZ)
  1&gt;VirtMemory.obj : error LNK2019: unresolved external symbol _EnumProcessModules@16 referenced in function "void __cdecl MemoryMap(void)" (?MemoryMap@@YAXXZ)
  1&gt;.\Debug\VirtMemory.exe : fatal error LNK1120: 2 unresolved externals
  1&gt;
  1&gt;Build FAILED.
  1&gt;
  1&gt;Time Elapsed 00:00:01.43
  enter code here
  </code></pre>