메모리 주소만으로 Heap에서 해당 타입의 크기와 배열 인자의 개수를 알아내는 방법

Posted by 빵빵빵
2009/03/04 20:01 전산(컴퓨터)/PC-Windows



  1. #include <Tlhelp32.h>   
  2.   
  3. int GetVoidPointerSize(void *nPoint)   
  4. {   
  5.     int pid = (int)GetCurrentProcessId();   
  6.     HANDLE hSnap;   
  7.     HEAPLIST32 hl;   
  8.     HEAPENTRY32 he;   
  9.   
  10.         hSnap = CreateToolhelp32Snapshot( TH32CS_SNAPHEAPLIST, pid );   
  11.         if( hSnap == (HANDLE)-1 ){ return 0; }   
  12.   
  13.         hl.dwSize=sizeof(HEAPLIST32);   
  14.         if (Heap32ListFirst(hSnap,&hl))   
  15.         {   
  16.             do   
  17.             {   
  18.                 he.dwSize=sizeof(HEAPENTRY32);   
  19.                 if (Heap32First(&he,pid,hl.th32HeapID))   
  20.                 {   
  21.                     do   
  22.                     {   
  23.                         if( he.dwAddress == (DWORD)nPoint )   
  24.                         {   
  25.                             // get kernel heap allocation chunk size   
  26.                             char *t = (char*)(((unsigned char*)he.dwAddress) - 2);   
  27.                             return he.dwBlockSize - *(t);   
  28.                         }   
  29.   
  30.                     } while (Heap32Next(&he));   
  31.                 }   
  32.             } while (Heap32ListNext(hSnap,&hl));   
  33.         }   
  34.         CloseHandle(hSnap);   
  35.     return 0;   
  36. }  

2009/03/04 20:01 2009/03/04 20:01

이 글에는 트랙백을 보낼 수 없습니다