title: Find my own process ID in VBScript
body: <p>I'm using the following code snippet to determine what process ID my vbscript is running as:</p>
  
  <pre><code>On Error Resume Next
  Dim iMyPID : iMyPID = GetObject("winmgmts:root\cimv2").Get("Win32_Process.Handle='" &amp; CreateObject("WScript.Shell").Exec("mshta.exe").ProcessID &amp; "'").ParentProcessId
  If Err.Number &lt;&gt; 0 Then Call Handle_Error(Err.Description)
  On Error Goto 0
  </code></pre>
  
  <p>On my Windows 7 (32-bit) machine this works about 90% of the time and <code>iMyPID</code> contains the process ID of the currently running script. However 10% of the time <code>Handle_Error</code> gets called with the error message "<strong>SWbemServicesEX: Not found</strong>".</p>
  
  <p>Recently someone else running Windows 7 (64-bit) reported that <code>Handle_Error</code> always gets called with the error message "<strong>Out of memory</strong>". This seems an insane error message just to find out your own process ID!</p>
  
  <p>Can anyone recommend a better way of doing this?</p>