Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try block windows shutdown from windows update #12

Open
wants to merge 2 commits into
base: mod
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions OKEGui/OKEGui/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Startup="AppStartup"
SessionEnding="App_SessionEnding"
StartupUri="Gui/MainWindow.xaml">
<Application.Resources>
</Application.Resources>
Expand Down
22 changes: 22 additions & 0 deletions OKEGui/OKEGui/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using OKEGui.Utils;
using System;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;

namespace OKEGui
{
Expand All @@ -11,6 +14,25 @@ namespace OKEGui
public partial class App : Application
{
private static readonly NLog.Logger Logger = NLog.LogManager.GetCurrentClassLogger();
public const int WM_QUERYENDSESSION = 0x11;
public const int WM_ENDSESSION = 0x16;

[DllImport("user32.dll", SetLastError = true)]
static extern bool ShutdownBlockReasonCreate(IntPtr hWnd, [MarshalAs(UnmanagedType.LPWStr)] string reason);
[DllImport("user32.dll", SetLastError = true)]
static extern bool ShutdownBlockReasonDestroy(IntPtr hWnd);
[DllImport("kernel32.dll")]
static extern bool SetProcessShutdownParameters(uint dwLevel, uint dwFlags);

private void App_SessionEnding(object sender, SessionEndingCancelEventArgs e)
{

IntPtr windowHandle = Process.GetCurrentProcess().MainWindowHandle;
// Prevent windows shutdown
ShutdownBlockReasonCreate(windowHandle, "干啥呢干啥呢,OKEGui还没关呢?");
e.Cancel = true;

}

private void AppStartup(object sender, StartupEventArgs e)
{
Expand Down
6 changes: 6 additions & 0 deletions OKEGui/OKEGui/Gui/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using OKEGui.Worker;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;

namespace OKEGui
{
Expand Down Expand Up @@ -82,8 +83,12 @@ protected override void OnSourceInitialized(EventArgs e)
{
hwndSource.AddHook(_systemMenu.HandleMessage);
}
//HwndSource source = PresentationSource.FromVisual(this) as HwndSource;
//source.AddHook(WndProc);
}



private void Checkbox_Changed(object sender, RoutedEventArgs e)
{
if (!wm.IsRunning)
Expand Down Expand Up @@ -346,6 +351,7 @@ void MainWindow_Closing(object sender, CancelEventArgs e)
}
else
{
IntPtr windowHandle = Process.GetCurrentProcess().MainWindowHandle;
SubProcessService.KillAll();
}
}
Expand Down