Windows Explorer - PID returned by the Shell function

Jaafar Tribak

Well-known Member
Joined
Dec 5, 2002
Messages
9,616
Office Version
  1. 2016
Platform
  1. Windows
Hi,

I am shelling a new folder window in order to display the C:\ root directory as follows :

Code:
Sub Test()
    Dim vPid As Variant
    
    vPid = Shell("explorer.exe """ & "C:\" & "", vbNormalFocus)
    MsgBox vPid
    Call Shell("TaskKill /F /PID " & CStr(vPid), vbHide)
End Sub

A new explorer window is displayed as expected and a PID number is returned however when I pass the PID to the Shell function in the second call in order to terminate the explorer process and close the explorer window, the explorer window remains and never closes.

It looks as if the PID returned by the first shell call is "incorrect" (In fact, I can see in the Task Manager a different PID for the newly opened explorer)

I have tried other methods for opening explorer such as with the ShellExecute API, and Shell Automation but the returned PID is still not working.

Any thoughts anyone ?

Thank you.
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi Jaafar,

If you run Process Monitor, and filter on Process Name Is explorer.exe and show only process and thread activity, you'll see that the process started by Shell does indeed start explorer.exe (there is a Load Image operation which loads C:\Windows\explorer.exe), and this process's parent is Excel.exe. However, approx. 1/10th second later another explorer.exe process is started by svchost.exe, and then the original Shell'd process exits.
 
Upvote 0
Hi Jaafar,

If you run Process Monitor, and filter on Process Name Is explorer.exe and show only process and thread activity, you'll see that the process started by Shell does indeed start explorer.exe (there is a Load Image operation which loads C:\Windows\explorer.exe), and this process's parent is Excel.exe. However, approx. 1/10th second later another explorer.exe process is started by svchost.exe, and then the original Shell'd process exits.

Hi John_w,

Thank you for looking into this. I have read that the new shelled explorer hands the request to the existing explorer instance.

I guess there is no easy way, if any, to kill the shelled explorer by its PID.
 
Last edited:
Upvote 0
I know almost nothing about this anymore (it has been years and years since I have needed to do anything DOS related), but I have observed the following if it helps. It appears the PID that you are storing is that of the Command Prompt's window, not the File Explorer window that you actually open. If you run this code, the directory displays in the Command Prompt window and when you dismiss the MessageBox that Command Prompt window is removed.
Code:
Sub Test()
    Dim vPid As Variant
    
  vPid = Shell(Environ$("comspec") & " /k dir c:", vbNormalFocus)
  MsgBox vPid
  Call Shell("TaskKill /F /PID " & CStr(vPid), vbHide)

End Sub
I am guessing that the File Explorer window is a Window object which the Command Prompt window knows nothing about. I am not big into API stuff either but perhaps you can find a way to launch the File Explore window with a unique title so that you can use the Windows API functions to locate that specific window and kill it.
 
Last edited:
Upvote 0
I know almost nothing about this anymore (it has been years and years since I have needed to do anything DOS related), but I have observed the following if it helps. It appears the PID that you are storing is that of the Command Prompt's window, not the File Explorer window that you actually open. If you run this code, the directory displays in the Command Prompt window and when you dismiss the MessageBox that Command Prompt window is removed.
Code:
Sub Test()
    Dim vPid As Variant
    
  vPid = Shell(Environ$("comspec") & " /k dir c:", vbNormalFocus)
  MsgBox vPid
  Call Shell("TaskKill /F /PID " & CStr(vPid), vbHide)

End Sub
I am guessing that the File Explorer window is a Window object which the Command Prompt window knows nothing about. I am not big into API stuff either but perhaps you can find a way to launch the File Explore window with a unique title so that you can use the Windows API functions to locate that specific window and kill it.

Thanks Rick.

I could certainly use the Windows API to make sure I get the correct explorer PID but this was just one of those oddities that I wanted to investigate for the sake of learning.
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,257
Members
449,075
Latest member
staticfluids

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top