Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 701119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:34:27+00:00 2026-05-14T03:34:27+00:00

In order to try out how to program with the Win32 API, I wrote

  • 0

In order to try out how to program with the Win32 API, I wrote a program that creates a process.
Then I want to check if my process waits for the newly created process, close the handle and then check WaitForSingleObject again (the second 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; //The process is finished
    else
        cout<<"waitprocess:false"<<endl;

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

    if (retwait= WaitForSingleObject (pro2info.hProcess, INFINITE)==true) //When the process has finished
        cout<<"waitprocess:true"<<endl;
    else
        cout<<"waitprocess:false"<<endl;

    //cout<<GetLastError()<<endl; //Output the last error.

    ExitProcess(0);
}

Second 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();
        //Wait until the user confirms

    Sleep (700);
    ExitProcess(0);

    cout<<"test";
}

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

Instead of the if-else statement, I 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 do wrong with my if-else statement?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T03:34:27+00:00Added an answer on May 14, 2026 at 3:34 am

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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to make my program more streamlined (Without try and catch everywhere), I
I have this simple little batch file program that I wrote but it fails
I have a raffle C++ program that I use to draw out of a
I try to order the results of a query by the date which is
I need to read the output from ffmpeg in order to even try the
I'm doing a HQL query as follows, and try to order the list by
I try developing two filter which order a list of groupforumtopics by the amount
I try to use the SQL statement SELECT * FROM table ORDER BY column
This is what I try to execute: CREATE TABLE order ( o_id INTEGER not
I'm try display data from column points , SELECT * from table ORDER by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.