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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. get the process' path by its pid

get the process' path by its pid

Scheduled Pinned Locked Moved C / C++ / MFC
algorithmsdata-structureshelpquestionworkspace
8 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.
  • K Offline
    K Offline
    Krauze
    wrote on last edited by
    #1

    Ive tried the following codes in win32 console environment

    DWORD   dwPIDLst\[1024\]; // the array to store the PIDs
    DWORD   dwBytesWritten; // employed to calculate the number of PIDs
    HANDLE  hProc;
    HMODULE hMod;
    char    strPath\[MAX\_PATH\];
    int     nPIDNum;
    // enumerate all the processes that are executing
    EnumProcesses( dwPIDLst, sizeof( dwPIDLst ), &dwBytesWritten );
    // get the number of the processes
    // this algorithm is suggested in the MSDN
    nPIDNum = dwBytesWritten / sizeof( DWORD );
    
    for( int i = 0; i < nPIDNum; i++ )
    {
    	ZeroMemory( strPath, sizeof( strPath ) ); // i'm not sure whether this line is needed
    
    	hProc = OpenProcess( PROCESS\_QUERY\_INFORMATION | PROCESS\_VM\_READ, FALSE, dwPIDLst\[i\] );
    	EnumProcessModules( hProc, &hMod, sizeof( hMod ), &dwBytesWritten );
    	GetModuleFileNameEx( hProc, hMod, strPath, sizeof( strPath ) );
    
    	CloseHandle( hProc );
    }
    

    But there come probs: 1.cannot convert char[260] to LPTSTR ( strPath ) 2.cannot convert HANDLE to HMODULE ( hMod ) All of these occur in the function GetModuleFileNameEx() Can anyone help? Really thanx!

    C L N 3 Replies Last reply
    0
    • K Krauze

      Ive tried the following codes in win32 console environment

      DWORD   dwPIDLst\[1024\]; // the array to store the PIDs
      DWORD   dwBytesWritten; // employed to calculate the number of PIDs
      HANDLE  hProc;
      HMODULE hMod;
      char    strPath\[MAX\_PATH\];
      int     nPIDNum;
      // enumerate all the processes that are executing
      EnumProcesses( dwPIDLst, sizeof( dwPIDLst ), &dwBytesWritten );
      // get the number of the processes
      // this algorithm is suggested in the MSDN
      nPIDNum = dwBytesWritten / sizeof( DWORD );
      
      for( int i = 0; i < nPIDNum; i++ )
      {
      	ZeroMemory( strPath, sizeof( strPath ) ); // i'm not sure whether this line is needed
      
      	hProc = OpenProcess( PROCESS\_QUERY\_INFORMATION | PROCESS\_VM\_READ, FALSE, dwPIDLst\[i\] );
      	EnumProcessModules( hProc, &hMod, sizeof( hMod ), &dwBytesWritten );
      	GetModuleFileNameEx( hProc, hMod, strPath, sizeof( strPath ) );
      
      	CloseHandle( hProc );
      }
      

      But there come probs: 1.cannot convert char[260] to LPTSTR ( strPath ) 2.cannot convert HANDLE to HMODULE ( hMod ) All of these occur in the function GetModuleFileNameEx() Can anyone help? Really thanx!

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      Krauze wrote:

      1.cannot convert char[260] to LPTSTR ( strPath )

      Change from:

      Krauze wrote:

      char strPath[MAX_PATH];

      To:

      TCHAR strPath[MAX_PATH];

      Krauze wrote:

      2.cannot convert HANDLE to HMODULE ( hMod )

      This is bit strange, since you're passing an HMODULE... :)

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      K 1 Reply Last reply
      0
      • K Krauze

        Ive tried the following codes in win32 console environment

        DWORD   dwPIDLst\[1024\]; // the array to store the PIDs
        DWORD   dwBytesWritten; // employed to calculate the number of PIDs
        HANDLE  hProc;
        HMODULE hMod;
        char    strPath\[MAX\_PATH\];
        int     nPIDNum;
        // enumerate all the processes that are executing
        EnumProcesses( dwPIDLst, sizeof( dwPIDLst ), &dwBytesWritten );
        // get the number of the processes
        // this algorithm is suggested in the MSDN
        nPIDNum = dwBytesWritten / sizeof( DWORD );
        
        for( int i = 0; i < nPIDNum; i++ )
        {
        	ZeroMemory( strPath, sizeof( strPath ) ); // i'm not sure whether this line is needed
        
        	hProc = OpenProcess( PROCESS\_QUERY\_INFORMATION | PROCESS\_VM\_READ, FALSE, dwPIDLst\[i\] );
        	EnumProcessModules( hProc, &hMod, sizeof( hMod ), &dwBytesWritten );
        	GetModuleFileNameEx( hProc, hMod, strPath, sizeof( strPath ) );
        
        	CloseHandle( hProc );
        }
        

        But there come probs: 1.cannot convert char[260] to LPTSTR ( strPath ) 2.cannot convert HANDLE to HMODULE ( hMod ) All of these occur in the function GetModuleFileNameEx() Can anyone help? Really thanx!

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        I just compiled the above and did not see message number 2. Are you sure that is the exact message your compiler produced?

        It's time for a new signature.

        K 1 Reply Last reply
        0
        • C CPallini

          Krauze wrote:

          1.cannot convert char[260] to LPTSTR ( strPath )

          Change from:

          Krauze wrote:

          char strPath[MAX_PATH];

          To:

          TCHAR strPath[MAX_PATH];

          Krauze wrote:

          2.cannot convert HANDLE to HMODULE ( hMod )

          This is bit strange, since you're passing an HMODULE... :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          K Offline
          K Offline
          Krauze
          wrote on last edited by
          #4

          Ive changed strPath from char to TCHAR. And the codes can be successfully compiled. However, another prob occurs that the program aborts because of potential memory abuse ( DEP has noticed it ). As for the 2nd prob, it may be caused by the 1st one. As the former is solved, the compiler doesnt warn it any more.

          1 Reply Last reply
          0
          • L Lost User

            I just compiled the above and did not see message number 2. Are you sure that is the exact message your compiler produced?

            It's time for a new signature.

            K Offline
            K Offline
            Krauze
            wrote on last edited by
            #5

            It may be caused by the 1st one. As the former is solved, the compiler doesnt warn it any more.

            L 1 Reply Last reply
            0
            • K Krauze

              It may be caused by the 1st one. As the former is solved, the compiler doesnt warn it any more.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Yes quite possibly, but not in my compiler - Visual C++ 2010 Express Edition.

              It's time for a new signature.

              1 Reply Last reply
              0
              • K Krauze

                Ive tried the following codes in win32 console environment

                DWORD   dwPIDLst\[1024\]; // the array to store the PIDs
                DWORD   dwBytesWritten; // employed to calculate the number of PIDs
                HANDLE  hProc;
                HMODULE hMod;
                char    strPath\[MAX\_PATH\];
                int     nPIDNum;
                // enumerate all the processes that are executing
                EnumProcesses( dwPIDLst, sizeof( dwPIDLst ), &dwBytesWritten );
                // get the number of the processes
                // this algorithm is suggested in the MSDN
                nPIDNum = dwBytesWritten / sizeof( DWORD );
                
                for( int i = 0; i < nPIDNum; i++ )
                {
                	ZeroMemory( strPath, sizeof( strPath ) ); // i'm not sure whether this line is needed
                
                	hProc = OpenProcess( PROCESS\_QUERY\_INFORMATION | PROCESS\_VM\_READ, FALSE, dwPIDLst\[i\] );
                	EnumProcessModules( hProc, &hMod, sizeof( hMod ), &dwBytesWritten );
                	GetModuleFileNameEx( hProc, hMod, strPath, sizeof( strPath ) );
                
                	CloseHandle( hProc );
                }
                

                But there come probs: 1.cannot convert char[260] to LPTSTR ( strPath ) 2.cannot convert HANDLE to HMODULE ( hMod ) All of these occur in the function GetModuleFileNameEx() Can anyone help? Really thanx!

                N Offline
                N Offline
                norish
                wrote on last edited by
                #7

                One question.

                DWORD dwPIDLst[1024];

                nPIDNum = dwBytesWritten / sizeof( DWORD );

                Was it ok? In other words, nPIDNum is under 1024?

                K 1 Reply Last reply
                0
                • N norish

                  One question.

                  DWORD dwPIDLst[1024];

                  nPIDNum = dwBytesWritten / sizeof( DWORD );

                  Was it ok? In other words, nPIDNum is under 1024?

                  K Offline
                  K Offline
                  Krauze
                  wrote on last edited by
                  #8

                  In fact there's a fault in my codes when dynamically loading GetModuleFileNameEx(). But Ive corrected it. So it can perfectly run now. What I need to do now is just to add exception handlers. Thank yall.

                  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