[Reposted] How to get the largest available continues memory block

xiaoxiao2021-03-06  101

Original Address: http: //blog.kalmbachnet.de/ postid = 9 Some applications require a huge amount of continues memory And sometimes, windows is not able to satisfy the request To find out the reason of why the virtual memory is?.. Badly s Larges Continous Memory Block, You Can Use The Following Code To Get The Info About The Virtual Address Space:

#include

#include

#include

#include

#pragma comment (lib, "psapi.lib")

Void PrintMeminfo (Memory_basic_Information & MBI)

{

IF (Mbi.State! = MEM_FREE)

Return;

_tprintf (_T ("BaseAddress: 0x% 8.8X, 0x% 8.8X, 0x% 8.8X / N"),

Mbi.Baseaddress, Mbi.allocationBase, Mbi.allocationProtect);

_tprintf (_T ("Regionsize: 0x% 8.8x / n"), MBI.REGIONSize;

_tprintf (_T ("state:"));

Switch (Mbi.State)

{

Case Mem_Commit:

_tprintf (_T ("commit / n"));

Break;

Case Mem_Free:

_tprintf (_T ("free / n"));

Break;

Case Mem_Reserve:

_tprintf (_t ("reserve / n"));

Break;

DEFAULT:

_tprintf (_t ("unknown / n"));

Break;

}

_tprintf (_T ("type:"));

Switch (Mbi.Type)

{

Case Mem_Image:

_tprintf (_t ("image / n"));

Break;

Case MEM_MAPPED:

_tprintf (_t ("mapped / n")));

Break;

Case Mem_Private:

_tprintf (_t ("private / n"));

Break;

DEFAULT:

_tprintf (_T ("unknown (% d) / n"), mbi.type);

Break;

}

_tprintf (_t ("/ n"));

}

Void PrintModules (DWORD Processid)

{

HModule HMODS [1024];

Handle HPROCESS;

DWORD CBNEDED;

Unsigned Int i;

_tprintf (_t ("/ nprocess id:% u / n"), processid;

HProcess = OpenProcess (Process_Query_Information | Process_VM_Read, False, ProcessID);

IF (null hprocess) return;

IF (HProcessModules (HProcess, HMODS, SIZEOF (HMODS), & CBNeeded)

{

For (i = 0; cbeneeded / sizeof (hmodule)); i )

{

Tchar SzmodName [MAX_PATH];

IF (GetModuleFileNameex (HProcess, HMODS [I], SzModName, Sizeof (SzmodName)))

{

_tprintf (_t ("/ t% s (0x% 08x) / n"), SzmodName, HMODS [I]);

}

}

}

CloseHandle (HPROCESS);

}

SIZE_T GetLargestFreeMemRegion (LPVOID * LPBASEADDR, BOOL ShowMemInfo)

{

System_info systeminfo;

GetSystemInfo (& SystemInfo);

Void * p = 0;

Memory_basic_information mbi;

SIZE_T LARGESTSIZE = 0;

While (P

{

SIZE_T DWRET = VirtualQuery (P, & MBI, SIZEOF (MBI));

IF (dwret> 0)

{

IF (ShowMeminfo)

PrintMemInfo (MBI);

IF (Mbi.State Mem_Free)

{

Largestsize

{

Largestsize = mbi.regionsize;

IF (LPBaseAddr! = NULL)

* lpbaseaddr = mbi.baseaddress;

}

}

P = (void *) ((char *) p) mbi.regionSize;

}

Else

{

IF (ShowMeminfo)

_tprintf (_t ("### VirtualQuery Failed (% P) / N"), P);

P = (void *) ((char *) p) systemInfo.dwpagesize;

}

}

Return Largestsize;

}

Int_tmain ()

{

LPVOID BaseAddr;

SIZE_T LS = GetLargestFreeMemregion (& BaseAddr, true);

PRINTMODULES (GetCurrentProcessid ());

_tprintf ("/ nlargest free region: 0x% 8.8x Bytes AT 0x% 8.8x / n"), LS, BaseAddr;

Return 0;

}

转载请注明原文地址:https://www.9cbs.com/read-96112.html

New Post(0)