簡體   English   中英

C ++將stdcall函數名稱作為參數傳遞

[英]C++ Pass stdcall function name as parameter

我無法將stdcall函數名稱“ TestFunction”作為參數傳遞給ExecuteLocalThread並在beginthreadex中使用

unsigned __stdcall TestFunction(void* timerPointer)
{
    unsigned result =0;
    printf("thread is running\n");
    return result;
}

void ExecuteLocalThread(unsigned int (_stdcall *_StartAddress)(void *))
{
    HANDLE   heartBeatThread;
    unsigned int hbThreadID;
    heartBeatThread = (HANDLE)_beginthreadex(NULL, 0 , unsigned int (_stdcall *_StartAddress)(void *)/*&TestFunction*/, (void*)this, CREATE_SUSPENDED, &hbThreadID);
    ResumeThread( heartBeatThread );
}

嘗試:

heartBeatThread = (HANDLE)_beginthreadex(NULL, 0 , _StartAddress/*&TestFunction*/, (void*)this, CREATE_SUSPENDED, &hbThreadID);

我強烈建議為函數指針創建一個typedef,並在其他地方使用它:

typedef unsigned int _stdcall (*Pfn)(void*); // typedefs to "Pfn"

void ExecuteLocalThread(Pfn _StartAddress)
{
    HANDLE   heartBeatThread;
    unsigned int hbThreadID;
    heartBeatThread = (HANDLE)_beginthreadex(NULL, 0, _StartAddress, (void*)this, CREATE_SUSPENDED, &hbThreadID);
    ResumeThread( heartBeatThread );
}

它更容易鍵入,更易於閱讀,更難弄亂;)連轉換也變得很容易:將somePtr為函數指針類型: (Pfn)somePtr

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM