Thread: GetExitCodeProcess ExitProcess Tester..

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Question GetExitCodeProcess ExitProcess Tester..

    Done CreateProcess OK,

    Now want to ExitProcess aqain.......
    problem lies in how I give the ExitProcess the exitstatus code
    from within my program......

    How do I use GetExitCodeProcess....or what do I do

    See my code below

    Thanks......


    Tony.void __fastcall TfmCreateProcess::OpenNotepadClick(TObject *Sender)
    {
    // don't enter the function if we are already waiting
    // for another instance of notepad to close.
    if(bWaiting)
    return;

    char szWindowDir [MAX_PATH];
    GetWindowsDirectory(szWindowDir, MAX_PATH);
    AnsiString strNotepad = AnsiString(szWindowDir) + "\\notepad.exe";
    strNotepad = strNotepad + " " + "c:\\_CutSizes\\Readme.txt";

    STARTUPINFO StartupInfo;
    ZeroMemory( &StartupInfo, sizeof(STARTUPINFO));
    StartupInfo.cb = sizeof(STARTUPINFO);

    PROCESS_INFORMATION ProcessInfo;

    if(CreateProcess(NULL, strNotepad.c_str(), NULL, NULL,
    TRUE, NORMAL_PRIORITY_CLASS, NULL, NULL,
    &StartupInfo, &ProcessInfo))
    {
    bWaiting = true;
    Label1->Caption = "Waiting for Notepad to close";
    dwExitStatus = STILL_ACTIVE;

    // loop until Notepad closes.........

    // NOTE: this is where I want another button on
    // my form to close Notepad....question is how do
    // I get the exitstatus code in order to execute
    // ExitProcess....?

    do
    {
    if( !GetExitCodeProcess(ProcessInfo.hProcess, &dwExitStatus))
    break;
    Application->ProcessMessages();
    } while (dwExitStatus == STILL_ACTIVE);

    Label1->Caption = "done";
    bWaiting = false;

    // dont' need the handles any more, so close them
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
    }
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Try TerminateProcess() as it will allow you to kill another process ans opposed to ExitProcess() whick kills the current process...

    ...Excuse my rushed example, but this should do what you want

    Code:
    #include <windows.h>
    #include <iostream>
    #include <string>
    using namespace std;
    
    int main(){
    
    STARTUPINFO StartupInfo;
    PROCESS_INFORMATION ProcessInfo;
    string str = "Notepad.exe",
           strEnt;
    
    ZeroMemory( &StartupInfo, sizeof(STARTUPINFO));
    StartupInfo.cb = sizeof(STARTUPINFO);
    
    if(CreateProcess(NULL,
                     const_cast<CHAR*>(str.c_str()),
                     NULL,
                     NULL,
                     TRUE,
                     NORMAL_PRIORITY_CLASS,
                     NULL,
                     NULL,
                     &StartupInfo,
                     &ProcessInfo)){
    
    cout << "Type \"QUIT\" to kill notepad" << endl;
    cin >> strEnt;
    if (strEnt == "QUIT"){
    TerminateProcess(ProcessInfo.hProcess,
                     0);
    }
    }
    return 0;
    }
    Oh...and beware...there may be Access right complications on WinNT and above. The Process handle needs PROCESS_TERMINATE rights. And as I am using toy windows (win98) as I sit here, I cant provide for that in my example

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Primality Tester
    By eurylochus1 in forum C Programming
    Replies: 13
    Last Post: 09-27-2008, 10:08 AM
  2. Help me with my basic program, nothing I create will run
    By Ravens'sWrath in forum C Programming
    Replies: 31
    Last Post: 05-13-2007, 02:35 AM
  3. getexitcodeprocess exit code
    By rotis23 in forum Windows Programming
    Replies: 1
    Last Post: 09-23-2004, 05:09 AM