2013-04-23 18 views
5

私はC++(ATLを使用するDLL)で書いている.NETプロファイラで作業しています。私は30秒ごとにファイルに書き込むスレッドを作成したい。私は、スレッドにDLL内にスレッドを作成します

HANDLE l_handle = CreateThread(NULL, 0, MyThreadFunction, NULL, 0L, NULL); 

を起動しようとすると、私はスレッド機能は私のクラスの1

DWORD WINAPI CProfiler::MyThreadFunction(void* pContext) 
{ 
    //Instructions that manipulate attributes from my class; 
} 

の方法になりたい私はこのエラーを得た:

argument of type "DWORD (__stdcall CProfiler::*)(void *pContext)" 
is incompatible with parameter of type "LPTHREAD_START_ROUTINE" 

どのように適切にDLL内にスレッドを作成しますか? 助けが必要です。

+1

関数ポインタとメンバ関数へのポインタは非常に異なります。メンバ関数をstaticとして宣言する必要があります。 –

+0

[クラスメンバである関数に対してCreateThreadを使用する方法は?](http://stackoverflow.com/questions/1372967/how-do-you-use-createthread-for-functions-which-are-class - 会員) –

答えて

7

メンバ関数へのポインタを正規関数ポインタのように渡すことはできません。メンバ関数をstaticとして宣言する必要があります。オブジェクトのメンバー関数を呼び出す必要がある場合は、プロキシ関数を使用できます。

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); 
関連する問題