深入机器码...
#include "stdafx.h"
#include <windows.h>
#include <iostream>
using namespace std;
class A
{
public:
A();
virtual ~A();
virtual void Func();
int n;
};
A::A()
{
n = 0;
}
A::~A()
{
}
void A::Func()
{
n++;
}
class B : public A
{
public:
B();
virtual ~B();
virtual void Func();
};
B::B()
{
}
B::~B()
{
}
void B::Func()
{
n--;
}
int main(int argc, char* argv[])
{
A* pa = new B;
typedef void (A::*PFN)();
PFN pfn = A::Func;
BYTE byCode[] = {
0xb9, 0xcc, 0xcc, 0xcc, 0xcc, // mov ecx, #...
0xbe, 0xcc, 0xcc, 0xcc, 0xcc, // mov esi, #...
0xff, 0xd6, // call esi
0xc3
};
// write object pointer to byCode buffer
DWORD dw = (DWORD)pa;
byCode[1] = (dw >> 0x00) & 0xff;
byCode[2] = (dw >> 0x08) & 0xff;
byCode[3] = (dw >> 0x10) & 0xff;
byCode[4] = (dw >> 0x18) & 0xff;
// type cast "dw = (DWORD)(void*)pfn" ...
_asm
{
push eax
mov eax, pfn
mov dw, eax
pop eax
}
byCode[6] = (dw >> 0x00) & 0xff;
byCode[7] = (dw >> 0x08) & 0xff;
byCode[8] = (dw >> 0x10) & 0xff;
byCode[9] = (dw >> 0x18) & 0xff;
// don't modify next lines, it is called from out code
_asm
{
push eax
lea eax, byCode
call eax
pop eax
}
cout << pa->n << endl;
return 0;
}
问题点数:100、回复次数:3Top
1 楼vcsongs(vcsongs)回复于 2002-02-03 18:53:47 得分 0
如何保证pa, pfn绝对地址的不变? Top
2 楼yanchang(笨笨)回复于 2002-02-03 19:17:30 得分 100
不用指针成不成Top
3 楼vcsongs(vcsongs)回复于 2002-02-03 19:35:54 得分 0
? 哪里不用指针.
对象指针不用可以的.
比如:
A a;
...
_asm
{
lea max, a
mov ...
}
但函数指针不用不行吧...
Top




