可执行内存的分配

//ExeMemory.h #ifndef _EXE_MEM_H_ #define _EXE_MEM_H_ #include #if defined(WIN32) && defined(_MSC_VER) #include #else #include #endif typedef int (*ExeMemoyFun)(); typedef unsigned int size_t; //#define EXE_MEMORY_DEBUG class ExeMemory { public: ExeMemory() { m_pCode=NULL; m_size=0; } ExeMemory(size_t size) { NewMem(size); } ~ExeMemory() { DelMem(); } void NewMem(size_t size) { DelMem(); //size=4096; //4字节对齐 //unsigned int mv=size%sizeof(void*); //if (mv!=0) size+=sizeof(void*)-mv; #if defined(WIN32) && defined(_MSC_VER) m_pCode=VirtualAlloc(NULL,size,MEM_COMMIT|MEM_RESERVE,PAGE_EXECUTE_READWRITE); MEMORY_BASIC_INFORMATION t; DWORD dwResult=VirtualQuery(m_pCode,&t,sizeof(MEMORY_BASIC_INFORMATION)); /* if (dwResult) { TRACE("dwResult:%d/n",dwResult); TRACE("BaseAddress:%d/n",t.BaseAddress); TRACE("AllocationBase:%d/n",t.AllocationBase); TRACE("AllocationProtect:%d/n",t.AllocationProtect); TRACE("RegionSize:%d/n",t.RegionSize); TRACE("State:%d/n",t.State); TRACE("Protect:%d/n",t.Protect); TRACE("Type:%d/n",t.Type); } */ #else m_pCode = mmap(0,size,PROT_READ|PROT_WRITE|PROT_EXEC,MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); #endif if (m_pCode) { m_size=size; #if defined(WIN32) && defined(_MSC_VER) ::FlushInstructionCache(::GetCurrentProcess(),m_pCode,m_size); #endif } } void DelMem() { if (m_pCode!=NULL) { #if defined(WIN32) && defined(_MSC_VER) VirtualFree(m_pCode,NULL,MEM_RELEASE); //在删除的时候,请勿带内存大小参数,否则兼容工具会检测到不兼容特性 #else munmap(m_pCode, m_size); #endif m_size=0; m_pCode=NULL; } } void zero(unsigned char c) { memset(m_pCode, c, m_size); } size_t Size() { return m_size; } operator unsigned int() { return (unsigned int)m_pCode; } operator bool() { return (m_pCode!=NULL); } operator void*() const { return m_pCode; } operator unsigned char*() const { return (unsigned char *)m_pCode; } operator char*() const { return (char *)m_pCode; } void CallSub(size_t offset=0) { ExeMemoyFun fun=(ExeMemoyFun)((unsigned char *)m_pCode+offset); fun(); } int CallFun(size_t offset=0) { ExeMemoyFun fun=(ExeMemoyFun)((unsigned char *)m_pCode+offset); return fun(); } void SetValue(unsigned int template_val, unsigned int val, unsigned char op=0xB8) { if ( m_pCode==NULL || template_val==0 ) return; unsigned char *ptr=(unsigned char *)m_pCode; for (unsigned int i=0;i

你可能感兴趣的:(VC++,内联汇编)