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. Using OpenProcess

Using OpenProcess

Scheduled Pinned Locked Moved C / C++ / MFC
helptutorialquestion
5 Posts 2 Posters 12 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.
  • C Offline
    C Offline
    capricious_001
    wrote on last edited by
    #1

    Hi guys, I am trying to use openprocess however I am running into a few problems. Specifically for the last parameter of the function OpenProcess, I am wondering how would I retreive the process id using hwnd retrieved from FindWindow? I have tried GetProcessId however that doesnt seem to work. I get a compiler error saying that its an undeclared identifier. However I have included windows.h so there should not be any problems. Would anyone know how to get the process id of a window handle? Thanks, Robbie

    O 1 Reply Last reply
    0
    • C capricious_001

      Hi guys, I am trying to use openprocess however I am running into a few problems. Specifically for the last parameter of the function OpenProcess, I am wondering how would I retreive the process id using hwnd retrieved from FindWindow? I have tried GetProcessId however that doesnt seem to work. I get a compiler error saying that its an undeclared identifier. However I have included windows.h so there should not be any problems. Would anyone know how to get the process id of a window handle? Thanks, Robbie

      O Offline
      O Offline
      ovidiucucu
      wrote on last edited by
      #2

      First note that GetProcessId requires at least Windows XP or Windows Server 2003. Call GetWindowThreadProcessId and OpenProcess:

         DWORD dwProcessID = 0;
         ::GetWindowThreadProcessId(hWnd, &dwProcessID);
         HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION,
                                         FALSE,
                                         dwProcessID);
         if(NULL != hProcess)
         {
            // enjoy of hProcess;
            // ...
            ::CloseHandle(hProcess);
         }
         else
         {
            // ... oups, error!
            DWORD dwError = ::GetLastError();
            // ...
         }
      

      Ovidiu Cucu Microsoft MVP - Visual C++

      C 1 Reply Last reply
      0
      • O ovidiucucu

        First note that GetProcessId requires at least Windows XP or Windows Server 2003. Call GetWindowThreadProcessId and OpenProcess:

           DWORD dwProcessID = 0;
           ::GetWindowThreadProcessId(hWnd, &dwProcessID);
           HANDLE hProcess = ::OpenProcess(PROCESS_QUERY_INFORMATION,
                                           FALSE,
                                           dwProcessID);
           if(NULL != hProcess)
           {
              // enjoy of hProcess;
              // ...
              ::CloseHandle(hProcess);
           }
           else
           {
              // ... oups, error!
              DWORD dwError = ::GetLastError();
              // ...
           }
        

        Ovidiu Cucu Microsoft MVP - Visual C++

        C Offline
        C Offline
        capricious_001
        wrote on last edited by
        #3

        lol I just saw and used that from the MSDN and it worked. Thanks for confirming thats the right one to use!

        O 1 Reply Last reply
        0
        • C capricious_001

          lol I just saw and used that from the MSDN and it worked. Thanks for confirming thats the right one to use!

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

          You are welcome! BTW. Is MSDN really so funny to "lol", or just my answer ? ;) Ovidiu Cucu Microsoft MVP - Visual C++

          C 1 Reply Last reply
          0
          • O ovidiucucu

            You are welcome! BTW. Is MSDN really so funny to "lol", or just my answer ? ;) Ovidiu Cucu Microsoft MVP - Visual C++

            C Offline
            C Offline
            capricious_001
            wrote on last edited by
            #5

            I've skewed the meaning of "lol" so much that I end up including it in almost every post even if I have a straight face, but if I had to literally have a chuckle at your answer then it would have to be corniest joke I would have ever laughed at ;)

            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