Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help! Check for process is running or not ...

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
Hi,
I need to find out if the process is running or not. I require to get this information with help of the name of the process. How do i do it using 'C' on windows ????

Thanks in advance .

Sanjay
 
See MSDN, Enumerating all processes article. Use EnumProcesses - EnumProcessModules - GetModuleBaseName calls chain (psapi.h - psapi.lib from Windows SDK, or psapi.dll) - then select the proper process from the list.
See example source from this article:
Code:
void PrintProcessNameAndID( DWORD processID )
{
    char szProcessName[MAX_PATH] = "unknown";

    // Get a handle to the process.

    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                   PROCESS_VM_READ,
                                   FALSE, processID );

    // Get the process name.

    if ( hProcess )
    {
        HMODULE hMod;
        DWORD cbNeeded;

        if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), 
             &cbNeeded) )
        {
            GetModuleBaseName( hProcess, hMod, szProcessName, 
                               sizeof(szProcessName) );
        }
    }

    // Print the process name and identifier.

    printf( "%s (Process ID: %u)\n", szProcessName, processID );

    CloseHandle( hProcess );
}

void main( )
{
    // Get the list of process identifiers.

    DWORD aProcesses[1024], cbNeeded, cProcesses;
    unsigned int i;

    if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
        return;

    // Calculate how many process identifiers were returned.

    cProcesses = cbNeeded / sizeof(DWORD);

    // Print the name and process identifier for each process.

    for ( i = 0; i < cProcesses; i++ )
        PrintProcessNameAndID( aProcesses[i] );
}
Be careful, there are many processes (My WS runs 152 now;). Don't forget: OS may reuse pids...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top