这一篇文章中主要是讲解怎么写代码把以上3篇文章的理论用起来,写出一个能用的Thread类(只是模型)
我在第3篇中提到了,要想把类的成员函数做Thread函数,一定要在一个时候把this指针赋值给ecx,问题是在类的成员函数里面是不太可能做这种事情的,因为如果你要让fun成为线程函数,你就需要在别的函数里面初始化ecx,这样感觉有点多余。我用的最简单的一个方法是写一段机器代码,在这个机器代码里面完成所有的工作:初始化ecx,跳转到fun函数………至于那段机器代码怎么来的我想,我们用Vs调试的时候有个显示机器代码的选项,可以用这个看到吧,呵呵。不说了,下面直接放代码。
//core.h
#ifndef __ZX_CORE_H__
#define __ZX_CORE_H__
#include <windows.h>
#ifndef interface
#define interface struct
#endif
#ifndef implement
#define implement :public
#endif
const static unsigned char g_thread_proc[]=
{
//------------parameter-----------------
0x8B,0x44,0x24,0x04, // mov eax,dword ptr [esp+10h]
0x50, // push eax
//-----------this pointer-------------
0xB9,0x00,0x00,0x00,0x00, // mov ecx,0x12FF5C
//-----------call back function-------------
0xB8,0x00,0x00,0x00,0x00, // mov eax,0
0xFF,0xD0, // call eax
//return
0xC2,0x10,0x00 // ret 10h
};
#endif
//runnable.h
#ifndef __ZX_RUNNABLE_H__
#define __ZX_RUNNABLE_H__
#include "core.h"
interface ZXRunnable
{
virtual void run(void* lpParameter)= 0;
};
#endif
//thread.h
#ifndef __ZX_THREAD_H__
#define __ZX_THREAD_H__
#include "core.h"
#include "runnable.h"
class ZXThread
{
public:
ZXThread();
ZXThread(ZXRunnable* runnable);
virtual ~ZXThread();
public:
void Start();
void Wait();
void SetRunnable(ZXRunnable* runnable);
ZXRunnable* GetRunnable();
private:
ZXRunnable* m_pRunnable;
HANDLE m_hThread;
unsigned char m_thread_proc[sizeof(g_thread_proc)];
};
#endif
//thread.cpp
#include "thread.h"
ZXThread::ZXThread(): m_pRunnable(NULL), m_hThread(NULL) { }
ZXThread::ZXThread(ZXRunnable* runnable): m_pRunnable(runnable), m_hThread(NULL){}
ZXThread::~ZXThread()
{
delete m_pRunnable;
}
void ZXThread::SetRunnable(ZXRunnable* runnable)
{
m_pRunnable= runnable;
}
ZXRunnable* ZXThread::GetRunnable()
{
return(m_pRunnable);
}
void ZXThread::Start()
{
CopyMemory(m_thread_proc, g_thread_proc, sizeof(g_thread_proc));
*(int*)(&m_thread_proc[6])= (int)m_pRunnable;
void (ZXRunnable::*func)(void* lpParameter)= &ZXRunnable::run;
int addr;
__asm
{
mov eax, func
mov addr, eax
}
*(int*)(&m_thread_proc[11])= addr;
m_hThread= ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)(void*)m_thread_proc,
NULL, 0, NULL);
}
void ZXThread::Wait()
{
::WaitForSingleObject(m_hThread, INFINITE);
}
以下就是测试用例了哦:
#include <iostream>
#include "thread.h"
using namespace std;
class ZXRun implement ZXRunnable
{
public:
virtual void run(void* lpParameter)
{
cout<<"Hello,World!"<<endl;
}
};
int main()
{
ZXThread boss(new ZXRun);
boss.Start();
boss.Wait();
}
注意,这个代码可能会崩溃,因为我们让数据当指令执行,因此需要关闭DEP,关闭DEP的方法参见网址:http://www.baidu.com/s?ie=utf-8&bs=VS%E5%85%B3%E9%97%ADDEP&f=8&rsv_bp=1&rsv_spt=3&wd=Visual+Studio%E5%85%B3%E9%97%ADDEP&inputT=2696
其步骤也比较简单:打开工程属性(不是解决方案属性哦)->Linker->Advanced->Data execution prevention(后面选择NO)。
至此,全部过程完成。