青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品

Leo

<2025年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

統(tǒng)計(jì)

  • 隨筆 - 2
  • 文章 - 0
  • 評(píng)論 - 2
  • 引用 - 0

常用鏈接

留言簿(1)

隨筆檔案

搜索

  •  

最新評(píng)論

閱讀排行榜

評(píng)論排行榜

2007年5月16日

如何讓API回調(diào)你的VC類成員函數(shù)而不是靜態(tài)函數(shù)

 

只要在函數(shù)聲明前加static就好了,哈哈哈哈哈~~~~~  

。。。開個(gè)玩笑。以前確實(shí)大家都是這樣做的,在靜態(tài)的成員函數(shù)中再查找this指針,它多半是全局變量,或者是回調(diào)函數(shù)提供的附加參數(shù)。如果是前者,就會(huì)大大破壞程序的結(jié)構(gòu)。而現(xiàn)在,隨著社會(huì)生產(chǎn)力的發(fā)展,偶們已經(jīng)能做到將成員函數(shù)映射成為一個(gè)臨時(shí)的靜態(tài)函數(shù)了。本文就來演示一下這種實(shí)現(xiàn)方式。

首先需要包含一個(gè)由yzwykkldczsh同志編寫的模板類-----萬能多用自適應(yīng)無限制回調(diào)模板(為紀(jì)念友人fishskin,此模板又稱為H>W模板) 

/**************************************************************************
 *   ACCallback.h
 *   Helper class of Member function callback mechanism
 **************************************************************************/
#include "stdafx.h"
#include "windows.h"

#pragma pack(push, 1)
struct _ACCallbackOpCodes
{
 unsigned char tag;  // CALL e8
 LONG_PTR offset;  // offset (dest - src - 5, 5=sizeof(tag + offset))
 LONG_PTR _this;   // a this pointer
 LONG_PTR _func;   // pointer to real member function address
};
#pragma pack(pop)

static __declspec( naked ) int STDACJMPProc()
{
 _asm
 {
  POP ECX 
  MOV EAX, DWORD PTR [ECX + 4] // func
  MOV ECX, [ECX]     // this  
  JMP EAX
 }
}

static LONG_PTR CalcJmpOffset(LONG_PTR Src, LONG_PTR Dest)
{
 return Dest - (Src + 5);
}

/*
 * NOTE: _TPStdFunc: a type of function pointer to API or Callbacks, *MUST* be _stdcall
         _TPMemberFunc: a type of function pointer to class member function,
         *MUST* be the *DEFAULT* calling conversation, *NO* prefix should be added,
          that is, using ECX for "this" pointer, pushing parameters from right to left,
          and the callee cleans the stack.
          _TClass: the class who owns the callback function. The caller should only own the _stdcall function pointer
   LIFE TIME:  It is important to keep the ACCallback object alive until the CALLBACK is not required!!!
 */
template<typename _TPStdFunc, class _TClass, typename _TPMemberFunc>
class ACCallback
{
public:
 _TClass *m_pThis;
 _TPMemberFunc m_pFunc;

private:
 _TPStdFunc m_pStdFunc;

 void MakeCode()
 {
  if (m_pStdFunc) ::VirtualFree(m_pStdFunc, 0, MEM_RELEASE);
  m_pStdFunc = (_TPStdFunc)::VirtualAlloc(NULL, sizeof(_ACCallbackOpCodes), MEM_COMMIT, PAGE_EXECUTE_READWRITE);
  _ACCallbackOpCodes *p = (_ACCallbackOpCodes *)m_pStdFunc;
  p->_func = *(LONG_PTR *)&m_pFunc;
  p->_this = (LONG_PTR)m_pThis;
  p->tag = 0xE8;
  p->offset = CalcJmpOffset((LONG_PTR)p, (LONG_PTR)STDACJMPProc);
 }

public:
 ACCallback<_TPStdFunc, _TClass, _TPMemberFunc>()
 {
 }
 ACCallback<_TPStdFunc, _TClass, _TPMemberFunc>(_TClass* pThis,
  _TPMemberFunc pFunc
  )
 {
  m_pFunc = pFunc;
  m_pThis = pThis;
  m_pStdFunc = NULL;
  MakeCode();
 }
 void Assign(_TClass* pThis,
  _TPMemberFunc pFunc
  )
 {
  m_pFunc = pFunc;
  m_pThis = pThis;
  m_pStdFunc = NULL;
  MakeCode();
 }
 ~ACCallback<_TPStdFunc, _TClass, _TPMemberFunc>()
 {
  ::VirtualFree(m_pStdFunc, 0, MEM_RELEASE);
 }
 operator _TPStdFunc()
 {
  return m_pStdFunc;
 }
};

/********************************** EXAMPLE **********************************
class CClass1
{
public:
 TCHAR m_Buf[255];
 BOOL EnumWindowProc(HWND hwnd, LPARAM lp)
 {
  GetWindowText(hwnd, m_Buf, 255);
  printf("Enum window=%s\n", m_Buf);
  return TRUE;
 }
 typedef BOOL (CClass1::*CLASSWNDENUMPROC)(HWND, LPARAM);
};

TO USE:
 CClass1 c1;
 ACCallback<WNDENUMPROC, CClass1, CClass1::CLASSWNDENUMPROC> cb(&c1, &CClass1::EnumWindowProc);
 EnumWindows(cb, 0);

************************* END OF EXAMPLE *********************************/

模板的三個(gè)參數(shù)分別是:API函數(shù)指針的類型,類名字,類成員函數(shù)指針的類型(兩種函數(shù)指針在參數(shù)和返回值上應(yīng)該一樣,只是前者聲明為_stdcall,后者不加任何調(diào)用修飾,即默認(rèn)的__thiscall方式)
該項(xiàng)頭文件的注釋中給了一個(gè)調(diào)用API函數(shù)EnumWindows的例子。現(xiàn)在偶們來試試調(diào)用SetTimer。

class CTestCallback
{
private:
 /* A callback of SetTimer, mirrored into member OnTimer */
 typedef void (CTestCallback::*CLASSTIMERPROC)(HWND, UINT, UINT_PTR, DWORD);
 void OnTimer (HWND hwnd, UINT uMsg, UINT_PTR idEvent, DWORD dwTime);
 ACCallback<TIMERPROC, CTestCallback, CLASSTIMERPROC> m_DOnTimer;
}

調(diào)用時(shí),只要這樣寫:
/* 初始化回調(diào)結(jié)構(gòu) */
m_DOnTimer.Assign(this, &CTestCallback::OnTimer);
m_uid = ::SetTimer( NULL, 0, 1000, m_DOnTimer);

最后記得在CTestCallback的析構(gòu)函數(shù)中KillTimer。由于m_DOnTimer會(huì)實(shí)現(xiàn)轉(zhuǎn)化到靜態(tài)函數(shù)指針類型的操作符,所以調(diào)用的地方只要直接寫回調(diào)結(jié)構(gòu)的名字就可以了。

使用該模板需要注意兩點(diǎn):
1.API函數(shù)應(yīng)當(dāng)是_stdcall類型的(這一點(diǎn)絕大部分API都滿足)。類成員函數(shù)必須是默認(rèn)的調(diào)用方式,不要加_stdcall或_cdecl之類的修飾。此方式的重要條件就在于_stdcall和__thiscall之間只相差了一個(gè)ECX指出的this指針,所以我們才能實(shí)現(xiàn)這種映射(這種方式在VCL和ATL的窗口類中都有使用到);
2.回調(diào)結(jié)構(gòu)的生存周期應(yīng)當(dāng)是在整個(gè)回調(diào)函數(shù)有效的時(shí)間內(nèi)。因此,對(duì)于EnumWindows這樣的函數(shù),只要聲明在棧上就可以了;但對(duì)于SetTimer,就必須定義為類成員變量,同時(shí),在類的析構(gòu)函數(shù)中必須及時(shí)銷毀這個(gè)timer。

posted @ 2007-05-16 13:32 LeoChen 閱讀(1782) | 評(píng)論 (2)編輯 收藏
How to use SetTimer() with callback to a non-static member function

Quick update...

After reviewing the comments and suggestions from a few people, I made the solution better. Look for an update to this article which uses a better approach, namely using the functions:

  • CreateWaitableTimer()
  • SetWaitableTimer()
  • WaitForMultipleObjects()

The solution based on these functions will allow multiple instances of the CSleeperThread class to run (instead of just one using the current example). So stay tuned, I'll have this article updated as soon as possible. :-)

Introduction

I have seen many questions on the boards about how to properly use SetTimer(). I've also noticed that most of these questions are around how to put a thread to sleep for X seconds. One obvious answer would be to use the Sleep() function. The main drawback is, how do you gracefully shut down your thread, or cancel the Sleep() operation before the time expires.

This article is meant to address all of the above. I give an example of putting a thread to sleep using SetTimer(). The SetTimer() calls back to a non-static function. This is key, because normally you have to pass a static member to SetTimer() which means it can't access any other non-static variables or member functions of the class.

Details

Since implementing a non-static callback member is key to this, we'll go into this first. Implementing a callback to a static member function doesn't require anything different from implementing a regular C callback function. Since static member functions have the same signature as C functions with the same calling conventions, they can be referenced using just the function name.

Making a non-static callback member function is a different story, because they have a different signature than a C function. To make a non-static member function, it requires the use of two additional items:

  • A global (void*) pointer, referencing the class of the callback function
  • A wrapper function which will be passed to SetTimer()

This is actually a fairly simple implementation. First, you need to define your class:

class CSleeperThread : public CWinThread {
public:
static VOID CALLBACK TimerProc_Wrapper( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD dwTime );
VOID CALLBACK TimerProc( HWND hwnd,
UINT uMsg, UINT idEvent, DWORD dwTime );
void ThreadMain();
void WakeUp();
private:
static void * pObject;
UINT_PTR pTimer;
CRITICAL_SECTION lock;
};

Then, don't forget to include the following line in your class implementation file:

void * CSleeperThread::pObject;

Now that we have our class declared, we can look at the wrapper function, the non-static member function and the member function that will call SetTimer():

VOID CALLBACK CSleeperThread::TimerProc_Wrapper( HWND hwnd, UINT uMsg,
UINT idEvent, DWORD dwTime ) {
CSleeperThread *pSomeClass = (CSleeperThread*)pObject; // cast the void pointer
pSomeClass->TimerProc(hwnd, uMsg, idEvent, dwTime); // call non-static function
}

The wrapper function first initializes a CSleeperThread pointer with pObject. Since pSomeClass is a local pointer, we can access it within the static wrapper function.

VOID CALLBACK CSleeperThread::TimerProc(HWND hwnd,
UINT uMsg, UINT idEvent, DWORD dwTime) {
::EnterCriticalSection(&lock);
if(idEvent == pTimer) {
KillTimer(NULL, pTimer);  // kill the timer so it won't fire again
ResumeThread();  // resume the main thread function
}
::LeaveCriticalSection(&lock);
}

The TimerProc member function isn't static, so we can access other non-static functions like ResumeThread() and we can access the private variable lock. Notice that I've entered a critical section which prevents a second timer event to enter the callback, thus ensuring that the first execution of TimerProc() will cancel out the timer.

Next, let's take a look at the main execution function, ThreadMain().

void CSleeperThread::ThreadMain()
{
pObject = this; // VERY IMPORTANT, must be initialized before
// calling SetTimer()
// call SetTimer, passing the wrapper function as the callback
pTimer = SetTimer(NULL, NULL, 10000, TimerProc_Wrapper);
// suspend until the timer expires
SuspendThread();
// the timer has expired, continue processing 
}

The first step in ThreadMain() is absolutely critical. We need to assign the class instance pointer (this) to the pObject variable. This is how the wrapper callback function will gain access to execute the non-static member function.

Next, we just call SetTimer() passing in a function pointer to our wrapper function. SetTimer() will call the wrapper function when the timer expires. The wrapper function in turn, will execute the non-static function TimerProc(), by accessing the static variable pSomeClass.

NOTE: I chose to implement a main function that will create the timer, go to sleep, continue processing and then exit when finished. This is in effect a function that will only execute once per timer. You could easily add a loop to ThreadMain() which would execute once for each timer event.

One last little function. Since we used SuspendThread() in ThreadMain(), if we need to wake up the thread (for whatever reason), all we have to do is make a call to ResumeThread(). So, I've added an access function like so:

void WakeUp() {
::EnterCriticalSection(&lock);
KillTimer(NULL, pTimer);
ResumeThread(); // wake the thread up
}

Buh dee buh dee, that's all folks...

And there we have it. A thread safe class that goes to sleep using SetTimer() and a non-static callback function; which also has the ability to wake up before the timer expires.

Hopefully, you have found this helpful. I've actually used this code in a project I'm working on now, and was in hopes someone else would get some good use out of it.

Someone once told me "you'll like programming if you like banging your head against the wall repeatedly". I've found that to be true, it took me literally several days to figure out what I've put into this article, I'm just slow I guess.

Whew, my head hurts, time for some Advil...or Ibooprofin.. or asssprin.... or something.

Credits...

I probably learned way more in the process of writing this article. So, much thanks goes to Lars Haendel for creating a web-site dedicated to understanding function pointers, without which I wouldn't know didley.

www.function-pointer.org.

posted @ 2007-05-16 10:31 LeoChen 閱讀(1658) | 評(píng)論 (0)編輯 收藏
僅列出標(biāo)題  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            久久久亚洲一区| 欧美日韩精品一区视频| 久久国产精品久久久久久久久久 | 久久精品国产亚洲a| 国产一二三精品| 亚洲国产综合视频在线观看| 亚洲视频在线观看| 久热国产精品视频| 一区二区三区四区五区在线| 久久精品国产一区二区电影 | 中文网丁香综合网| 国内成人自拍视频| 亚洲午夜久久久久久久久电影院| 久久欧美中文字幕| 欧美国产先锋| 国产综合第一页| 亚洲国产精品久久久久秋霞影院 | 嫩模写真一区二区三区三州| 日韩视频免费在线| 奶水喷射视频一区| 国产一在线精品一区在线观看| 欧美大色视频| 久久夜色精品国产| 韩日成人在线| 久久精品亚洲一区二区| 久久综合一区二区三区| 欧美激情视频给我| 99re热这里只有精品视频| 欧美xx视频| 国产精品亚洲аv天堂网| 亚洲视频成人| 免费成年人欧美视频| 亚洲激情在线观看视频免费| 亚洲一区一卡| 国产精品二区影院| 亚洲视频综合| 裸体一区二区| 久久久久久久久一区二区| 欧美日韩一区二区三区在线看| 日韩视频一区二区| 亚洲精品永久免费精品| 欧美精品亚洲一区二区在线播放| 久久久国产91| 国产日产精品一区二区三区四区的观看方式 | 国产日韩欧美一区| 久久久国产午夜精品| 欧美色图天堂网| 欧美在线在线| 久久精品国产免费观看| 午夜精品一区二区三区四区| 亚洲综合成人婷婷小说| 国产亚洲欧美中文| 亚洲欧美电影院| 午夜视频精品| 国产精品网站一区| 亚洲一区二区毛片| 午夜精品一区二区三区在线 | 欧美中文日韩| 亚洲精品一区在线观看| 麻豆精品在线观看| 欧美大片在线看| 亚洲人成网在线播放| 亚洲午夜精品视频| 永久免费毛片在线播放不卡| 亚洲国产精品一区二区www在线| 亚洲电影下载| 一区二区三区黄色| 悠悠资源网亚洲青| 久久久天天操| 欧美一区国产一区| 欧美国产日韩精品免费观看| 亚洲国产成人porn| 国产日韩在线一区二区三区| 欧美一区1区三区3区公司| 日韩视频在线观看国产| 欧美日产一区二区三区在线观看| 日韩一区二区高清| 性色av一区二区怡红| 欧美黄污视频| 亚洲视频免费在线| 久久精品国产亚洲aⅴ| 在线观看精品一区| 欧美精品一区视频| 欧美黄色片免费观看| 国产精品午夜久久| 久久久久久久久久久一区| 亚洲二区免费| 亚洲欧洲av一区二区| 激情校园亚洲| 亚洲欧美文学| 亚洲欧美国产视频| 欧美日韩国产精品成人| 亚洲欧美电影在线观看| 久色婷婷小香蕉久久| 在线一区二区三区四区五区| 老司机免费视频久久| 久久躁日日躁aaaaxxxx| 国产精品久久毛片a| 99热这里只有精品8| 日韩写真在线| 国产三级精品在线不卡| 欧美国产日韩免费| 欧美一区二区在线观看| 亚洲国产欧美不卡在线观看| 欧美一级电影久久| 亚洲精品日韩激情在线电影| 免费日韩成人| 午夜精品影院在线观看| 久久精品一区| 国产精品99久久久久久宅男| 尤物yw午夜国产精品视频| 国产精品久久久久9999| 欧美成ee人免费视频| 亚洲第一页自拍| 久久爱www| 亚洲一区在线直播| 国产欧美一区二区在线观看| 欧美巨乳波霸| 欧美a级片网| 久久精品72免费观看| 亚洲尤物视频网| 99视频精品全部免费在线| 欧美激情亚洲国产| 免费av成人在线| 亚洲精品午夜精品| 在线国产亚洲欧美| 国产欧美婷婷中文| 国产精品久久久久久久久久直播 | 性8sex亚洲区入口| 亚洲综合视频一区| 制服丝袜激情欧洲亚洲| 亚洲久色影视| 亚洲激情视频在线观看| 在线成人av网站| 在线国产亚洲欧美| 亚洲高清视频一区二区| 欧美日韩国产欧美日美国产精品| 美女成人午夜| 免费亚洲网站| 欧美激情综合五月色丁香小说| 女人香蕉久久**毛片精品| 麻豆精品网站| 欧美精品二区三区四区免费看视频| 久久综合狠狠综合久久激情| 久热成人在线视频| 美女露胸一区二区三区| 欧美成人精品三级在线观看| 欧美激情aⅴ一区二区三区| 乱码第一页成人| 欧美成人一区二区| 欧美日韩另类综合| 国产精品久久久久秋霞鲁丝 | 国内伊人久久久久久网站视频| 国产欧美日韩伦理| 狠狠色综合一区二区| 在线精品高清中文字幕| 91久久精品美女高潮| 在线视频精品一| 欧美在线不卡| 欧美大香线蕉线伊人久久国产精品| 亚洲国产黄色片| 中文高清一区| 久久久久九九九| 欧美久久电影| 国产亚洲精品久久久久动| 黑人巨大精品欧美黑白配亚洲| 亚洲国产成人av好男人在线观看| 日韩视频久久| 久久国产精品72免费观看| 男人天堂欧美日韩| 一区二区欧美在线观看| 欧美在线视频免费播放| 欧美成人免费网| 国产九九精品视频| 国产精品进线69影院| 樱花yy私人影院亚洲| 国产精品99久久久久久久久久久久| 午夜在线成人av| 亚洲国产激情| 久久成人综合视频| 欧美三级在线| 亚洲国产精品久久久久婷婷老年 | 亚洲婷婷综合久久一本伊一区| 久久精品中文| 国产精品jvid在线观看蜜臀| 欧美—级在线免费片| 国产视频一区免费看| 亚洲毛片在线看| 久久久亚洲国产天美传媒修理工| 日韩亚洲欧美一区| 久久只精品国产| 国产日本欧洲亚洲| av成人黄色| 亚洲欧美成人一区二区在线电影 | 久久精品人人| 一本一本久久a久久精品综合麻豆 一本一本久久a久久精品牛牛影视 | 欧美一区91| 国产精品日本欧美一区二区三区| 亚洲精选在线观看| 欧美成人国产|