Helper Function
더보기 접기
CString GetMemBlockInfo(MEMORY_BASIC_INFORMATION *pmbi) { CString str, temp; str.Format(_T("%X,%X"), pmbi->BaseAddress, pmbi->RegionSize);//pmbi->AllocationBase, // str+= _T(" | TY"); temp.Format(_T("_%X"), pmbi->Type); str+=temp; DWORD nType = pmbi->Type; if( nType & MEM_PRIVATE ){ str +=_T("_PRIVATE");} if( nType & MEM_MAPPED ){ str +=_T("_MAPPED");} if( nType & MEM_IMAGE ){ str +=_T("_IMAGE");} // str+= _T(" | ST"); temp.Format(_T("_%X"), pmbi->State); str+=temp; DWORD nState = pmbi->State; if( nState & MEM_COMMIT ){ str +=_T("_COMMIT");} if( nState & MEM_FREE ){ str +=_T("_FREE");} if( nState & MEM_RESERVE ){ str +=_T("_RESERVE");} // str+= _T(" | A_PT"); temp.Format(_T("_%X"), pmbi->AllocationProtect); str+=temp; DWORD nAllocProtect = pmbi->AllocationProtect; if( nAllocProtect & PAGE_READWRITE ){ str +=_T("_RW");} if( nAllocProtect & PAGE_READONLY ){ str +=_T("_RO"); } if( nAllocProtect & PAGE_WRITECOPY ){ str +=_T("_WC");} if( nAllocProtect & PAGE_EXECUTE ){ str +=_T("_E");} if( nAllocProtect & PAGE_EXECUTE_READ ){ str +=_T("_ER");} if( nAllocProtect & PAGE_EXECUTE_READWRITE ){ str +=_T("_ERW");} if( nAllocProtect & PAGE_EXECUTE_WRITECOPY ){ str +=_T("_EWC");} if( nAllocProtect & PAGE_GUARD ){ str +=_T("_G");} if( nAllocProtect & PAGE_NOACCESS ){ str +=_T("_NA");} if( nAllocProtect & PAGE_NOCACHE ){ str +=_T("_NC");} //}else{ str += temp;//_T("_ _"); } //str += temp; // str+= _T(" | PT"); temp.Format(_T("_%X"), pmbi->Protect); str+=temp; DWORD nProtect = pmbi->Protect; if( nProtect & PAGE_READWRITE ){ str +=_T("_RW");} if( nProtect & PAGE_READONLY ){ str +=_T("_RO"); } if( nProtect & PAGE_WRITECOPY ){ str +=_T("_WC");} if( nProtect & PAGE_EXECUTE ){ str +=_T("_E");} if( nProtect & PAGE_EXECUTE_READ ){ str +=_T("_ER");} if( nProtect & PAGE_EXECUTE_READWRITE ){ str +=_T("_ERW");} if( nProtect & PAGE_EXECUTE_WRITECOPY ){ str +=_T("_EWC");} if( nProtect & PAGE_GUARD ){ str +=_T("_G");} if( nProtect & PAGE_NOACCESS ){ str +=_T("_NA");} if( nProtect & PAGE_NOCACHE ){ str +=_T("_NC");} return str; } int MemInfo( DWORD _dwAddr, MEMORY_BASIC_INFORMATION *pmbi_, DWORD *pdwLimitSize_) { CString str; //char buff[10]; int ret =0, nMemBlockSize=0; bool bret=false; MEMORY_BASIC_INFORMATION mbi; // 페이지 정보 if( pdwLimitSize_ ) *pdwLimitSize_ = 0; if( pmbi_ ) memset(pmbi_, 0, sizeof(MEMORY_BASIC_INFORMATION)); if( _dwAddr >= MEM_ADDR_MAX) return FALSE; if( sizeof(mbi) == ( ret=VirtualQuery( (LPVOID)_dwAddr, &mbi, sizeof(mbi)) ) ){ if( pmbi_ ) *pmbi_ = mbi; str = GetMemBlockInfo( &mbi); DWORD nMemBlockSize = ((DWORD)mbi.BaseAddress + mbi.RegionSize) - _dwAddr; if( pdwLimitSize_ ) *pdwLimitSize_ = nMemBlockSize; if( (mbi.Type ==0) || (mbi.AllocationProtect == 0) || (mbi.State == 0) || (mbi.State == MEM_FREE) || (mbi.State == MEM_RESERVE) // //||(mbi.Type == MEM_IMAGE) //||(mbi.Type == MEM_MAPPED) //||(mbi.Type == MEM_PRIVATE) ){ //읽지 못하는 상태 //if( pbReadEnable ) *pbReadEnable = false; //CString str = GetMemBlockInfo( &mbi); _DbgStr(_T("MemInfo-상태 문제(X)-%s,%s"), str, DbgMsg()); bret=false; }else{ //if( pbReadEnable ) *pbReadEnable = true; //_DbgStr(_T("MemInfo-상태 -정상- addr=%X, (size=%X)"),mbi.BaseAddress, mbi.RegionSize ); _DbgStr(_T("MemInfo-상태 정상(0)- addr=%X, %s"), _dwAddr, str ); bret=true; } }else{ _DbgStr(_T("MemInfo-VirtualQuery-에러-%X- 끝냄"), _dwAddr); return FALSE; return false; } //if( pmbi_ ) *pmbi_ = mbi; return bret; } int MemPtStatus( DWORD _dwAddr, DWORD *pdwProtectStatus_) { int ret=0; MEMORY_BASIC_INFORMATION mbi; // 페이지 정보 if( sizeof(mbi) == ( ret=VirtualQuery( (LPVOID)_dwAddr, &mbi, sizeof(mbi)) ) ){ //if(pdwProtectStatus_) *pdwProtectStatus_ = mbi.AllocationProtect;// 할당시 보호 상태 if(pdwProtectStatus_) *pdwProtectStatus_ = mbi.Protect;//COMMIT 상태에서 보호 상태 return TRUE; } return FALSE; }
접기
int _Test() { //메모리 주소 지정 할당 DWORD dwAddr = 0; MEMORY_BASIC_INFORMATION mbi; DWORD dwMemSize=0; // const int SIZE=10;//원하는 할당크기 BYTE buf[SIZE+1],buf2[SIZE+1];memset(buf,0,sizeof(buf));memset(buf2,0,sizeof(buf2)); if(dwAddr) MemInfo((DWORD)dwAddr, &mbi, &dwMemSize);//정보 //VirtualAlloc을 위한 할당 크기 구하기 SYSTEM_INFO sysInfo;GetSystemInfo(&sysInfo); DWORD dwPageCount = (DWORD)(SIZE / sysInfo.dwPageSize+1); DWORD dwAllocSize = dwPageCount * sysInfo.dwPageSize; pMem=(char*)0x79000000;//주소 지정 g_dwAddr = dwAddr = (DWORD)pMem ; MemInfo((DWORD)dwAddr, &mbi, &dwMemSize); DWORD dwMemProtectStatus = 0, dwOldStatus=0, dwStatusIn=0; if( MemPtStatus( dwAddr, &dwMemProtectStatus) ){ //==================================================================== //VirtualAlloc //COMMIT 상태라야 VirtualProtect가 가능하다. char *pMemt = (char *)VirtualAlloc((LPVOID)dwAddr, dwAllocSize, MEM_RESERVE , PAGE_READWRITE);//준비 MemInfo((DWORD)dwAddr, &mbi, &dwMemSize); MemPtStatus( dwAddr, &dwMemProtectStatus); pMem = (char *)VirtualAlloc(pMem,dwAllocSize, MEM_COMMIT , PAGE_EXECUTE);//실제 할당 MemInfo((DWORD)dwAddr, &mbi, &dwMemSize); MemPtStatus( dwAddr, &dwMemProtectStatus); //==================================================================== //VirtualProtect //mbi.Protect 가 0 이 아니고, COMMIT 상태 라야 VirtualProtect 가 성공 // mbi.AllocationProtect 와는 관계없음 if( mbi.Protect && (mbi.State & MEM_COMMIT) ){ dwStatusIn = PAGE_EXECUTE_READWRITE;// if( VirtualProtect((LPVOID)dwAddr, SIZE, dwStatusIn, &dwOldStatus) ){ _DbgStr(_T("VirtualProtect-성공-(%X), (%X/%X)"), dwAddr, mbi.AllocationProtect, mbi.Protect ); MemInfo((DWORD)dwAddr, &mbi, &dwMemSize); }else{ _DbgStr(_T("VirtualProtect-에러-(%X), (%X/%X)%s"), dwAddr, mbi.AllocationProtect, mbi.Protect, DbgMsg() ); MemInfo((DWORD)dwAddr, &mbi, &dwMemSize); } }else{ _DbgStr(_T("VirtualProtect-준비가 안됨-(%X), (%X/%X)"), dwAddr, mbi.AllocationProtect, mbi.Protect ); MemInfo((DWORD)dwAddr, &mbi, &dwMemSize); } }
return 1;
}