有没有办法关闭所有资源管理器窗口而不重新启动explorer.exe进程?
上下文:- 在卸载基于 installshield 的安装程序期间,我必须删除一个用于显示文件右键单击上下文菜单的 dll。在卸载过程中,我必须删除该 dll。不幸的是它被explorer.exe锁定了。
有没有办法只关闭资源管理器窗口而不重新启动explorer.exe进程?
在谷歌搜索了这么多的线索之后,我可以想出以下 C++ 程序,它只关闭资源管理器窗口,而无需重新启动 explorer.exe 进程。
这里我使用 EnumWindows 并迭代所有窗口,并根据窗口的类名仅关闭资源管理器窗口。
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
wofstream myfile;
BOOL CALLBACK enumWindowsProc(
__in HWND hWnd,
__in LPARAM lParam
) {
int length = 255;
TCHAR* buffer,*buffer1;
buffer = new TCHAR[ length + 1 ];
buffer1 = new TCHAR[ length + 1 ];
memset( buffer, 0, ( length + 1 ) * sizeof( TCHAR ) );
memset( buffer1, 0, ( length + 1 ) * sizeof( TCHAR ) );
DWORD pid;
DWORD dwThreadID = ::GetWindowThreadProcessId( hWnd, &pid);
::GetWindowText(hWnd,buffer,length +1);
wstring windowTitle = wstring( buffer );
delete[] buffer;
//cout << windowTitle.c_str();
::GetClassName(hWnd,buffer1,length +1);
wstring windowClass = wstring( buffer1 );
delete[] buffer1;
if(windowClass.compare(L"CabinetWClass") == 0 || windowClass.compare(L"ExploreWClass") == 0)
{
//::PostMessage(hWnd, WM_ENDSESSION, MAKEWORD(true,1), ENDSESSION_CLOSEAPP);
//::PostMessage(hWnd, 0x5B4, 0, 0);
PostMessage(hWnd,WM_CLOSE,0,0);
}
myfile << windowTitle.c_str();
myfile << L"|" ;
myfile << pid ;
myfile << L"|" ;
myfile << windowClass.c_str() ;
myfile << L"\n" ;
return TRUE;
}
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
myfile.open ("processes.txt");
BOOL enumeratingWindowsSucceeded = ::EnumWindows( enumWindowsProc, NULL );
cin.get();
myfile.close();
return 0;
}
我确信您可以调用 FindWindow 并使用 SendMessage 来关闭资源管理器窗口,但 explorer.exe 进程仍将运行,并且您仍将拥有文件锁定。
Windows Installer 可以在重新启动时删除锁定的文件。如果您不想重新启动,则必须杀死并重新启动资源管理器。
据我所知,这里没有其他模式。
来自 InstallScript 语言参考的 FindWindow 示例:
/*--------------------------------------------------------------*\
*
* InstallShield Example Script
*
* Demonstrates the FindWindow and SendMessage functions.
*
* This script launches Windows Notepad and then calls
* FindWindow to locate the Notepad window. Next, it calls
* SendMessage to maximize the window; after a three-second
* delay, it calls SendMessage again to minimize the
* window. When the script ends, Windows NotePad remains
* open but minimized. Note that the parameters passed to
* SendMessage are Windows system messages whose values
* are defined as constants in this script.
*
* Note: Before running this script, set the preprocessor
* constant NOTEPAD so that it references the fully-
* qualified name of the Windows Notepad executable.
*
\*--------------------------------------------------------------*/
#define NOTEPAD "C:\\Windows\\Notepad.exe"
// Include Ifx.h for built-in InstallScript function prototypes.
#include "Ifx.h"
export prototype ExFn_FindWindow(HWND);
function ExFn_FindWindow(hMSI)
NUMBER nMsg, nwParam, nlParam;
HWND nHwnd;
begin
// Do not display the setup's background window.
Disable (BACKGROUND);
// Open the Windows Notepad.
if (LaunchApp (NOTEPAD, "") < 0 ) then
MessageBox ("Unable to launch Notepad.", SEVERE);
abort;
endif;
// Wait three seconds so we can view the window before
// it's maximized.
Delay (3);
// Retrieve the handle of the Notepad window. The first
// parameter is the window class. A null string in the
// second parameter specifies the topmost Notepad window.
nHwnd = FindWindow ("NotePAD", "");
if (nHwnd = NULL) then
MessageBox ("Unable to find the Notepad window.", SEVERE);
else
// Send system command to maximize the window.
SendMessage (nHwnd, WM_SYSCOMMAND, SC_MAXIMIZE, 0);
// Wait three seconds so we can view the window
// before it's minimized.
Delay (3);
// Send system command to minimize the window.
SendMessage (nHwnd, WM_SYSCOMMAND, SC_MINIMIZE, nlParam);
endif;
end;