2

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

DWORD 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 .

Community
  • 1
  • 1
user63898
  • 29,839
  • 85
  • 272
  • 514

2 Answers2

2

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)

Community
  • 1
  • 1
rtn
  • 127,556
  • 20
  • 111
  • 121
1

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.

Richard
  • 106,783
  • 21
  • 203
  • 265
  • Use of beginthread is only required in certain relatively unusual circumstances. Normally, the CRT state for each thread is auto-initialised on thread entry. This is necessary because the CRT is used on lots of threads (such as threadpool threads) that aren't created by BeginThread. You only need to do manual beginthread calls if you have static-linked to the CRT (not recommended anyway) *and* your binary is either an EXE or a DLL with DisableThreadLibraryCalls. General advice: Don't static link the CRT. – Martyn Lovell May 07 '11 at 20:12
  • @Martyn: so it is... nice when restrictions are removed. I suppose I should re-read docs for APIs I know every decade or so :-) – Richard May 08 '11 at 07:10