Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在DLL中创建线程 我正在用.C++编写一个.NET剖析器(使用ATL的DLL)。我想创建一个线程,每30秒写入一个文件。我希望线程函数是我的一个类的方法 DWORD WINAPI CProfiler::MyThreadFunction( void* pContext ) { //Instructions that manipulate attributes from my class; }_C++_Dll_Thread Sleep - Fatal编程技术网

在DLL中创建线程 我正在用.C++编写一个.NET剖析器(使用ATL的DLL)。我想创建一个线程,每30秒写入一个文件。我希望线程函数是我的一个类的方法 DWORD WINAPI CProfiler::MyThreadFunction( void* pContext ) { //Instructions that manipulate attributes from my class; }

在DLL中创建线程 我正在用.C++编写一个.NET剖析器(使用ATL的DLL)。我想创建一个线程,每30秒写入一个文件。我希望线程函数是我的一个类的方法 DWORD WINAPI CProfiler::MyThreadFunction( void* pContext ) { //Instructions that manipulate attributes from my class; },c++,dll,thread-sleep,C++,Dll,Thread Sleep,当我尝试启动线程时 HANDLE l_handle = CreateThread( NULL, 0, MyThreadFunction, NULL, 0L, NULL ); 我得到了这个错误: argument of type "DWORD (__stdcall CProfiler::*)(void *pContext)" is incompatible with parameter of type "LPTHREAD_START_ROUTINE" 如何在DLL中正确创建线程? 非常感谢您

当我尝试启动线程时

HANDLE l_handle = CreateThread( NULL, 0, MyThreadFunction, NULL, 0L, NULL );
我得到了这个错误:

argument of type "DWORD (__stdcall CProfiler::*)(void *pContext)" 
is incompatible with parameter of type "LPTHREAD_START_ROUTINE"
如何在DLL中正确创建线程?
非常感谢您的帮助。

您不能像传递常规函数指针一样传递成员函数的指针。您需要将成员函数声明为静态函数。如果需要对对象调用成员函数,可以使用代理函数

struct Foo
{
    virtual int Function();

    static DWORD WINAPI MyThreadFunction( void* pContext )
    {
        Foo *foo = static_cast<Foo*>(pContext);

        return foo->Function();
     }
};


Foo *foo = new Foo();

// call Foo::MyThreadFunction to start the thread
// Pass `foo` as the startup parameter of the thread function
CreateThread( NULL, 0, Foo::MyThreadFunction, foo, 0L, NULL );
structfoo
{
虚int函数();
静态DWORD WINAPI MyThreadFunction(void*pContext)
{
Foo*Foo=static_cast(pContext);
返回foo->Function();
}
};
Foo*Foo=新的Foo();
//调用Foo::MyThreadFunction来启动线程
//传递'foo'作为线程函数的启动参数
CreateThread(NULL,0,Foo::MyThreadFunction,Foo,0L,NULL);

函数指针和成员函数指针非常不同。您需要将成员函数声明为static。的可能重复项