Skip to content

Commit

Permalink
Implemented several bugfixes
Browse files Browse the repository at this point in the history
Implemented several bugfixes
  • Loading branch information
SamyoFox authored Dec 4, 2021
2 parents 8d2a29f + 6c8c8f9 commit 7d3cb30
Show file tree
Hide file tree
Showing 6 changed files with 125 additions and 22 deletions.
4 changes: 0 additions & 4 deletions BluescreenSimulator/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ public static void ShowOnMonitor(this Window window, Screen targetScreen)

public static void ExecuteCmdCommands(params string[] commands)
{


var cmd = new Process
{
StartInfo =
Expand All @@ -46,8 +44,6 @@ public static void ExecuteCmdCommands(params string[] commands)
cmd.StandardInput.Flush();
}
cmd.StandardInput.Close();
cmd.WaitForExit();
Console.WriteLine(cmd.StandardOutput.ReadToEnd());
}

public static void SetWindowToScreen(Window window, Screen screen)
Expand Down
6 changes: 4 additions & 2 deletions BluescreenSimulator/ViewModels/BluescreenViewModelBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using BluescreenSimulator.Views;
using System;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
Expand Down Expand Up @@ -167,7 +169,7 @@ public async Task StartProgress(CancellationToken token = default)
Progress = 0;
return;
}
if (EnableUnsafe && !string.IsNullOrWhiteSpace(CmdCommand))
if (MainWindow.EnableUnsafe && !string.IsNullOrWhiteSpace(CmdCommand))
{
Utils.ExecuteCmdCommands(CmdCommand);
}
Expand Down
3 changes: 2 additions & 1 deletion BluescreenSimulator/Views/BluescreenWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,15 @@ private void SetUpQR()
true);
var source = Imaging.CreateBitmapSourceFromHBitmap(bitmap.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
*/
QrCodeImage.Source = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources.qr.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
QrCodeImage.Source = Imaging.CreateBitmapSourceFromHBitmap(Properties.Resources.qr_transparent.GetHbitmap(), IntPtr.Zero, Int32Rect.Empty, BitmapSizeOptions.FromEmptyOptions());
}

private async void Bluescreen_Loaded(object sender, RoutedEventArgs e)
{
try
{
await _vm.StartProgress(_source.Token);
_realClose = true;
Close(); // we're done
}
catch (TaskCanceledException)
Expand Down
2 changes: 1 addition & 1 deletion BluescreenSimulator/Views/BluescreenWindowWin7.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
Background="{converters:ColorBinding BackgroundColor}"
Foreground="{converters:ColorBinding ForegroundColor}"
WindowStartupLocation="CenterScreen" Style="{StaticResource BluescreenWindowStyle}"
Title="Bluescreen" KeyDown="Window_KeyDown" Closing="Window_Closing">
Title="Bluescreen">
<Window.Resources>
<Style TargetType="TextBlock">
<Setter Property="FontSize" Value="23"/>
Expand Down
115 changes: 106 additions & 9 deletions BluescreenSimulator/Views/BluescreenWindowWin7.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
using System.Threading;
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using BluescreenSimulator.ViewModels;
using KeyEventArgs = System.Windows.Input.KeyEventArgs;
using static System.Windows.Input.Key;
using System.Linq;

namespace BluescreenSimulator.Views
{
Expand All @@ -14,40 +19,132 @@ public partial class BluescreenWindowWin7 : Window
{
//Variables
private readonly Windows7BluescreenViewModel _vm;
private bool _realClose;


public BluescreenWindowWin7(Windows7BluescreenViewModel vm = null)
{ // gets the main screen current Resolution
DataContext = _vm = vm ?? new Windows7BluescreenViewModel();
InitializeComponent();

Task.Run(SetupScreen);
Cursor = Cursors.None;
Loaded += Bluescreen_Loaded;
Closing += Window_AboutToClose;
KeyDown += Window_PreviewKeyDown;
HookKeyboard();
}

private readonly CancellationTokenSource _source = new CancellationTokenSource();

private async Task SetupScreen()
private async void Bluescreen_Loaded(object sender, RoutedEventArgs e)
{
try
{
await _vm.StartProgress(_source.Token);
_realClose = true;
Close(); // we're done
}
catch (TaskCanceledException)
{
// ok
_vm.Progress = 0;
}
}

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
private void Window_AboutToClose(object sender, CancelEventArgs e)
{
_source.Cancel(); // cancel the current progress.
e.Cancel = !_realClose; // no.
if (!e.Cancel)
{
_source.Cancel();
UnhookWindowsHookEx(hHook);
}
else
{
Focus();
}
}

private void Window_KeyDown(object sender, KeyEventArgs e)
private static readonly Key[] BlockingKeys = { Key.System, F4, LWin, RWin, Tab, LeftAlt, RightAlt };

private void Window_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.F7)
if (_realClose) return;
if (e.Key is Key.F7)
{
_realClose = true;
Close();
}

if (BlockingKeys.Contains(e.Key))
{
e.Handled = true;
}
}


#region Windows Api

private void HookKeyboard()
{
var hModule = GetModuleHandle(IntPtr.Zero);
hookProc = LowLevelKeyboardProc;
hHook = SetWindowsHookEx(WH_KEYBOARD_LL, hookProc, hModule, 0);
if (hHook == IntPtr.Zero)
{
MessageBox.Show("Failed to set hook, error = " + Marshal.GetLastWin32Error());
}
}

private struct KBDLLHOOKSTRUCT
{
public int vkCode;
private int scanCode;
public int flags;
private int time;
private int dwExtraInfo;
}

private delegate int LowLevelKeyboardProcDelegate(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);

[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelKeyboardProcDelegate lpfn, IntPtr hMod, int dwThreadId);

[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hHook);

[DllImport("user32.dll")]
private static extern int CallNextHookEx(int hHook, int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam);

[DllImport("kernel32.dll")]
private static extern IntPtr GetModuleHandle(IntPtr path);

private IntPtr hHook;
private LowLevelKeyboardProcDelegate hookProc; // prevent gc
private const int WH_KEYBOARD_LL = 13;

private static int LowLevelKeyboardProc(int nCode, int wParam, ref KBDLLHOOKSTRUCT lParam)
{
if (nCode >= 0)
switch (wParam)
{
case 256: // WM_KEYDOWN
case 257: // WM_KEYUP
case 260: // WM_SYSKEYDOWN
case 261: // M_SYSKEYUP
if (
(lParam.flags == 32) || // alt whatever after
(lParam.vkCode == 0x1b && lParam.flags == 32) || // Alt+Esc
(lParam.vkCode == 0x73 && lParam.flags == 32) || // Alt+F4
(lParam.vkCode == 0x1b && lParam.flags == 0) || // Ctrl+Esc
(lParam.vkCode == 0x5b && lParam.flags == 1) || // Left Windows Key
(lParam.vkCode == 0x5c && lParam.flags == 1)) // Right Windows Key
{
return 1; //Do not handle key events
}
break;
}
return CallNextHookEx(0, nCode, wParam, ref lParam);
}

#endregion Windows Api
}
}
17 changes: 12 additions & 5 deletions BluescreenSimulator/Views/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Diagnostics;
using System.IO;
using System.Windows;
using System.Windows.Input;
Expand All @@ -9,14 +10,14 @@ namespace BluescreenSimulator.Views
{
public partial class MainWindow : Window
{
private bool enableUnsafe;
public static bool EnableUnsafe;
private MainWindowViewModel _vm;
private IBluescreenViewModel CurrentBluescreen => _vm.SelectedBluescreen;
public MainWindow(bool enableUnsafe)
{
InitializeComponent();
DataContext = _vm = new MainWindowViewModel();
this.enableUnsafe = enableUnsafe;
EnableUnsafe = enableUnsafe;
InputBindings.Add(new InputBinding(new DelegateCommand(() =>
{
Settings.Default.IsDarkTheme = !Settings.Default.IsDarkTheme;
Expand Down Expand Up @@ -66,7 +67,7 @@ private void GenerateExe(object sender, RoutedEventArgs e)
}

var iexpressSED =
$@"
$@"
[Version]
Class=IEXPRESS
SEDVersion=3
Expand All @@ -92,7 +93,13 @@ private void GenerateExe(object sender, RoutedEventArgs e)
var SEDPath = Path.GetTempPath() + "\\optionfile.SED";

File.WriteAllText(SEDPath, iexpressSED);
Utils.ExecuteCmdCommands($"C:\\Windows\\system32\\iexpress.exe /N {SEDPath}");
Process p = new Process();
p.StartInfo.FileName = "C:\\Windows\\system32\\iexpress.exe";
p.StartInfo.Arguments = $"/N {SEDPath}";
p.StartInfo.CreateNoWindow = true;
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.Start();
p.WaitForExit();
File.Delete(SEDPath);

MessageBox.Show("Your EXE-File has been created.", "BluescreenWindow Simulator", MessageBoxButton.OK, MessageBoxImage.Information);
Expand All @@ -108,7 +115,7 @@ private string GenerateCommand()

private bool CheckData()
{
if (CurrentBluescreen.EnableUnsafe && !string.IsNullOrEmpty(CurrentBluescreen.CmdCommand.Trim()))
if (EnableUnsafe && !string.IsNullOrEmpty(CurrentBluescreen.CmdCommand.Trim()))
{
var messageBoxResult = MessageBox.Show("Using a CMD command can be dangerous. " +
"I will not be responsible for any data loss or other damage arising from irresponsible or careless use of the CMD command option. " +
Expand Down

0 comments on commit 7d3cb30

Please sign in to comment.