What is the Windows equivalent of pidof from Linux?

In batchscript, I need to get a list of process identifiers with a given binary path C:\path\to\binary.exe. On Linux, I can just do it pidof /path/to/binary.

Is there a Win32 executable that does the same thing supported by WinXP Home for Win7 (task list does not work)?

The package that includes this should be portable, so downloading 10 MB is not what I'm looking for.

Is there a C function that does this and is supported from WinXP to Win7? Note. I want to map the process path, not the file name, which can be used by other applications.

+3
source share
4 answers

API Toolhelp , . . . :

int main( int argc, char* argv[] )
{

    if( argc > 1 )
    {
        printf( "\nGetting PID of: %s\n", argv[1] );
        HANDLE hProcSnapshot = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
        if( INVALID_HANDLE_VALUE != hProcSnapshot )
        {
            PROCESSENTRY32 procEntry = {0};
            procEntry.dwSize = sizeof(PROCESSENTRY32);
            if( ::Process32First( hProcSnapshot, &procEntry ) )
            {
                do
                {
                    HANDLE hModSnapshot = ::CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, procEntry.th32ProcessID );
                    if( INVALID_HANDLE_VALUE != hModSnapshot )
                    {
                        MODULEENTRY32 modEntry = {0};
                        modEntry.dwSize = sizeof( MODULEENTRY32 );
                        if( Module32First( hModSnapshot, &modEntry ) )
                        {
                            if( 0 == stricmp( argv[1], modEntry.szExePath ) )
                            {
                                printf( "\nPID: %ld\n", procEntry.th32ProcessID );
                                ::CloseHandle( hModSnapshot );
                                break;
                            }
                        }
                        ::CloseHandle( hModSnapshot );
                    }
                }
                while( ::Process32Next( hProcSnapshot, &procEntry ) );
            }
            ::CloseHandle( hProcSnapshot );
        }
    }
    return 0;
}
+1

wmic.exe XP, Vista 7 . Windows XP.

wmic process where ExecutablePath='C:\\windows\\system32\\notepad.exe' get ProcessId

Windows XP Home, EnumProcess GetModuleFileNameEx. , , , . QueryFullProcessImageName, , , Vista +.

, Process32First ( swatkat). Module32First, MODULEENTRY32->szExePath. , x64, QueryFullProcessImageName.

+4

PowerShell , Win 7 .

param($fileName)
Get-Process | where -FilterScript {$_.MainModule.FileName -eq $fileName}

script , , , .

bat, :

powershell -Command "& {Get-Process | where -FilterScript {$ _. MainModule.FileName -eq% FILENAME%}"

+1
source

You can write a small C # application that first calls Process.GetProcessesByName (String) , then go to the results and print the Id property of each of them when MainModule. FileName is equal to the path you are looking for.

+1
source

Source: https://habr.com/ru/post/1774770/


All Articles