title: C++ iterate processes and find out command line args of each process
body: <p>I have the following problem to solve (VS2012, C++)
  I have to find out if a specific HTA app is running from my exe. For that, I have to find the process mshta and check if it has correct arguments (should have been started as "mshta somehta.hta"). My first attempt was to iterate over the processes/modules, which I can do now. I see mshta listed and its PID. But, I did not find the way to get the info, how it was started. Is there a way to do it?</p>
  
  <pre><code>ProcessExists(wchar_t* processName)
  {
      DWORD aProcesses[1024], cbNeeded, cProcesses;
      unsigned int i;
  
      if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &amp;cbNeeded ) )
      {
          return false;
      }
  
  
      // Calculate how many process identifiers were returned.
  
      cProcesses = cbNeeded / sizeof(DWORD);
  
      // Print the name and process identifier for each process.
  
      for ( i = 0; i &lt; cProcesses; i++ )
      {
          if( aProcesses[i] != 0 )
          {
              PrintProcessNameAndID( aProcesses[i] );
          }
      }
  
      return false;
  
   }
   void PrintProcessNameAndID( DWORD processID )
  {
      TCHAR szProcessName[MAX_PATH] = TEXT("&lt;unknown&gt;");
  
      // Get a handle to the process.
  
      HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                 PROCESS_VM_READ,
                                 FALSE, processID );
  
      // Get the process name.
  
      if (NULL != hProcess )
      {
          HMODULE hMod;
          DWORD cbNeeded;
  
          if ( EnumProcessModules( hProcess, &amp;hMod, sizeof(hMod), 
           &amp;cbNeeded) )
          {
              GetModuleBaseName( hProcess, hMod, szProcessName, 
                             sizeof(szProcessName)/sizeof(TCHAR) );
  
  
          }
      }
  
      // Print the process name and identifier.
  
      dprintf( TEXT("%s  (PID: %u) %s %s\n"), szProcessName, processID );
  
     // Release the handle to the process.
  
     CloseHandle( hProcess );
  }
  </code></pre>