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

Leo

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

統計

  • 隨筆 - 2
  • 文章 - 0
  • 評論 - 2
  • 引用 - 0

常用鏈接

留言簿(1)

隨筆檔案

搜索

  •  

最新評論

閱讀排行榜

評論排行榜

2007年5月16日

如何讓API回調你的VC類成員函數而不是靜態函數

 

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

。。。開個玩笑。以前確實大家都是這樣做的,在靜態的成員函數中再查找this指針,它多半是全局變量,或者是回調函數提供的附加參數。如果是前者,就會大大破壞程序的結構。而現在,隨著社會生產力的發展,偶們已經能做到將成員函數映射成為一個臨時的靜態函數了。本文就來演示一下這種實現方式。

首先需要包含一個由yzwykkldczsh同志編寫的模板類-----萬能多用自適應無限制回調模板(為紀念友人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 *********************************/

模板的三個參數分別是:API函數指針的類型,類名字,類成員函數指針的類型(兩種函數指針在參數和返回值上應該一樣,只是前者聲明為_stdcall,后者不加任何調用修飾,即默認的__thiscall方式)
該項頭文件的注釋中給了一個調用API函數EnumWindows的例子。現在偶們來試試調用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;
}

調用時,只要這樣寫:
/* 初始化回調結構 */
m_DOnTimer.Assign(this, &CTestCallback::OnTimer);
m_uid = ::SetTimer( NULL, 0, 1000, m_DOnTimer);

最后記得在CTestCallback的析構函數中KillTimer。由于m_DOnTimer會實現轉化到靜態函數指針類型的操作符,所以調用的地方只要直接寫回調結構的名字就可以了。

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

posted @ 2007-05-16 13:32 LeoChen 閱讀(1782) | 評論 (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) | 評論 (0)編輯 收藏
僅列出標題  
青青草原综合久久大伊人导航_色综合久久天天综合_日日噜噜夜夜狠狠久久丁香五月_热久久这里只有精品
  • <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∨| 免费在线观看一区二区| 久久国产精品色婷婷| 久久国产高清| 久热精品在线| 欧美国产91| 欧美视频免费在线| 国产精品免费一区二区三区观看| 国产精品女人网站| 国内在线观看一区二区三区| 国产欧美精品日韩| 伊人久久大香线蕉av超碰演员| 亚洲人成网站色ww在线| 亚洲永久视频| 久久精品视频在线看| 欧美肥婆在线| 亚洲综合日韩中文字幕v在线| 性欧美超级视频| 欧美二区在线观看| 国产美女搞久久| 亚洲激情欧美| 午夜精品视频| 亚洲激精日韩激精欧美精品| 亚洲一区二区精品| 久久中文在线| 国产精品丝袜白浆摸在线| 韩国福利一区| 亚洲天堂成人| 美女诱惑一区| 亚洲欧美日韩久久精品| 欧美高清视频免费观看| 国产日本亚洲高清| 国产精品99久久不卡二区| 久久一区二区三区四区五区| 日韩香蕉视频| 你懂的国产精品| 国产精品国产三级国产aⅴ入口 | 久久久国际精品| 欧美激情一区在线观看| 国外精品视频| 午夜精品一区二区三区电影天堂 | 欧美一区在线视频| 欧美日韩福利在线观看| 精品二区视频| 亚洲欧美一区二区在线观看| 亚洲激情视频网| 久久只有精品| 在线观看成人一级片| 欧美在线中文字幕| 亚洲小说春色综合另类电影| 欧美国产亚洲另类动漫| 国产最新精品精品你懂的| 国产精品99久久久久久人| 久久久久久久久久久久久久一区| 一区二区精品| 欧美色123| 99re热这里只有精品视频| 免费在线看成人av| 久久精品一本| 精品成人久久| 免费永久网站黄欧美| 久久免费国产精品1| 一区二区三区在线免费视频| 欧美一区影院| 欧美一级在线视频| 激情综合色综合久久| 媚黑女一区二区| 久久久另类综合| 在线免费观看日本一区| 欧美刺激性大交免费视频 | 国产老女人精品毛片久久| 一区二区三区.www| 99伊人成综合| 国产欧美一级| 美腿丝袜亚洲色图| 欧美国产成人精品| 亚洲婷婷综合色高清在线| 亚洲性图久久| 在线观看三级视频欧美| 欧美二区在线| 欧美午夜大胆人体| 亚洲女同同性videoxma| 香蕉久久a毛片| 亚洲国产裸拍裸体视频在线观看乱了中文 | 亚洲视频免费| 国产精品久久久久aaaa樱花| 亚洲在线成人| 久久精品成人一区二区三区蜜臀| 精品99视频| 亚洲精品视频啊美女在线直播| 国产精品成人免费视频| 久久亚洲精品欧美| 欧美日韩免费一区二区三区| 羞羞答答国产精品www一本| 久久精品国产2020观看福利| 日韩一级精品| 午夜欧美视频| 日韩一级精品视频在线观看| 午夜精品视频在线观看| 亚洲精品久久视频| 午夜精品久久久久| 亚洲欧洲精品成人久久奇米网 | 欧美成在线视频| 国产精品视频不卡| 免费精品视频| 国产精品免费看片| 亚洲激情偷拍| 黑人巨大精品欧美黑白配亚洲| 日韩亚洲欧美一区二区三区| 国产一级揄自揄精品视频| 最新日韩欧美| 在线观看视频一区| 亚洲一区二区三区乱码aⅴ蜜桃女| 91久久国产自产拍夜夜嗨| 亚洲欧美国产另类| 一区二区三区视频观看| 美女网站在线免费欧美精品| 欧美专区在线观看| 欧美性色综合| 91久久精品一区二区三区| 在线观看欧美日韩| 欧美一区二区三区在线播放| 亚洲制服少妇| 欧美日韩在线三级| 亚洲精品一区中文| 99国产精品视频免费观看| 久久经典综合| 久久五月天婷婷| 国产区精品在线观看| 一区电影在线观看| 亚洲一区二区三区国产| 欧美日韩精品综合在线| 亚洲激情在线观看视频免费| 亚洲第一区在线观看| 久久亚洲捆绑美女| 欧美国产精品久久| 亚洲高清二区| 欧美大片免费观看| 欧美激情亚洲另类| 亚洲精品一区中文| 老司机久久99久久精品播放免费| 欧美粗暴jizz性欧美20| 亚洲国产欧洲综合997久久| 久久这里有精品15一区二区三区| 快she精品国产999| 亚洲国产成人精品视频| 欧美国产另类| 亚洲视频你懂的| 亚洲韩国青草视频| 正在播放欧美一区| 欧美福利一区二区三区| 亚洲三级免费| 亚洲性人人天天夜夜摸| 国产精品中文字幕欧美| 久久精品九九| 亚洲第一久久影院| 在线视频精品一区| 国产麻豆午夜三级精品| 久热精品视频在线观看| 亚洲人午夜精品| 欧美亚洲视频| 在线观看视频一区| 欧美日韩国产限制| 亚洲免费影视第一页| 免费在线看一区| 亚洲欧美精品在线观看| 永久91嫩草亚洲精品人人| 欧美成人午夜激情| 亚洲综合视频一区| 欧美成人一区二区三区片免费| 一本综合久久| 激情欧美一区二区三区| 欧美国产一区在线| 亚洲欧美韩国| 欧美激情视频在线播放 | 亚洲区一区二区三区| 午夜精品久久久久久久男人的天堂 | 日韩亚洲欧美成人| 国产精品日韩在线播放| 久久久久国产精品人| 日韩特黄影片| 理论片一区二区在线| 亚洲一级一区| 黄色在线一区| 国产精品免费一区二区三区观看| 嫩草伊人久久精品少妇av杨幂| 午夜精品久久| 亚洲精品之草原avav久久| 久久综合色综合88| 性久久久久久久| 中日韩高清电影网| 一区二区在线免费观看| 国产精品一区一区| 欧美日韩在线一区二区| 欧美福利视频网站| 久久免费视频在线观看| 亚洲欧美激情一区| 亚洲影视综合|