tags:

views:

189

answers:

3

hello in order to try out how to programm with the win32 api i wrote a programm that creates a process. then i want to check if my proccess waits for the new created process, close the handle and then check waitforsingle obj. again. (the seconde process is sleeping for 700 ms)

first process:

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

void main()
{

bool ret;
bool retwait;

STARTUPINFO startupinfo;
GetStartupInfo (&startupinfo);

PROCESS_INFORMATION pro2info;

wchar_t wcsCommandLine[] = L"D:\\betriebssystemePRA1PRO2.exe";



    ret = CreateProcess(NULL, wcsCommandLine, NULL, NULL, false, CREATE_NEW_CONSOLE, NULL,
        NULL, &startupinfo, &pro2info);


    cout<<"hProcess: "<<pro2info.hProcess<<endl;
    cout<<"dwProcessId: "<<pro2info.dwProcessId <<endl;

if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true)
        cout<<"waitprocess:true"<<endl; //prozess ist fertig
    else
        cout<<"waitprocess:false"<<endl;

    CloseHandle (pro2info.hProcess);//prozesshandle schließen, "verliert connection" 

    if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true)//wenn der prozess fertig ist
        cout<<"waitprocess:true"<<endl;
    else
        cout<<"waitprocess:false"<<endl;


    //cout<<GetLastError()<<endl;   //gibt den letzten fehler aus

ExitProcess(0);


}

seconde process:

#include <iostream>
#include <windows.h>
#include <string>

using namespace std;

void main()
{


    int b;

    b=GetCurrentProcessId();

    cout<<b<<endl;
    cout<<"Druecken Sie Enter zum Beenden"<<endl;
    cin.get();
        //warten bis Benutzer bestätigt

    Sleep (700);
    ExitProcess(0);

    cout<<"test";
}

the first process prints false, false ; but it should print true, false.

instead of the if-else statement is used this:

//switch(WaitForSingleObject (pro2info.hProcess, INFINITE)){
    //  case WAIT_OBJECT_0: cout << "ja";
    //      break;
    //  case WAIT_FAILED:cout << "nein";
    //      break;
    //  case WAIT_TIMEOUT:
    //      break;
    //}
    //  cout<<"waitprocess:true"<<endl;//prozess ist fertig
    //else
    //  cout<<"waitprocess:false"<<endl;

and this seems to work. what did i wrong with my if-else statement? thanks in advance

A: 

According to MSDN, WaitForSingleObject will return WAIT_OBJECT_0 if the wait wasn't aborted. If you check the documentation, the value of WAIT_OBJECT_0 happens to be 0x00000000L, which happens to be the value commonly converted to false, not true. Hence your comparison fails.

Promoting the return value of WaitForSingleObject to a bool is IMHO not a good idea given that you get several potentially illuminating non-zero return codes that indicate why the wait expired.

If you still want to keep the above code as using a boolean check, change the tests to !WaitForSingleObject(...) instead.

Timo Geusch
A: 

I think you sort of answered your question yourself. The point is that WaitForSingleObject doesn't return true or false, but WAIT_OBJECT_0 et al.

So instead of

if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true)

you need

if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==WAIT_OBJECT_0)
jpalecek
+2  A: 

You really need to pay attention to the meaning for the return value of the API functions. You cannot ignore a FALSE return from CreateProcess(). WaitForSingleObject() can return several values, it returns 0 if the wait completed successfully. Which makes you print "false".

Hans Passant