Helper Function



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;

}


반응형

'Code > Desktop' 카테고리의 다른 글

VisualStudio Projet 이름 변경 방법  (0) 2014.08.28
Cheat Engine 컴파일 방법  (0) 2014.06.29
cURL 사용법  (0) 2014.06.21
이더넷 패킷 구조  (0) 2014.06.17
Cpp Sound Library(C++)  (0) 2014.06.05
Posted by codens