Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Process ID

Process ID

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Opwar
    wrote on last edited by
    #1

    How do you find a programs Process ID?

    D J 2 Replies Last reply
    0
    • O Opwar

      How do you find a programs Process ID?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      How about GetCurrentProcessId()?


      "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

      1 Reply Last reply
      0
      • O Opwar

        How do you find a programs Process ID?

        J Offline
        J Offline
        Jens Doose
        wrote on last edited by
        #3

        That depends on the kind of data you already have. Is it the current process? Or do you have the name of the executable? Or a window handle? Jens

        O 1 Reply Last reply
        0
        • J Jens Doose

          That depends on the kind of data you already have. Is it the current process? Or do you have the name of the executable? Or a window handle? Jens

          O Offline
          O Offline
          Opwar
          wrote on last edited by
          #4

          Yeah the name is armyops.exe

          J D 2 Replies Last reply
          0
          • O Opwar

            Yeah the name is armyops.exe

            J Offline
            J Offline
            Jens Doose
            wrote on last edited by
            #5

            Depending on your operating system you can use - the PSAPI functions (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/process_status_helper.asp) - or the ToolHelp functions (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/tool_help_library.asp) Jens

            1 Reply Last reply
            0
            • O Opwar

              Yeah the name is armyops.exe

              D Offline
              D Offline
              Diddy
              wrote on last edited by
              #6

              If you have the name of the process, and that process is not you, you must either search for a window you know that process created, and get the process ID from that, or, if you only have the name of the process, you need to use the PSAPI/ToolHelper API to enumerate all running processes, look at each one's name, check if it matched, and if so, grab it's process ID. See here http://msdn.microsoft.com/library/default.asp?url=/library/en-us/perfmon/base/about\_psapi.asp Bare in mind PSAPI only works with NT/2k/XP so if you want a cross platform soulition, use ToolTip - see the file Tlhelp32.h in the platform SDK for a statring point- but you want something like this which terminates a process given a name BOOL CTestEnvKillerApp::InitInstance() { // check which o/s OSVERSIONINFO osvi; memset(&osvi, NULL, sizeof(OSVERSIONINFO)); osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO); GetVersionEx(&osvi); if (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) // NT { // load the process status helper library HMODULE hLib = LoadLibrary( _T("psapi.dll") ); if (NULL == hLib) { AfxMessageBox(_T("Could not load PSAPI.DLL.\nPlease check it's in your path"), MB_OK | MB_ICONEXCLAMATION); return FALSE; } // get the functions lpfnEnumProcesses pFnEnumProc = (lpfnEnumProcesses)GetProcAddress( hLib, "EnumProcesses" ); lpfnGetModuleBaseName pFnGetModBaseName = (lpfnGetModuleBaseName)GetProcAddress( hLib, "GetModuleBaseNameA" ); lpfnEnumProcessModules pFnEnumProcMod = (lpfnEnumProcessModules)GetProcAddress( hLib, "EnumProcessModules" ); if ( (NULL == pFnEnumProc) || (NULL == pFnGetModBaseName) || (NULL == pFnEnumProcMod) ) { AfxMessageBox(_T("Could not find procedure in PSAPI.DLL"), MB_OK | MB_ICONEXCLAMATION); FreeLibrary(hLib); return FALSE; } DWORD dwNeeded = 0; DWORD dwProcessIDs[1024]; memset(dwProcessIDs, NULL, sizeof(DWORD)*1024); // get all the process IDs if ( pFnEnumProc(dwProcessIDs, 1024, &dwNeeded) ) { char szProcessName[MAX_PATH]; short sNumProcs = (short)( dwNeeded/sizeof(DWORD) ); for (short s = 0; s < sNumProcs; s++) { memset(szProcessName, NULL, sizeof(char)*MAX_PATH); // get the handle HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | PROCESS_TERMINATE, FALSE, dwProcessIDs[s] ); if (hProcess) { HMODULE hMod = NULL; dwNeeded = 0; // retrieve handle if ( pFnEnumProcMod( hProcess, &hMod, sizeof(hMod), &dwNeeded) ) {

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups