Improvements and cleanup

This commit is contained in:
CreepyCrafter24 2019-11-29 21:27:24 +01:00
parent ef4fadde3b
commit c95f2b8584
22 changed files with 289 additions and 229 deletions

View File

@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CC_Functions.Misc
namespace CC_Functions.Misc
{
public static class ArrayFormatter
{
@ -22,4 +16,4 @@ namespace CC_Functions.Misc
catch { throw; }
}
}
}
}

73
Misc/HID.cs Normal file
View File

@ -0,0 +1,73 @@
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.Text;
namespace Misc
{
public static class HID
{
private static byte[] _fingerPrint;
private static readonly string HIDClasses = @"Win32_Processor:UniqueId
Win32_Processor:ProcessorId
Win32_Processor:Name
Win32_Processor:Manufacturer
Win32_BIOS:Manufacturer
Win32_BIOS:SMBIOSBIOSVersion
Win32_BIOS:IdentificationCode
Win32_BIOS:SerialNumber
Win32_BIOS:ReleaseDate
Win32_BIOS:Version
Win32_BaseBoard:Model
Win32_BaseBoard:Manufacturer
Win32_BaseBoard:Name
Win32_BaseBoard:SerialNumber
Win32_NetworkAdapterConfiguration:MACAddress:IPEnabled";
public static byte[] Value
{
get {
if (_fingerPrint == null)
{
string fingerprint_tmp = "";
HIDClasses.Split('\r').Select(s =>
{
if (s.StartsWith("\n"))
s = s.Remove(0, 1);
return s.Split(':');
}).ToList().ForEach(s =>
{
using (ManagementClass mc = new ManagementClass(s[0]))
using (ManagementObjectCollection moc = mc.GetInstances())
{
ManagementBaseObject[] array = moc.OfType<ManagementBaseObject>().ToArray();
for (int j = 0; j < array.Length; j++)
{
if ((s.Length > 2) && array[j][s[2]].ToString() != "True") continue;
try
{
fingerprint_tmp += array[j][s[1]].ToString();
break;
}
catch
{
}
}
}
});
using (MD5 sec = new MD5CryptoServiceProvider())
{
byte[] bt = Encoding.ASCII.GetBytes(fingerprint_tmp);
_fingerPrint = sec.ComputeHash(bt);
}
}
return _fingerPrint;
}
}
public static byte[] EncryptLocal(byte[] unencrypted) => ProtectedData.Protect(unencrypted, Value, DataProtectionScope.CurrentUser);
public static byte[] DecryptLocal(byte[] encrypted) => ProtectedData.Unprotect(encrypted, Value, DataProtectionScope.CurrentUser);
}
}

View File

@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CC_Functions.Misc</RootNamespace>
<AssemblyName>CC_Functions.Misc</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
@ -45,6 +46,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ArrayFormatter.cs" />
<Compile Include="HID.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Security.cs" />
</ItemGroup>

View File

@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@ -33,4 +32,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -1,20 +1,12 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Management;
using System.Security.Cryptography;
using System.IO;
using System.Security.Principal;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Misc
{
public static class MiscFunctions
{
public static bool IsAdministrator => new WindowsPrincipal(WindowsIdentity.GetCurrent()).IsInRole(WindowsBuiltInRole.Administrator);
public static long GetDirectorySize(string path)
{
try
@ -30,67 +22,4 @@ namespace Misc
catch { throw; }
}
}
public static class HID
{
private static byte[] _fingerPrint;
static readonly string HIDClasses = @"Win32_Processor:UniqueId
Win32_Processor:ProcessorId
Win32_Processor:Name
Win32_Processor:Manufacturer
Win32_BIOS:Manufacturer
Win32_BIOS:SMBIOSBIOSVersion
Win32_BIOS:IdentificationCode
Win32_BIOS:SerialNumber
Win32_BIOS:ReleaseDate
Win32_BIOS:Version
Win32_BaseBoard:Model
Win32_BaseBoard:Manufacturer
Win32_BaseBoard:Name
Win32_BaseBoard:SerialNumber
Win32_NetworkAdapterConfiguration:MACAddress:IPEnabled";
public static byte[] Value
{
get {
if (_fingerPrint == null)
{
string fingerprint_tmp = "";
HIDClasses.Split('\r').Select(s =>
{
if (s.StartsWith("\n"))
s = s.Remove(0, 1);
return s.Split(':');
}).ToList().ForEach(s =>
{
using (ManagementClass mc = new ManagementClass(s[0]))
using (ManagementObjectCollection moc = mc.GetInstances())
{
ManagementBaseObject[] array = moc.OfType<ManagementBaseObject>().ToArray();
for (int j = 0; j < array.Length; j++)
{
if ((s.Length > 2) && array[j][s[2]].ToString() != "True") continue;
try
{
fingerprint_tmp += array[j][s[1]].ToString();
break;
}
catch
{
}
}
}
});
using (MD5 sec = new MD5CryptoServiceProvider())
{
byte[] bt = Encoding.ASCII.GetBytes(fingerprint_tmp);
_fingerPrint = sec.ComputeHash(bt);
}
}
return _fingerPrint;
}
}
public static byte[] EncryptLocal(byte[] unencrypted) => ProtectedData.Protect(unencrypted, Value, DataProtectionScope.CurrentUser);
public static byte[] DecryptLocal(byte[] encrypted) => ProtectedData.Unprotect(encrypted, Value, DataProtectionScope.CurrentUser);
}
}
}

View File

@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
</startup>
</configuration>

View File

@ -54,7 +54,6 @@
this.power_mode_box = new System.Windows.Forms.ComboBox();
this.wnd_select_mouse = new System.Windows.Forms.Button();
this.wnd_action_destroy = new System.Windows.Forms.Button();
this.wnd_action_overlay = new System.Windows.Forms.Button();
this.wnd_action_front = new System.Windows.Forms.Button();
this.wnd_action_enabled = new System.Windows.Forms.CheckBox();
this.wnd_action_title_get = new System.Windows.Forms.Button();
@ -65,10 +64,11 @@
this.wnd_select_selected = new System.Windows.Forms.Label();
this.wnd_select_self = new System.Windows.Forms.Button();
this.wnd = new System.Windows.Forms.GroupBox();
this.wnd_select_class_box = new System.Windows.Forms.TextBox();
this.wnd_action_icon = new System.Windows.Forms.Panel();
this.wnd_action_visible = new System.Windows.Forms.CheckBox();
this.wnd_action_style = new System.Windows.Forms.ComboBox();
this.wnd_action_visible = new System.Windows.Forms.CheckBox();
this.wnd_action_icon = new System.Windows.Forms.Panel();
this.wnd_select_class_box = new System.Windows.Forms.TextBox();
this.wnd_action_overlay = new System.Windows.Forms.CheckBox();
this.keyboard.SuspendLayout();
((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_h_bar)).BeginInit();
((System.ComponentModel.ISupportInitialize)(this.wnd_action_pos_w_bar)).BeginInit();
@ -333,16 +333,6 @@
this.wnd_action_destroy.UseVisualStyleBackColor = true;
this.wnd_action_destroy.Click += new System.EventHandler(this.Wnd_action_destroy_Click);
//
// wnd_action_overlay
//
this.wnd_action_overlay.Location = new System.Drawing.Point(147, 106);
this.wnd_action_overlay.Name = "wnd_action_overlay";
this.wnd_action_overlay.Size = new System.Drawing.Size(64, 23);
this.wnd_action_overlay.TabIndex = 11;
this.wnd_action_overlay.Text = "Overlay";
this.wnd_action_overlay.UseVisualStyleBackColor = true;
this.wnd_action_overlay.Click += new System.EventHandler(this.Wnd_action_overlay_Click);
//
// wnd_action_front
//
this.wnd_action_front.Location = new System.Drawing.Point(168, 160);
@ -432,6 +422,7 @@
//
// wnd
//
this.wnd.Controls.Add(this.wnd_action_overlay);
this.wnd.Controls.Add(this.wnd_action_style);
this.wnd.Controls.Add(this.wnd_action_visible);
this.wnd.Controls.Add(this.wnd_action_icon);
@ -446,7 +437,6 @@
this.wnd.Controls.Add(this.wnd_action_pos);
this.wnd.Controls.Add(this.wnd_select_mouse);
this.wnd.Controls.Add(this.wnd_action_destroy);
this.wnd.Controls.Add(this.wnd_action_overlay);
this.wnd.Controls.Add(this.wnd_action_front);
this.wnd.Controls.Add(this.wnd_action_enabled);
this.wnd.Controls.Add(this.wnd_action_title_get);
@ -464,21 +454,15 @@
this.wnd.TabStop = false;
this.wnd.Text = "CC-Functions.W32.Wnd32";
//
// wnd_select_class_box
// wnd_action_style
//
this.wnd_select_class_box.Location = new System.Drawing.Point(93, 79);
this.wnd_select_class_box.Name = "wnd_select_class_box";
this.wnd_select_class_box.Size = new System.Drawing.Size(104, 20);
this.wnd_select_class_box.TabIndex = 5;
//
// wnd_action_icon
//
this.wnd_action_icon.BackColor = System.Drawing.SystemColors.ControlLight;
this.wnd_action_icon.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.wnd_action_icon.Location = new System.Drawing.Point(217, 106);
this.wnd_action_icon.Name = "wnd_action_icon";
this.wnd_action_icon.Size = new System.Drawing.Size(23, 23);
this.wnd_action_icon.TabIndex = 22;
this.wnd_action_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.wnd_action_style.FormattingEnabled = true;
this.wnd_action_style.Location = new System.Drawing.Point(6, 133);
this.wnd_action_style.Name = "wnd_action_style";
this.wnd_action_style.Size = new System.Drawing.Size(121, 21);
this.wnd_action_style.TabIndex = 24;
this.wnd_action_style.SelectedIndexChanged += new System.EventHandler(this.Wnd_action_style_SelectedIndexChanged);
//
// wnd_action_visible
//
@ -491,15 +475,32 @@
this.wnd_action_visible.UseVisualStyleBackColor = true;
this.wnd_action_visible.CheckedChanged += new System.EventHandler(this.Wnd_action_visible_CheckedChanged);
//
// wnd_action_style
// wnd_action_icon
//
this.wnd_action_style.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList;
this.wnd_action_style.FormattingEnabled = true;
this.wnd_action_style.Location = new System.Drawing.Point(6, 133);
this.wnd_action_style.Name = "wnd_action_style";
this.wnd_action_style.Size = new System.Drawing.Size(121, 21);
this.wnd_action_style.TabIndex = 24;
this.wnd_action_style.SelectedIndexChanged += new System.EventHandler(this.Wnd_action_style_SelectedIndexChanged);
this.wnd_action_icon.BackColor = System.Drawing.SystemColors.ControlLight;
this.wnd_action_icon.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Stretch;
this.wnd_action_icon.Location = new System.Drawing.Point(217, 106);
this.wnd_action_icon.Name = "wnd_action_icon";
this.wnd_action_icon.Size = new System.Drawing.Size(23, 23);
this.wnd_action_icon.TabIndex = 22;
//
// wnd_select_class_box
//
this.wnd_select_class_box.Location = new System.Drawing.Point(93, 79);
this.wnd_select_class_box.Name = "wnd_select_class_box";
this.wnd_select_class_box.Size = new System.Drawing.Size(104, 20);
this.wnd_select_class_box.TabIndex = 5;
//
// wnd_action_overlay
//
this.wnd_action_overlay.AutoSize = true;
this.wnd_action_overlay.Location = new System.Drawing.Point(139, 110);
this.wnd_action_overlay.Name = "wnd_action_overlay";
this.wnd_action_overlay.Size = new System.Drawing.Size(62, 17);
this.wnd_action_overlay.TabIndex = 25;
this.wnd_action_overlay.Text = "Overlay";
this.wnd_action_overlay.UseVisualStyleBackColor = true;
this.wnd_action_overlay.CheckedChanged += new System.EventHandler(this.wnd_action_overlay_CheckedChanged);
//
// Form1
//
@ -559,7 +560,6 @@
private System.Windows.Forms.ComboBox power_mode_box;
private System.Windows.Forms.Button wnd_select_mouse;
private System.Windows.Forms.Button wnd_action_destroy;
private System.Windows.Forms.Button wnd_action_overlay;
private System.Windows.Forms.Button wnd_action_front;
private System.Windows.Forms.CheckBox wnd_action_enabled;
private System.Windows.Forms.Button wnd_action_title_get;
@ -574,5 +574,6 @@
private System.Windows.Forms.Panel wnd_action_icon;
private System.Windows.Forms.CheckBox wnd_action_visible;
private System.Windows.Forms.ComboBox wnd_action_style;
private System.Windows.Forms.CheckBox wnd_action_overlay;
}
}

View File

@ -1,25 +1,20 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CC_Functions.W32;
using static CC_Functions.W32.Power;
namespace CC_Functions.W32.Test
{
public partial class Form1 : Form
{
MouseHook mHook;
KeyboardHook kHook;
private MouseHook mHook;
private KeyboardHook kHook;
public static Wnd32 tmpWnd;
public static Form1 mainF;
public static Form frm;
public static Label lab;
public Form1()
{
InitializeComponent();
@ -73,6 +68,9 @@ namespace CC_Functions.W32.Test
wnd_select_title_box.Text = tmpWnd.title;
wnd_action_enabled.Checked = tmpWnd.enabled;
wnd_select_selected.Text = "Selected: " + tmpWnd.hWnd.ToString();
wnd_action_style.SelectedIndex = (int)tmpWnd.state;
wnd_select_class_box.Text = tmpWnd.className;
wnd_action_visible.Checked = tmpWnd.shown;
try { wnd_action_icon.BackgroundImage = tmpWnd.icon.ToBitmap(); } catch { wnd_action_icon.BackgroundImage = null; }
try { wnd_action_pos_x_bar.Value = tmpWnd.position.X; } catch { }
try { wnd_action_pos_y_bar.Value = tmpWnd.position.Y; } catch { }
@ -193,9 +191,10 @@ namespace CC_Functions.W32.Test
Application.Exit();
}
bool moving;
Point locDelB;
DateTime mST;
private bool moving;
private Point locDelB;
private DateTime mST;
private void Exit_MouseMove(object sender, MouseEventArgs e)
{
if (moving && (DateTime.Now - mST).TotalSeconds >= 0.1f)
@ -219,5 +218,7 @@ namespace CC_Functions.W32.Test
Enum.TryParse(wnd_action_style.SelectedValue.ToString(), out FormWindowState status);
tmpWnd.state = status;
}
private void wnd_action_overlay_CheckedChanged(object sender, EventArgs e) => tmpWnd.overlay = wnd_action_overlay.Checked;
}
}
}

View File

@ -1,22 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CC_Functions.W32.Test
{
static class Program
internal static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
private static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}
}

View File

@ -1,5 +1,4 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
@ -33,4 +32,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -12,7 +12,7 @@ namespace CC_Functions.W32.Test.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.2.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "16.3.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@ -8,10 +8,11 @@
<OutputType>WinExe</OutputType>
<RootNamespace>CC_Functions.W32.Test</RootNamespace>
<AssemblyName>CC_Functions.W32.Test</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>

View File

@ -1,8 +1,8 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CC_Functions.W32
{
@ -20,7 +20,9 @@ namespace CC_Functions.W32
private const int WM_KEYDOWN = 0x0100;
private static LowLevelKeyboardProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public delegate void keyPress(KeyboardHookEventArgs _args);
public event keyPress OnKeyPress;
public KeyboardHook()
@ -67,4 +69,4 @@ namespace CC_Functions.W32
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}
}

View File

@ -11,6 +11,7 @@ namespace CC_Functions.W32
}
public Keys Key { get; }
public override string ToString() => Key.ToString();
}
}
}

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Drawing;
using System.Runtime.InteropServices;
namespace CC_Functions.W32
{
@ -21,7 +18,9 @@ namespace CC_Functions.W32
private static List<MouseHook> instances = new List<MouseHook>();
private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;
public delegate void mouseEvent(MouseHookEventArgs _args);
public event mouseEvent OnMouse;
public MouseHook()
@ -41,6 +40,7 @@ namespace CC_Functions.W32
}
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0)
@ -55,6 +55,7 @@ namespace CC_Functions.W32
}
private const int WH_MOUSE_LL = 14;
public enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
@ -95,4 +96,4 @@ namespace CC_Functions.W32
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);
}
}
}

View File

@ -3,7 +3,8 @@ using System.Drawing;
namespace CC_Functions.W32
{
public class MouseHookEventArgs : EventArgs {
public class MouseHookEventArgs : EventArgs
{
public MouseHookEventArgs(Point point, MouseHook.MouseMessages message)
{
Point = point;
@ -12,9 +13,10 @@ namespace CC_Functions.W32
public Point Point { get; }
public MouseHook.MouseMessages Message { get; }
public override string ToString()
{
return Message.ToString() + "; " + Point.ToString();
}
}
}
}

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;
using System.Windows.Forms;
namespace CC_Functions.W32
@ -11,7 +6,8 @@ namespace CC_Functions.W32
public static class KeyboardReader
{
[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
static extern short GetKeyState(int keyCode);
private static extern short GetKeyState(int keyCode);
public static bool IsKeyDown(Keys key)
{
try
@ -30,4 +26,4 @@ namespace CC_Functions.W32
}
}
}
}
}

View File

@ -1,9 +1,5 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using static CC_Functions.W32.Privileges;
namespace CC_Functions.W32
@ -11,7 +7,8 @@ namespace CC_Functions.W32
public static class Power
{
[DllImport("ntdll.dll", SetLastError = true)]
static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);
private static extern IntPtr RtlAdjustPrivilege(int Privilege, bool bEnablePrivilege, bool IsThreadPrivilege, out bool PreviousValue);
[DllImport("ntdll.dll")]
private static extern uint NtRaiseHardError(
uint ErrorStatus,
@ -21,9 +18,11 @@ namespace CC_Functions.W32
uint ValidResponseOption,
out uint Response
);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
private static extern bool ExitWindowsEx(ExitWindows uFlags, ShutdownReason dwReason);
[Flags]
public enum ShutdownReason : uint
{
@ -66,19 +65,24 @@ namespace CC_Functions.W32
FlagUserDefined = 0x40000000,
FlagPlanned = 0x80000000
}
[Flags]
public enum ExitWindows : uint
{
// ONE of the following five:
LogOff = 0x00,
ShutDown = 0x01,
Reboot = 0x02,
PowerOff = 0x08,
RestartApps = 0x40,
// plus AT MOST ONE of the following two:
Force = 0x04,
ForceIfHung = 0x10,
}
[Flags]
public enum ShutdownMode : uint
{
@ -89,6 +93,7 @@ namespace CC_Functions.W32
RestartApps = 0x40,
BSoD = 0x29a
}
[Flags]
public enum ShutdownMod : uint
{
@ -96,6 +101,7 @@ namespace CC_Functions.W32
Force = 0x04,
ForceIfHung = 0x10
}
public static unsafe void RaiseEvent(ShutdownMode mode, ShutdownReason reason = ShutdownReason.MinorOther, ShutdownMod mod = ShutdownMod.None)
{
if (mode == ShutdownMode.BSoD)
@ -112,4 +118,4 @@ namespace CC_Functions.W32
}
}
}
}
}

View File

@ -1,11 +1,7 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
namespace CC_Functions.W32
{
@ -146,7 +142,9 @@ namespace CC_Functions.W32
SE_UNDOCK_NAME_TEXT,
SE_UNSOLICITED_INPUT_NAME_TEXT
}
public static SecurityEntity EntityToEntity(SecurityEntity2 entity) => (SecurityEntity)entity;
internal static class NativeMethods
{
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
@ -174,6 +172,7 @@ namespace CC_Functions.W32
internal const uint TOKEN_ADJUST_DEFAULT = 0x0080;
internal const uint TOKEN_ADJUST_SESSIONID = 0x0100;
internal const uint TOKEN_READ = (STANDARD_RIGHTS_READ | TOKEN_QUERY);
internal const uint TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE | TOKEN_QUERY
| TOKEN_QUERY_SOURCE | TOKEN_ADJUST_PRIVILEGES | TOKEN_ADJUST_GROUPS | TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID;
@ -204,4 +203,4 @@ namespace CC_Functions.W32
}
}
}
}
}

View File

@ -1,14 +1,13 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("CC-Functions.W32")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyDescription("W32 Additions for CC-Functions")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCompany("CC24")]
[assembly: AssemblyProduct("CC-Functions.W32")]
[assembly: AssemblyCopyright("Copyright © 2019")]
[assembly: AssemblyTrademark("")]
@ -33,4 +32,4 @@ using System.Runtime.InteropServices;
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -9,9 +9,10 @@
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>CC_Functions.W32</RootNamespace>
<AssemblyName>CC-Functions.W32</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Runtime.ConstrainedExecution;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CC_Functions.W32
@ -13,15 +10,25 @@ namespace CC_Functions.W32
public sealed class Wnd32 : IEquatable<Wnd32>
{
#region Exposed
#region CreateInstance
Wnd32(IntPtr wndref) => hWnd = wndref;
private Wnd32(IntPtr wndref) => hWnd = wndref;
public static Wnd32 fromHandle(IntPtr handle) => new Wnd32(handle);
public static Wnd32 fromMetadata(string lpClassName = null, string lpWindowName = null) => fromHandle(FindWindow(lpClassName, lpWindowName));
public static Wnd32 fromPoint(Point point) => fromHandle(WindowFromPoint(point.X, point.Y));
public static Wnd32 fromForm(Form form) => fromHandle(form.Handle);
public static Wnd32 foreground() => fromHandle(GetForegroundWindow());
#endregion
#endregion CreateInstance
#region InstanceActions
public string title
{
get {
@ -34,6 +41,7 @@ namespace CC_Functions.W32
SetWindowText(hWnd, value);
}
}
public Rectangle position
{
get {
@ -47,6 +55,7 @@ namespace CC_Functions.W32
MoveWindow(hWnd, value.X, value.Y, value.Width, value.Height, true);
}
}
public bool isForeground
{
get {
@ -59,6 +68,7 @@ namespace CC_Functions.W32
throw new InvalidOperationException("You can't set a Window not to be in the foreground. Move another one over it!");
}
}
public bool enabled
{
get {
@ -68,6 +78,7 @@ namespace CC_Functions.W32
EnableWindow(hWnd, value);
}
}
public Icon icon
{
get {
@ -79,8 +90,12 @@ namespace CC_Functions.W32
return Icon.FromHandle(hicon);
}
}
public bool shown
{
get {
return IsWindowVisible(hWnd);
}
set {
if (value)
ShowWindow(hWnd, 9);
@ -88,6 +103,16 @@ namespace CC_Functions.W32
ShowWindow(hWnd, 0);
}
}
public string className
{
get {
StringBuilder ClassName = new StringBuilder(256);
_ = GetClassName(hWnd, ClassName, ClassName.Capacity);
return ClassName.ToString();
}
}
public FormWindowState state
{
get {
@ -111,9 +136,11 @@ namespace CC_Functions.W32
case FormWindowState.Minimized:
ShowWindow(hWnd, 11);
break;
case FormWindowState.Normal:
ShowWindow(hWnd, 1);
break;
case FormWindowState.Maximized:
ShowWindow(hWnd, 3);
break;
@ -121,7 +148,16 @@ namespace CC_Functions.W32
}
}
public void MakeOverlay() => _ = SetWindowPos(hWnd, HWND_TOPMOST, 0, 0, 0, 0, TOPMOST_FLAGS);
public void MakeOverlay() => overlay = true;
public bool overlay
{
set {
Rectangle tmp = position;
_ = SetWindowPos(hWnd, value ? HWND_TOPMOST : HWND_NOTOPMOST, tmp.X, tmp.Y, tmp.Width, tmp.Height, value ? SWP_NOMOVE | SWP_NOSIZE : 0);
}
}
public bool Destroy()
{
if (DestroyWindow(hWnd))
@ -131,60 +167,80 @@ namespace CC_Functions.W32
}
public bool stillExists => IsWindow(hWnd);
public override string ToString() => hWnd.ToString() + "; " + title + "; " + position.ToString();
public override bool Equals(object obj) => Equals(obj as Wnd32);
public bool Equals(Wnd32 other) => other != null && EqualityComparer<IntPtr>.Default.Equals(hWnd, other.hWnd);
public override int GetHashCode() => -75345830 + EqualityComparer<IntPtr>.Default.GetHashCode(hWnd);
public static bool operator ==(Wnd32 left, Wnd32 right) => EqualityComparer<Wnd32>.Default.Equals(left, right);
public static bool operator !=(Wnd32 left, Wnd32 right) => !(left == right);
#endregion
#endregion
#endregion InstanceActions
#endregion Exposed
#region W32
public IntPtr hWnd;
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true)]
static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport("user32.dll", SetLastError = true)]
static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindowEnabled(IntPtr hWnd);
private static extern bool IsWindowVisible(IntPtr hWnd);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Ansi)]
private static extern long GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
private static extern IntPtr WindowFromPoint(int xPoint, int yPoint);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern bool SetWindowText(IntPtr hWnd, string lpString);
[DllImport("user32.dll", SetLastError = true)]
private static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
[DllImport("user32.dll")]
private static extern bool SetForegroundWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern bool EnableWindow(IntPtr hWnd, bool bEnable);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowEnabled(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool DestroyWindow(IntPtr hwnd);
private static extern bool DestroyWindow(IntPtr hwnd);
private static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
private static readonly IntPtr HWND_TOP = new IntPtr(0);
private static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
private const UInt32 SWP_NOSIZE = 0x0001;
private const UInt32 SWP_NOMOVE = 0x0002;
static readonly IntPtr HWND_TOPMOST = new IntPtr(-1);
const UInt32 SWP_NOSIZE = 0x0001;
const UInt32 SWP_NOMOVE = 0x0002;
const UInt32 TOPMOST_FLAGS = SWP_NOMOVE | SWP_NOSIZE;
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
private static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, ExactSpelling = true, SetLastError = true)]
static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
private static extern bool GetWindowRect(IntPtr hWnd, ref RECT rect);
struct RECT
private struct RECT
{
public int left;
public int top;
@ -193,20 +249,21 @@ namespace CC_Functions.W32
}
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool IsWindow(IntPtr hWnd);
private static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
private static extern IntPtr SendMessage(IntPtr hWnd, int msg, int wParam, int lParam);
[DllImport("user32.dll")]
static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
private static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
[DllImport("user32.dll", SetLastError = true)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
#endregion
private static extern int GetWindowLong(IntPtr hWnd, int nIndex);
#endregion W32
}
}
}