title: Get process by name in VB6
body: <p>I want to find a prosses ID by its name and wait for its finish to continue...
  Right now I just try to get this prosses by name and its not work for me...</p>
  
  <p>I have try to use this example</p>
  
  <p><a href="https://www.experts-exchange.com/questions/20480608/Get-All-process-exe-names-in-Visual-Basic-6-0.html" rel="nofollow noreferrer">https://www.experts-exchange.com/questions/20480608/Get-All-process-exe-names-in-Visual-Basic-6-0.html</a></p>
  
  <p>But I got 0 from GetProcesses method</p>
  
  <p>for example in C#
  I write this code </p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              int x = Process.GetProcessesByName("PulserTester").First().Id;
          }
      }
  </code></pre>
  
  <p>this code work just fine and returned Id 87068 I have run this both program on the same time now on debugging so I am sure that the PulserTester program is run...</p>
  
  <pre class="lang-vb prettyprint-override"><code>Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" ( _
      ByVal hWnd As Long, _
      ByVal lpOperation As String, _
      ByVal lpFile As String, _
      ByVal lpParameters As String, _
      ByVal lpDirectory As String, _
      ByVal nShowCmd As Long) As Long
  
  
  
  Private Declare Function CloseHandle Lib "Kernel32.dll" (ByVal Handle As Long) As Long
  
  Private Declare Function OpenProcess Lib "Kernel32.dll" (ByVal dwDesiredAccessas As Long, _
  ByVal bInheritHandle As Long, _
  ByVal dwProcId As Long) As Long
  
  Private Declare Function EnumProcesses Lib "psapi.dll" (ByRef lpidProcess As Long, _
  ByVal cb As Long, _
  ByRef cbNeeded As Long) As Long
  
  Private Declare Function GetModuleFileNameExA Lib "psapi.dll" (ByVal hProcess As Long, _
  ByVal hModule As Long, _
  ByVal ModuleName As String, _
  ByVal nSize As Long) As Long
  
  Private Declare Function EnumProcessModules Lib "psapi.dll" (ByVal hProcess As Long, _
  ByRef lphModule As Long, _
  ByVal cb As Long, _
  ByRef cbNeeded As Long) As Long
  
  Private Declare Function TerminateProcess Lib "kernel32" (ByVal hProcess As Long, _
  ByVal uExitCode As Long) As Long
  
  Private Declare Sub ExitProcess Lib "kernel32" (ByVal uExitCode As Long)
  
  
  Public Function GetProcesses(strProcess As String) As Long
  
   Dim lProcCount     As Long
   Dim lProcPoss      As Long
   Dim lNum           As Long
   Dim ProcessIDs()   As Long
   Dim cbNeeded2      As Long
   Dim NumElements2   As Long
   Dim arrModules(1 To 200) As Long
   Dim lRet           As Long
   Dim sModName       As String
   Dim hwndProcess    As Long
   Dim iCounter       As Long
  
   Const SIZE = 500
   Const PROCESS_QUERY_INFORMATION = 1024
   Const PROCESS_VM_READ = 16
   Const PROCESS_ALL_ACCESS = &amp;H1F0FFF
  
   'need to get the array containing the process id's for each process object
  
   lProcCount = 8
   lProcPoss = 96
   Do While lProcCount &lt;= lProcPoss
       lProcCount = lProcCount * 2
       ReDim ProcessIDs(lProcCount / 4) As Long
       lRet = EnumProcesses(ProcessIDs(1), lProcCount, lProcPoss)
   Loop
   lNum = lProcPoss / 4
  
   For iCounter = 1 To lNum
       'Get a handle to the Process
       hwndProcess = OpenProcess(PROCESS_ALL_ACCESS, 0, ProcessIDs(iCounter))
       'Got a Process handle
       If hwndProcess &lt;&gt; 0 Then
           'Get an array of the module handles for the specified
           'process
           lRet = EnumProcessModules(hwndProcess, arrModules(1), 200, cbNeeded2)
           'If the Module Array is retrieved, Get the ModuleFileName
           If lRet &lt;&gt; 0 Then
               sModName = Space(255)
               lRet = GetModuleFileNameExA(hwndProcess, arrModules(1), sModName, SIZE)
               'Form1.lstProcesses.AddItem ProcessIDs(iCounter) &amp; vbTab &amp; Left(sModName, lRet)
               If UCase(Left(sModName, lRet)) = UCase(strProcess) Then
                   GetProcesses = GetProcesses + 1
               End If
           End If
       End If
       'Close the handle
       lRet = CloseHandle(hwndProcess)
   Next
  
  End Function
  
  Private Sub Form_Load()
      ShellExecute Me.hWnd, "open", "C:\Users\hed-b\Desktop\PulserTester.appref-ms", vbNullString, "C:\", ByVal 1&amp;
  
      MsgBox GetProcesses("PulserTester")
  End Sub
  </code></pre>
  
  <p>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~</p>
  
  <p>In short I try to write the equivalent code to this code
  from C# to VB6</p>
  
  <pre><code>var p = new Process();
              p.StartInfo = new ProcessStartInfo(@"cmd.exe")
              {
                  Arguments = @"/c C:\Users\hed-b\Desktop\PulserTester.appref-ms"
              };
              p.Start();
              while(Process.GetProcessesByName("PulserTester").Any() == false)
              { Thread.Sleep(100); }
              p = Process.GetProcessesByName("PulserTester").First();
              p.WaitForExit();
  </code></pre>