英文:
Java Stop Windows Shutdown
问题
我有一个奇怪的问题。我想要检测当Windows想要关闭时,并使用Java阻止它关闭。我考虑使用一个关闭钩子,然后运行一个命令shutdown /a,但似乎不起作用。
英文:
I have a odd problem. I want to detect when windows wants to shutdown and stop it from shutting down using java. I thought about using a shutdown hook and just run a command shutdown /a however it doesnt seem to be working.
答案1
得分: 1
你需要响应 WM_QUERYENDSESSION
消息并返回 0。
你可以使用 JNA 来实现。
另一个示例在这里:https://github.com/mirror/jdownloader/blob/master/src/org/jdownloader/osevents/windows/jna/ShutdownDetect.java
英文:
You need to respond to the WM_QUERYENDSESSION
message and return 0.
You can do it with JNA.
Another example here: https://github.com/mirror/jdownloader/blob/master/src/org/jdownloader/osevents/windows/jna/ShutdownDetect.java
答案2
得分: 0
经过几周的问题解决斗争,我终于解决了它,现在想在这里分享代码。以下是一个使用Java 1.8和JNA 5.13.0(还有platform)的简单示例。
提示:我最初在使用此代码时遇到问题(未满足的链接)。我发现我在JDK上有一个库(jar文件),其中包含较旧版本的JNA。确保您的项目中只有一个版本的JNA。
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.*;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.*;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.platform.win32.User32;
public class RealShutdownBlocker {
private static final int WM_QUERYENDSESSION = 0x0011;
public static void main(String[] args) {
User32 user32 = Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
WindowProc callback = new WindowProc() {
public LRESULT callback(HWND hWnd, int message, WPARAM wParam, LPARAM lParam) {
if (message == WM_QUERYENDSESSION) {
return new LRESULT(0);
}
return user32.DefWindowProc(hWnd, message, wParam, lParam);
}
};
WNDCLASSEX windowClass = new WNDCLASSEX();
windowClass.hInstance = null;
windowClass.lpszClassName = "BlockShutdown";
windowClass.lpfnWndProc = callback;
user32.RegisterClassEx(windowClass);
HWND hwnd = user32.CreateWindowEx(0, "BlockShutdown", "This prevents windows shutdown", WinUser.WS_OVERLAPPEDWINDOW, 100, 100, 640,
480, null, null, null, null);
// 需要设置为SW_SHOW才能正常工作
user32.ShowWindow(hwnd, WinUser.SW_SHOW);
MSG msg = new MSG();
while (user32.GetMessage(msg, null, 0, 0) != 0) {
user32.TranslateMessage(msg);
user32.DispatchMessage(msg);
}
}
}
编辑:不需要kernel32。已删除。
英文:
After battling with this problem few weeks and finally solving it i wanted to share the code here. Here is minimal example using java 1.8 and JNA 5.13.0 (also platform).
TIP: I had problem using this code first (unsatisfied links). I discovered i had library (jar file) on my JDK which had older JNA in it. Make sure you only have 1 version of JNA in your project.
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.*;
import com.sun.jna.platform.win32.WinUser;
import com.sun.jna.platform.win32.WinUser.*;
import com.sun.jna.win32.W32APIOptions;
import com.sun.jna.platform.win32.User32;
public class RealShutdownBlocker {
private static final int WM_QUERYENDSESSION = 0x0011;
public static void main(String[] args) {
User32 user32 = Native.loadLibrary("user32", User32.class, W32APIOptions.DEFAULT_OPTIONS);
WindowProc callback = new WindowProc() {
public LRESULT callback(HWND hWnd, int message, WPARAM wParam, LPARAM lParam) {
if (message == WM_QUERYENDSESSION) {
return new LRESULT(0);
}
return user32.DefWindowProc(hWnd, message, wParam, lParam);
}
};
WNDCLASSEX windowClass = new WNDCLASSEX();
windowClass.hInstance = null;
windowClass.lpszClassName = "BlockShutdown";
windowClass.lpfnWndProc = callback;
user32.RegisterClassEx(windowClass);
HWND hwnd = user32.CreateWindowEx(0, "BlockShutdown", "This prevents windows shutdown", WinUser.WS_OVERLAPPEDWINDOW, 100, 100, 640,
480, null, null, null, null);
// needs to be SW_SHOW for this to work
user32.ShowWindow(hwnd, WinUser.SW_SHOW);
MSG msg = new MSG();
while (user32.GetMessage(msg, null, 0, 0) != 0) {
user32.TranslateMessage(msg);
user32.DispatchMessage(msg);
}
}
}
EDIT: No need for kernel32. Deleted it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论