Sure, here’s the translation: Java 停止 Windows 关机

huangapple go评论80阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年4月4日 01:12:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/61017025.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定