CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: processid

  1. #1
    Join Date
    Mar 1999
    Posts
    16

    processid



    how can i get a processid of a running process which i

    did not start ?

  2. #2
    Join Date
    Feb 2002
    Posts
    4

    Re: processid

    Hi,
    U can use this function...
    long GetProcessId(const char* szProcessName)
    {
    LPDWORD lpdwPIDs ;
    DWORD dwSize, dwSize2, dwIndex ;
    HMODULE hMod ;
    HANDLE hProcess ;
    char szFileName[ MAX_PATH ] ;

    // Call the PSAPI function EnumProcesses to get all of the
    // ProcID's currently in the system.
    // NOTE: In the documentation, the third parameter of
    // EnumProcesses is named cbNeeded, which implies that you
    // can call the function once to find out how much space to
    // allocate for a buffer and again to fill the buffer.
    // This is not the case. The cbNeeded parameter returns
    // the number of PIDs returned, so if your buffer size is
    // zero cbNeeded returns zero.
    // NOTE: The "HeapAlloc" loop here ensures that we
    // actually allocate a buffer large enough for all the
    // PIDs in the system.
    dwSize2 = 256 * sizeof( DWORD ) ;
    lpdwPIDs = NULL ;
    do
    {
    if( lpdwPIDs )
    {
    HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
    dwSize2 *= 2 ;
    }
    lpdwPIDs = (unsigned long *)HeapAlloc( GetProcessHeap(), 0, dwSize2 );
    if( lpdwPIDs == NULL )
    {
    return 0 ;
    }
    if( !EnumProcesses( lpdwPIDs, dwSize2, &dwSize ) )
    {
    HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
    return 0 ;
    }
    }while( dwSize == dwSize2 ) ;

    // How many ProcID's did we get?
    dwSize /= sizeof( DWORD ) ;

    // Loop through each ProcID.
    for( dwIndex = 0 ; dwIndex < dwSize ; dwIndex++ )
    {
    szFileName[0] = 0 ;
    // Open the process (if we can... security does not
    // permit every process in the system).
    hProcess = OpenProcess(
    PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
    FALSE, lpdwPIDs[ dwIndex ] ) ;
    if( hProcess != NULL )
    {
    // Here we call EnumProcessModules to get only the
    // first module in the process this is important,
    // because this will be the .EXE module for which we
    // will retrieve the full path name in a second.
    if( EnumProcessModules( hProcess, &hMod, sizeof( hMod ), &dwSize2 ) )
    {
    // Get Full pathname:
    if( !GetModuleFileNameEx( hProcess, hMod,
    szFileName, sizeof( szFileName ) ) )
    {
    szFileName[0] = 0 ;
    }
    }
    CloseHandle( hProcess ) ;
    }

    if (szFileName[0])
    {
    char* pFile = strrchr(szFileName,'\\');
    pFile++;

    // Did we just bump into what we looked for ?
    if( _stricmp(pFile,szProcessName)==0)
    {
    long ProcessIdToReturn = lpdwPIDs[ dwIndex ];
    HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
    return ProcessIdToReturn;
    }
    }
    }

    HeapFree( GetProcessHeap(), 0, lpdwPIDs ) ;
    return 0;
    }


    This will get u ProcessID for any running process...Only Argument u have pass is the Process NAme.
    Like GetProcessId("myprocess.exe");


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