i´m just allocating the momeory with new. i assumed it should be accessible afterwards without problems.
should i manually set the rights?
here is what i do:
Code:
/**
* Asm Stuff
*/
#pragma pack(1)
class Instruction
{
public:
char opcode;
};
#pragma pack()
#pragma pack(1)
class AsmJmp : public Instruction
{
public:
void Apply(unsigned long addr)
{
opcode = 0xE9;
relativeAddr = addr;
}
unsigned long relativeAddr;
};
#pragma pack()
void* CPatchDetour::applyDetour(void *pTarget, void *pHook)
{
size_t cpySize = 0;
char* pSrc = reinterpret_cast<char*>(pTarget);
pSrc = findSafeSpot(pSrc); // working as intended
m_pTarget = pSrc;
cpySize = getNeededSize(pSrc); // working as intended
m_Len = cpySize;
LOG_(log_debug4) << FKT << "Dumping SafeSpot before modification: " << std::endl
<< toAsmString(pSrc,cpySize) << std::endl;
// allocate mem and backup original instructions
m_pOriginal = std::tr1::shared_ptr<char>(new char[cpySize + size_jmp]);
memcpy(m_pOriginal.get(), pSrc , cpySize); // looking at log seems to write stuff as intended
LOG_(log_debug4) << FKT << "Dumping backed up instructions before modification: " << std::endl
<< toAsmString(m_pOriginal.get(),cpySize) << std::endl;
fixRelativeInstructions(m_pOriginal.get(), pSrc ,cpySize); // working as intended
// append jump back to Target Instruction
reinterpret_cast<AsmJmp*>( cpySize + m_pOriginal.get() )
->Apply( (DWORD)pSrc - (DWORD)(m_pOriginal.get() + size_jmp) ); // looking at log seems to also write what it´s supposed to do
LOG_(log_debug4) << FKT << "Dumping backed up instructions after fixing relative Instructions: " << std::endl
<< toAsmString(m_pOriginal.get(),cpySize) << std::endl;
setupHook(pSrc, cpySize, (char*)pHook); // also working
LOG_(log_debug4) << FKT << "Dumping Hooked Function " << std::endl
<< toAsmString(pSrc,cpySize) << std::endl;
return m_pOriginal.get();
}
void CPatchDetour::setupHook(char* pTarget, size_t size, char* pHook)
{
// write our Hook code to target function
VirtualProtectBlock tarProtect(pTarget, size, PAGE_EXECUTE_READWRITE );
// write push addr ret
reinterpret_cast<AsmPushRet*>(pTarget)
->Apply((unsigned long)pHook);
// nop the rest
for( size_t i = size_push_ret; i < size ; i++)
{ // nop the rest
reinterpret_cast<char*>(pTarget)[i] = 0x90; //nop
}
}
ugly, but doing the job, at least on my machine and i don´t want to rewrite till i know what´s wrong.