如何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