gpt4 book ai didi

c++ - 如何使用某个驱动器中打开的文件夹关闭 Windows 资源管理器窗口

转载 作者:可可西里 更新时间:2023-11-01 14:49:58 33 4
gpt4 key购买 nike

我正在编写一个允许用户弹出(或安全移除)USB 驱动器的小应用程序。我的应用程序运行良好,但在 Windows 资源管理器中打开 USB 驱动器上的一个文件夹(或多个文件夹)的情况除外。在这种情况下,弹出功能会失败,因为驱动器似乎已被锁定。

所以我很好奇,因为用户正在通过我的应用程序发出弹出 USB 驱动器的命令,是否有任何方法可以让资源管理器从 USB 驱动器关闭那些打开的窗口?

附言。请注意,我不想关闭属于 Windows 资源管理器的所有进程,而只想关闭在特定驱动器上打开文件夹的进程。

最佳答案

procedure ProcessExplorerWindows(ADriveLetter: WideChar; AClose: Boolean);
var
ShellWindows: IShellWindows;
i: Integer;
Dispatch: IDispatch;
WebBrowser2: IWebBrowser2;
ServiceProvider: IServiceProvider;
ShellBrowser: IShellBrowser;
ShellView: IShellView;
FolderView: IFolderView;
PersistFolder2: IPersistFolder2;
ItemIDList: PItemIDList;
ShellFolder: IShellFolder;
ChildItem: PItemIDList;
Attr: DWORD;
StrRet: TStrRet;
FileName: UnicodeString;
DesktopItemIDList: PItemIDList;
begin
OleCheck(CoCreateInstance(CLASS_ShellWindows, nil, CLSCTX_INPROC_SERVER or CLSCTX_LOCAL_SERVER, IShellWindows, ShellWindows));
try
for i := ShellWindows.Count - 1 downto 0 do
begin
Dispatch := ShellWindows.Item(i);
try
OleCheck(Dispatch.QueryInterface(IWebBrowser2, WebBrowser2));
try
OleCheck(Dispatch.QueryInterface(IServiceProvider, ServiceProvider));
try
OleCheck(ServiceProvider.QueryService(SID_STopLevelBrowser, IShellBrowser, ShellBrowser));
try
OleCheck(ShellBrowser.QueryActiveShellView(ShellView));
try
OleCheck(ShellView.QueryInterface(IFolderView, FolderView));
try
OleCheck(FolderView.GetFolder(IPersistFolder2, PersistFolder2));
try
OleCheck(PersistFolder2.GetCurFolder(ItemIDList));
try
OleCheck(SHBindToParent(ItemIDList, IShellFolder, Pointer(ShellFolder), ChildItem));
try
Attr := SFGAO_FILESYSTEM;
OleCheck(ShellFolder.GetAttributesOf(1, ChildItem, Attr));
if Attr and SFGAO_FILESYSTEM = SFGAO_FILESYSTEM then
begin
OleCheck(ShellFolder.GetDisplayNameOf(ChildItem, SHGDN_FORPARSING, StrRet));
FileName := '';
case StrRet.uType of
STRRET_WSTR:
begin
FileName := StrRet.pOleStr;
CoTaskMemFree(StrRet.pOleStr);
end;
STRRET_OFFSET:
if Assigned(ChildItem) then
begin
Inc(PByte(ChildItem), StrRet.uOffset);
FileName := UnicodeString(PAnsiChar(ChildItem));
end;
STRRET_CSTR:
FileName := UnicodeString(AnsiString(StrRet.cStr));
end;
if ExtractFileDrive(FileName) = ADriveLetter + ':' then
if AClose then
WebBrowser2.Quit
else
begin
SHGetFolderLocation(0, CSIDL_DESKTOP, 0, 0, DesktopItemIDList);
try
OleCheck(ShellBrowser.BrowseObject(DesktopItemIDList, SBSP_SAMEBROWSER or SBSP_DEFMODE or SBSP_ABSOLUTE));
finally
CoTaskMemFree(DesktopItemIDList);
end;
end;
end;
finally
ShellFolder := nil;
end;
finally
CoTaskMemFree(ItemIDList);
end;
finally
PersistFolder2 := nil
end;
finally
FolderView := nil;
end;
finally
ShellView := nil;
end;
finally
ShellBrowser := nil;
end;
finally
ServiceProvider := nil;
end;
finally
WebBrowser2 := nil;
end;
finally
Dispatch := nil;
end;
end;
finally
ShellWindows := nil;
end;
end;

关于c++ - 如何使用某个驱动器中打开的文件夹关闭 Windows 资源管理器窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23965895/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com