class中封装回调函数 显式传递this指针

本文介绍了一种在C++中使用CreateThread创建线程的方法,并通过传递成员函数指针实现线程运行。此外,还探讨了在DLL中如何通过显式传递this指针来调用类的成员函数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文是用来凑数而已。因为本人实在讨厌某个数字,巧的是,本人在优快云博客上发表的文章数量正好是某个数字,于是加一篇。

 

1.最简单的就是CreateThread

#if !defined(AFX_TTHREAD_H__7222F1CA_7289_41A3_98A3_431B6044B3AE__INCLUDED_)
#define AFX_TTHREAD_H__7222F1CA_7289_41A3_98A3_431B6044B3AE__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif

class TThread  
{
public:
	void start();
	TThread();
	virtual ~TThread();

private:
	unsigned long _stdcall thread_test();

	int m_private_data;
};

#endif

#include "stdafx.h"
#include "TThread.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

TThread::TThread(){}
TThread::~TThread(){}

unsigned long _stdcall TThread::thread_test()
{
	m_private_data=123; return 0;
}

void TThread::start()
{
	typedef unsigned long (_stdcall TThread::*FUNC_POINT)();
	FUNC_POINT FuncPoint=(FUNC_POINT)&TThread::thread_test;

	::CreateThread(NULL,0,*(LPTHREAD_START_ROUTINE*)&FuncPoint,this,0,NULL);
}


这样,回调函数就可以如同类的普通成员函数那样,可以直接访问类的数据和其他成员函数。

 

2.在DLL中回调类的成员函数

假如有人写了个DLL,这个DLL可能被外部程序,比如C,C++或者其他语言使用,那么这个DLL该怎么编写?导出一个类?那么,C语言要想使用这个DLL恐怕会是一件麻烦的事情。

这种情况,DLL最好提供全局函数式接口。

再多考虑一些。假如这个DLL需要调用上层应用怎么办?

答案是,只能使用回调函数。

问题是,回调函数一般都是全局函数,C++程序中出现全局函数总不是一件很雅观的事情。所以,这里提供一种稍微变通一点的办法---显式传递this指针。

 

假设DLL导出了这么一个函数,而且DLL还在此导出函数中回调了类的某个成员函数,如下:

void dll_export_func(unsigned long func_addr,unsigned long class_this)
{
	_asm push	1
	_asm push	2
	_asm mov	ecx,class_this
	_asm call	func_addr
}


类的成员如何设计呢?很简单,形式跟前面说得差不多,如下(类的头文件就省略了):

TClass::TClass(){}
TClass::~TClass(){}

void TClass::init()
{
	typedef void (TClass::*FUNC_POINT)();
	FUNC_POINT FuncPoint=(FUNC_POINT)&TClass::dll_callback;
	::dll_export_func(*(unsigned long*)&FuncPoint,(unsigned long)this);
}

void TClass::dll_callback(int a,int b)
{
	printf("dll call my member func,pass param %d and %d\n",a,b);
}

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值