win32 CreateThread ,how to call the lpStartAddress parameter as c++ class function?
i was reading the thread that talking about my problem here:
how to use CreateThread for functions which are class members
after using the code from microsoft site here :
How to spawn console processes with redirected standard handles i implemented this solution but there some problems like :i need to use the lpvThreadParam parameter in
DWOR开发者_如何学JAVAD WINAPI GetAndSendInputThread(LPVOID lpvThreadParam)
how can i implement this in the example RichieHindle suggested? in the ThreadStart function ?
also the parameters in the bRunThread and hStdIn (from the microsoft example i initiate them on the constructor with value but when i execute the ThreadStart with the content of the GetAndSendInputThread from the microsoft example it seams that the values are never set. i hope i was clear , basically i like to be able run this example as c++ class .Ok. I think I know where you are heading now. How about something like this. Create the pipe in the class constructor and then use the hInputWrite handle in your class thread function (called from the static thread function.
class MyClass
{
HANDLE hInputWrite;
HANDLE hInputRead;
SECURITY_ATTRIBUTES sa;
MyClass() {
if (!CreatePipe(&hOutputReadTmp,&hOutputWrite,&sa,0))
DisplayError("CreatePipe");
}
static DWORD WINAPI StaticThreadStart(void* Param)
{
MyClass* This = (MyClass*) Param;
return This->ThreadStart();
}
DWORD ThreadStart(void)
{
// Do stuff with hInputWrite handle here
}
void startMyThread()
{
DWORD ThreadID;
CreateThread(NULL, 0, StaticThreadStart, (void*) this, 0, &ThreadID);
}
};
MyClass thread;
thread.startMyThread();
(Modified from RichieHindles answer)
Just change StaticThreadStart
to pass the pointer it receives to the real method.
NB. If you are using the C or C++ standard library you should really be using beginthread
or beginthreadex
from the MS extensions to the C runtime, these ensure the C/C++ libraries are correctly initialised for the new thread.
精彩评论