Teach you to use Windows APIs to write a Thread class (do not use static) -- (4)

Source: Internet
Author: User

This article mainly explains how to write code and use the theory of the above three articles to write a usable Thread class (just a model)

As I mentioned in Article 3rd, to make member functions of the Class A thread function, you must assign this pointer to ECx at one time, the problem is that it is unlikely to do this in the member functions of the class, because if you want to make fun a thread function, you need to initialize ECx in other functions, this is a bit redundant. One of the simplest ways I use is to write a piece of machine code and complete all the work in this machine code: Initialize ECx and jump to the fun function ......... As for how the machine code came from, I think we have an option to display the machine code when debugging with vs. You can use this option to see it. If you don't want to talk about it, put the code below.

 

//core.h#ifndef __ZX_CORE_H__#define __ZX_CORE_H__#include <windows.h>#ifndef interface#define interface struct#endif#ifndef implement#define implement :public#endifconst static unsigned charg_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//return0xC2,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;HANDLEm_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, funcmov 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);}

 

Here is the test case:

#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();}

 

Note that this Code may crash, because we have the data to be executed when the command is executed, so we need to close DEP, the way to close Dep see: 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

The steps are also relatively simple: Open Project Properties (not Solution Properties)-> linker-> advanced-> Data Execution Prevention (select no later ).

 

So far, the entire process has been completed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.