如何Restart Shell(Explorer.exe)

來源:MSDN  , cww更改

其實這程式是找出Explorer所在的Window,而後用WM_QUIT令之結束,再用一個Loop等待Explorer
的結束,最後才用Sell指令執行Explorer.exe;


Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
        (ByVal lpClassName As String, ByVal lpWindowName As String) As Long
Private Declare Function PostMessage Lib "user32" Alias "PostMessageA" _
        (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, _
         ByVal lParam As Long) As Long
Const WM_QUIT = &H12

Private Sub Form_Load()
Dim hwndShell As Long, i As Long
hwndShell = FindWindow("Progman", vbNullString)
i = PostMessage(hwndShell, WM_QUIT, 0, 0)
If i = 0 Then Exit Sub
Do While True '等待原先的Shell結束
   hwndShell = FindWindow("Progman", vbNullString)
   If hwndShell = 0 Then
      Exit Do
   End If
Loop
Shell "Explorer.exe", vbNormalFocus '執行新的Shell
End Sub