CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Mar 2003
    Location
    buffalo
    Posts
    30

    GetProcessStatus ? How can i do that

    I wanna get the status of a process to sewe if it is running or not.
    i got some sample source code from here however it was very out of date and i got lots of errors when testing it. i believe it was b/c of it only working on winNT/95 ...
    i wanna be able to see if a proces is still runnin by doing osomethign like GetProcessStatus("process name")
    and haveing it return true or false
    any suggestions are helpful.
    Thankz
    TuX

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    Under Win32, one solution is OpenProcess().

    Kuphryn

  3. #3
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Well what OS are you using? Look at the PSAPI Functions...

  4. #4
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    You can't really just use "process name" because more than one
    process of the same name can be run. Having said that, you can
    probably muck something together. There's a function called
    GetExitCodeProcess() that will tell you if a process is running or
    not [given its handle]. Here's a link to all of the WIN32 process
    and thread functions; they ought to be able to help you:

    http://msdn.microsoft.com/library/de...asp?frame=true

  5. #5
    Join Date
    Mar 2003
    Location
    buffalo
    Posts
    30

    this heps a little

    the progrma im using as a process was ported from linux ot windows so i doint know if the geterrorcodeprocess will work.. however im intersted in open process
    thankz for help..

  6. #6
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    The functions I gave you are WIN32 API functions; as long as you
    are running on WIN32, it doesn't matter where the code was
    originally developed. The only thing you need to worry about with
    GetExitCodeProcess() is that the process doesn't return
    PROCESS_STILL_ACTIVE as an error code.

    --Paul

  7. #7
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Though it's still not being clear on whether they have the handle to the process or not...eg: did they create the process, or do they need to enum the processes...or maybe I'm just reading it wrong

  8. #8
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    EnumProcesses() returns an array of PIDs. OpenProcess() will
    yield a process handle, given its PID. Thus, they don't need to
    have created the process in order to query its termination state.

    --Paul

  9. #9
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Thus my question on whether they created the process or not which directly leads to if they need to enumprocesses or not...guess we pretty much covered the bases though...

  10. #10
    Join Date
    Mar 2003
    Location
    buffalo
    Posts
    30

    i created it

    i created the process it is a 16-bit program thats been ported form linux via cygwin1.dll if that makes a difference.
    thankz
    TuX

  11. #11
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    Did you try the functions like I suggested? Unless you try those,
    I don't have any other advice. Before you reply by saying "they
    didn't work" be sure to give us your sample code and pertinent
    error codes that were returned so that we can better assist you.

  12. #12
    Join Date
    Mar 2003
    Location
    buffalo
    Posts
    30

    so far.. some luck

    ive been trying to use EnumProcess() and i have been having some luck

    Code:
    BOOL runnin;
    DWORD aProcesses[1024], cbNeeded, cProcesses;
        unsigned int i;
        EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded );       
        // Calculate how many process identifiers were returned.
        cProcesses = cbNeeded / sizeof(DWORD);
    	for ( i = 0; i < cProcesses; i++ ) {
            char szProcessName[MAX_PATH] = "unknown";
    	    // Get a handle to the process.
            HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                           PROCESS_VM_READ,
                                           FALSE, aProcesses[i] );
    
        // Get the process name.
            if (NULL != hProcess )     {
                HMODULE hMod;
                DWORD cbNeeded;
                if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
                 &cbNeeded) )        {
                GetModuleBaseName( hProcess, hMod, szProcessName, 
                                   sizeof(szProcessName) );
                }
             }
            if( szProcessName == "myprogram.exe" ) {
     runnin = true;
    printf("fhshsh\n"); //lemme know if it get here
    		}
    
    
    printf( "%s (Process ID: %u)\n", szProcessName, aProcesses[i] );  // dispay process info 
    
        CloseHandle( hProcess );
    }
    i can see the process myprogram.exe running but the program doesnt pick it up... it tells me on "dispay process info" except on the line before if( szProcessName == "myprogram.exe" ) {
    it doesnt work.. maybe im doing something wrong...

    btw i havent had any major probelms compiling it just stupid stuff..
    Last edited by tuxp3; March 10th, 2003 at 09:36 PM.
    Thankz,
    TuX

  13. #13
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    What do you mean "It doesn't pick it up" ? What is "it"? Which
    function isn't working for you? Is the function returning an error
    message?

    --Paul

  14. #14
    Join Date
    Sep 2002
    Location
    Maryland - Fear The Turtle!
    Posts
    7,537
    Stop and take a moment to analyze your code Stop overwriting szProcessName with unknown, and also try doing:

    if( strcmp(szProcessName,"myprog.exe") == 0 )
    {

    }

    Maybe it just looks like noodles cause of the forum but if you still insist on if () { over
    if ()
    {
    }

    then have fun reading your own code down the line.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured