Reputation: 507
I start an explorer window in java using
ProcessBuilder pb = new ProcessBuilder("explorer.exe",folderToOpen);
Process p = pb.start();
Before my java program exists, I want to close this window. It is important, because this folder is actually a virtual folder. The folder has been created using a java library for creating kernel level virtual filesystem. When the program exists, the folder ceases to exists. Windows explorer doesn't realize this, and keeps showing a number of error messages. These error messages stop only when the user closes the explorer window of the virtual folder.
For better user experience I need to automatically close this virtual folder on exit.
The problem with explorer.exe is that, it doesn't open the new window in the instance used to send the command. The window is opened in another process. So I don't have the handle to the process, and so I cannot close it.
I tried the JNA route, but this threw a system error code 5 (meaning access denied )
final String folderName = Paths.get(super.path).getFileName().toString();
final WinDef.HWND[] windowHandle = new WinDef.HWND[1];
User32.INSTANCE.EnumWindows(new WinUser.WNDENUMPROC() {
@Override
public boolean callback(WinDef.HWND hwnd, Pointer pointer) {
char[]c = new char[255];
User32.INSTANCE.GetWindowText(hwnd, c, 250);
String s = new String(c).trim();
System.out.println(s);
if (s.equals(folderName)) {
windowHandle[0] = hwnd;
User32.INSTANCE.DestroyWindow(hwnd);
System.out.println(Kernel32.INSTANCE.GetLastError());
return false;
}
return true;
}
}, Pointer.NULL);
Any suggestion, comments, tips, guide, ideas, hints ? Thanks :D
Update : This seems to work, however, the risk is that it might close a similar window ( a window whoes name is same as the window I want to close). This would be bad-side effect.
final String folderName = Paths.get(super.path).getFileName().toString();
User32.INSTANCE.EnumWindows(new WinUser.WNDENUMPROC() {
@Override
public boolean callback(WinDef.HWND hwnd, Pointer pointer) {
char[]c = new char[255];
User32.INSTANCE.GetWindowText(hwnd, c, 250);
String s = new String(c).trim();
System.out.println(s);
if (s.equals(folderName)) {
User32.INSTANCE.PostMessage(hwnd,User32.WM_CLOSE,null,null);
System.out.println(Kernel32.INSTANCE.GetLastError());
return false;
}
return true;
}
}, Pointer.NULL);
anyone there with a better idea ? Thanks again :D
Upvotes: 1
Views: 974