2

我无法将 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 );
}
4

2 回答 2

1

尝试:

heartBeatThread = (HANDLE)_beginthreadex(NULL, 0 , _StartAddress/*&TestFunction*/, (void*)this, CREATE_SUSPENDED, &hbThreadID);
于 2013-01-10T11:53:16.567 回答
0

我强烈建议为函数指针创建一个 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

于 2013-01-10T12:02:57.630 回答