/ src / modules / MouseWithoutBorders / App / Class / NativeMethods.cs
NativeMethods.cs
   1  // Copyright (c) Microsoft Corporation
   2  // The Microsoft Corporation licenses this file to you under the MIT license.
   3  // See the LICENSE file in the project root for more information.
   4  
   5  // <summary>
   6  //     Windows APIs.
   7  // </summary>
   8  // <history>
   9  //     2008 created by Truong Do (ductdo).
  10  //     2009-... modified by Truong Do (TruongDo).
  11  //     2023- Included in PowerToys.
  12  // </history>
  13  using System;
  14  using System.Diagnostics.CodeAnalysis;
  15  using System.Drawing;
  16  using System.Runtime.InteropServices;
  17  using System.Text;
  18  
  19  // We are sure we dont have managed resource in KEYBDINPUT, IntPtr just holds a value
  20  [module: SuppressMessage("Microsoft.Design", "CA1049:TypesThatOwnNativeResourcesShouldBeDisposable", Scope = "type", Target = "MouseWithoutBorders.NativeMethods+KEYBDINPUT", Justification = "Dotnet port with style preservation")]
  21  
  22  // Some other minor issues
  23  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#ConvertStringSidToSid(System.String,System.IntPtr&)", MessageId = "0", Justification = "Dotnet port with style preservation")]
  24  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#DrawText(System.IntPtr,System.String,System.Int32,MouseWithoutBorders.NativeMethods+RECT&,System.UInt32)", MessageId = "1", Justification = "Dotnet port with style preservation")]
  25  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#SetWindowText(System.IntPtr,System.String)", MessageId = "1", Justification = "Dotnet port with style preservation")]
  26  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#FindWindow(System.String,System.String)", MessageId = "0", Justification = "Dotnet port with style preservation")]
  27  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#FindWindow(System.String,System.String)", MessageId = "1", Justification = "Dotnet port with style preservation")]
  28  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#GetWindowText(System.IntPtr,System.Text.StringBuilder,System.Int32)", MessageId = "1", Justification = "Dotnet port with style preservation")]
  29  [module: SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#keybd_event(System.Byte,System.Byte,System.UInt32,System.Int32)", MessageId = "3", Justification = "Dotnet port with style preservation")]
  30  [module: SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#SendMessage(System.IntPtr,System.Int32,System.IntPtr,System.IntPtr)", MessageId = "return", Justification = "Dotnet port with style preservation")]
  31  [module: SuppressMessage("Microsoft.Reliability", "CA2006:UseSafeHandleToEncapsulateNativeResources", Scope = "member", Target = "MouseWithoutBorders.NativeMethods+KEYBDINPUT.#dwExtraInfo", Justification = "Dotnet port with style preservation")]
  32  [module: SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Scope = "member", Target = "MouseWithoutBorders.NativeMethods+INPUT64.#type", Justification = "Dotnet port with style preservation")]
  33  [module: SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#TerminateProcess(System.IntPtr,System.IntPtr)", MessageId = "1", Justification = "Dotnet port with style preservation")]
  34  [module: SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#GetClassName(System.IntPtr,System.Text.StringBuilder,System.Int32)", MessageId = "1", Justification = "Dotnet port with style preservation")]
  35  [module: SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#GetClassName(System.IntPtr,System.Text.StringBuilder,System.Int32)", MessageId = "return", Justification = "Dotnet port with style preservation")]
  36  [module: SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#GetAsyncKeyState(System.IntPtr)", MessageId = "0", Justification = "Dotnet port with style preservation")]
  37  [module: SuppressMessage("Microsoft.Portability", "CA1901:PInvokeDeclarationsShouldBePortable", Scope = "member", Target = "MouseWithoutBorders.NativeMethods.#GetAsyncKeyState(System.IntPtr)", MessageId = "return", Justification = "Dotnet port with style preservation")]
  38  
  39  // Disable the warning to preserve original code
  40  #pragma warning disable CA1716
  41  namespace MouseWithoutBorders.Class
  42  #pragma warning restore CA1716
  43  {
  44      internal partial class NativeMethods
  45      {
  46  #if !MM_HELPER
  47  
  48          [DllImport("user32.dll")]
  49          internal static extern IntPtr SetClipboardViewer(IntPtr hWndNewViewer);
  50  
  51          [DllImport("user32.dll", CharSet = CharSet.Auto)]
  52          [return: MarshalAs(UnmanagedType.Bool)]
  53          internal static extern bool ChangeClipboardChain(IntPtr hWndRemove, IntPtr hWndNewNext);
  54  
  55          [DllImport("user32.dll", CharSet = CharSet.Auto)]
  56          internal static extern IntPtr SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
  57  
  58          [DllImport("user32.dll")]
  59          internal static extern IntPtr GetForegroundWindow();
  60  
  61          [DllImport("user32.dll")]
  62          [return: MarshalAs(UnmanagedType.Bool)]
  63          internal static extern bool SetForegroundWindow(IntPtr hWnd);
  64  
  65          [DllImport("user32.dll")]
  66          internal static extern IntPtr CreateIconIndirect(ref IconInfo icon);
  67  
  68          [return: MarshalAs(UnmanagedType.Bool)]
  69          [DllImport("user32.dll", SetLastError = true)]
  70          internal static extern bool SetProcessDPIAware();
  71  
  72          [DllImport("Shcore.dll", SetLastError = true)]
  73          internal static extern int SetProcessDpiAwareness(uint type); // Win 8.1 and up, DPI can be per monitor.
  74  
  75          [DllImport("kernel32.dll")]
  76          internal static extern uint WTSGetActiveConsoleSessionId();
  77  
  78          [DllImport("Wtsapi32.dll")]
  79          internal static extern bool WTSQuerySessionInformation(IntPtr hServer, int sessionId, WTSInfoClass infoClass, out IntPtr ppBuffer, out int pBytesReturned);
  80  
  81          [DllImport("Wtsapi32.dll")]
  82          internal static extern void WTSFreeMemory(IntPtr pointer);
  83  
  84          internal enum WTSInfoClass
  85          {
  86              WTSInitialProgram,
  87              WTSApplicationName,
  88              WTSWorkingDirectory,
  89              WTSOEMId,
  90              WTSSessionId,
  91              WTSUserName,
  92              WTSWinStationName,
  93              WTSDomainName,
  94              WTSConnectState,
  95              WTSClientBuildNumber,
  96              WTSClientName,
  97              WTSClientDirectory,
  98              WTSClientProductId,
  99              WTSClientHardwareId,
 100              WTSClientAddress,
 101              WTSClientDisplay,
 102              WTSClientProtocolType,
 103              WTSIdleTime,
 104              WTSLogonTime,
 105              WTSIncomingBytes,
 106              WTSOutgoingBytes,
 107              WTSIncomingFrames,
 108              WTSOutgoingFrames,
 109              WTSClientInfo,
 110              WTSSessionInfo,
 111          }
 112  
 113  #endif
 114  
 115          [DllImport("user32.dll", SetLastError = true)]
 116          internal static extern bool ChangeWindowMessageFilterEx(IntPtr hWnd, uint msg, int action, IntPtr changeInfo);
 117  
 118          [DllImport("user32.dll", CharSet = CharSet.Auto)]
 119          [return: MarshalAs(UnmanagedType.Bool)]
 120          internal static extern bool PostMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);
 121  
 122          [DllImport("user32.dll", SetLastError = false)]
 123          internal static extern IntPtr GetDesktopWindow();
 124  
 125          [LibraryImport("user32.dll")]
 126          internal static partial IntPtr GetShellWindow();
 127  
 128          [DllImport("user32.dll")]
 129          internal static extern IntPtr GetWindowDC(IntPtr hWnd);
 130  
 131          [LibraryImport("user32.dll")]
 132          [return: MarshalAs(UnmanagedType.Bool)]
 133          internal static partial bool GetWindowRect(IntPtr hWnd, out RECT rect);
 134  
 135          [DllImport("user32.dll", CharSet = CharSet.Unicode)]
 136          internal static extern int DrawText(IntPtr hDC, string lpString, int nCount, ref RECT lpRect, uint uFormat);
 137  
 138          [DllImport("gdi32.dll")]
 139          internal static extern uint SetTextColor(IntPtr hdc, int crColor);
 140  
 141          [DllImport("gdi32.dll")]
 142          internal static extern uint SetBkColor(IntPtr hdc, int crColor);
 143  
 144          [DllImport("user32.dll")]
 145          internal static extern int ReleaseDC(IntPtr hWnd, IntPtr hDC);
 146  
 147          [DllImport("user32.dll", CharSet = CharSet.Unicode)]
 148          internal static extern IntPtr GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
 149  
 150          [DllImport("user32.dll", SetLastError = true)]
 151          [return: MarshalAs(UnmanagedType.Bool)]
 152          internal static extern bool AddClipboardFormatListener(IntPtr hwnd);
 153  
 154          [DllImport("user32.dll", SetLastError = true)]
 155          [return: MarshalAs(UnmanagedType.Bool)]
 156          internal static extern bool RemoveClipboardFormatListener(IntPtr hwnd);
 157  
 158          /*
 159          internal const int SW_MAXIMIZE = 3;
 160  
 161          [StructLayout(LayoutKind.Sequential)]
 162          internal struct WINDOWPLACEMENT
 163          {
 164              public int length;
 165              public int flags;
 166              public int showCmd;
 167              public System.Drawing.Point ptMinPosition;
 168              public System.Drawing.Point ptMaxPosition;
 169              public System.Drawing.Rectangle rcNormalPosition;
 170  
 171              public static WINDOWPLACEMENT Default
 172              {
 173                  get
 174                  {
 175                      WINDOWPLACEMENT result = new WINDOWPLACEMENT();
 176                      result.length = Marshal.SizeOf(result);
 177                      return result;
 178                  }
 179              }
 180          }
 181  
 182          [DllImport("user32.dll")]
 183          [return: MarshalAs(UnmanagedType.Bool)]
 184          internal static extern bool GetWindowPlacement(IntPtr hWnd, ref WINDOWPLACEMENT lpwndpl);
 185           * */
 186  
 187          [DllImport("user32.dll")]
 188          [return: MarshalAs(UnmanagedType.Bool)]
 189          internal static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
 190  
 191          [DllImport("user32.dll")]
 192          [return: MarshalAs(UnmanagedType.Bool)]
 193          internal static extern bool SetWindowPos(IntPtr hWnd, IntPtr hWndInsertAfter, int X, int Y, int cx, int cy, uint uFlags);
 194  
 195          [DllImport("user32.dll", SetLastError = true)]
 196          internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);
 197  
 198          [DllImport("user32.dll", CharSet = CharSet.Unicode)]
 199          [return: MarshalAs(UnmanagedType.Bool)]
 200          internal static extern bool SetWindowText(IntPtr hWnd, string lpString);
 201  
 202          [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
 203          internal static extern int GetWindowTextLength(IntPtr hWnd);
 204  
 205          [DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
 206          internal static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
 207  
 208          [DllImport("user32.dll", CharSet = CharSet.Auto)]
 209          [return: MarshalAs(UnmanagedType.Bool)]
 210          internal static extern bool DestroyIcon(IntPtr handle);
 211  
 212          // [DllImport("user32.dll")]
 213          // [return: MarshalAs(UnmanagedType.Bool)]
 214          // internal static extern bool IsWindowVisible(IntPtr hWnd);
 215  
 216          // [DllImport("user32")]
 217          // internal static extern int GetKeyboardState(byte[] pbKeyState);
 218  
 219          // [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
 220          // internal static extern short GetKeyState(int vKey);
 221          [DllImport("user32")]
 222          [return: MarshalAs(UnmanagedType.Bool)]
 223          internal static extern bool SetCursorPos(int X, int Y);
 224  
 225          [DllImport("user32", SetLastError = true)]
 226          [return: MarshalAs(UnmanagedType.Bool)]
 227          internal static extern bool GetCursorPos(ref Point p);
 228  
 229          [DllImport("advapi32", SetLastError = true)]
 230          [return: MarshalAs(UnmanagedType.Bool)]
 231          internal static extern bool OpenProcessToken(IntPtr ProcessHandle, int DesiredAccess, ref IntPtr TokenHandle);
 232  
 233          [DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 234          [return: MarshalAs(UnmanagedType.Bool)]
 235          internal static extern bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
 236  
 237          [DllImport("advapi32.dll", EntryPoint = "DuplicateTokenEx")]
 238          [return: MarshalAs(UnmanagedType.Bool)]
 239          internal static extern bool DuplicateTokenEx(
 240              IntPtr ExistingTokenHandle,
 241              uint dwDesiredAccess,
 242              ref SECURITY_ATTRIBUTES lpThreadAttributes,
 243              int TokenType,
 244              int ImpersonationLevel,
 245              ref IntPtr DuplicateTokenHandle);
 246  
 247          [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
 248          [return: MarshalAs(UnmanagedType.Bool)]
 249          internal static extern bool ConvertStringSidToSid(string StringSid, out IntPtr ptrSid);
 250  
 251          [DllImport("advapi32.dll", SetLastError = true)]
 252          [return: MarshalAs(UnmanagedType.Bool)]
 253          internal static extern bool SetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, ref TOKEN_MANDATORY_LABEL TokenInformation, uint TokenInformationLength);
 254  
 255          // [DllImport("advapi32.dll", SetLastError = true)]
 256          // [return: MarshalAs(UnmanagedType.Bool)]
 257          // internal static extern bool SetTokenInformation(IntPtr TokenHandle, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, UInt32 TokenInformationLength);
 258          [SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", Justification = "Dotnet port with style preservation")]
 259          [DllImport("advapi32.dll", EntryPoint = "CreateProcessAsUser", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.StdCall)]
 260          [return: MarshalAs(UnmanagedType.Bool)]
 261          internal static extern bool CreateProcessAsUser(
 262              IntPtr hToken,
 263              string lpApplicationName,
 264              string lpCommandLine,
 265              ref SECURITY_ATTRIBUTES lpProcessAttributes,
 266              ref SECURITY_ATTRIBUTES lpThreadAttributes,
 267              [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
 268              int dwCreationFlags,
 269              IntPtr lpEnvironment,
 270              string lpCurrentDirectory,
 271              ref STARTUPINFO lpStartupInfo,
 272              out PROCESS_INFORMATION lpProcessInformation);
 273  
 274          [DllImport("user32.dll")]
 275          [return: MarshalAs(UnmanagedType.Bool)]
 276          internal static extern bool EnumDisplayMonitors(
 277              IntPtr hdc,
 278              IntPtr lprcClip,
 279              EnumMonitorsDelegate lpfnEnum,
 280              IntPtr dwData);
 281  
 282          [DllImport("user32.dll")]
 283          [return: MarshalAs(UnmanagedType.Bool)]
 284          internal static extern bool GetMonitorInfo(IntPtr hMonitor, ref MonitorInfoEx lpmi);
 285  
 286          [DllImport("User32.dll", CharSet = CharSet.Unicode)]
 287          internal static extern int FindWindow(string ClassName, string WindowName);
 288  
 289          [DllImport("kernel32.dll")]
 290          internal static extern uint GetCurrentThreadId();
 291  
 292          [DllImport("user32.dll", SetLastError = true)]
 293          internal static extern IntPtr GetThreadDesktop(uint dwThreadId);
 294  
 295          [DllImport("user32.dll")]
 296          internal static extern short GetAsyncKeyState(IntPtr vKey); // Keys vKey
 297  
 298          [DllImport("user32.dll", SetLastError = true)]
 299          internal static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
 300  
 301          [LibraryImport("kernel32.dll",
 302              EntryPoint = "QueryFullProcessImageNameW",
 303              SetLastError = true,
 304              StringMarshalling = StringMarshalling.Utf16)]
 305          [return: MarshalAs(UnmanagedType.Bool)]
 306          internal static partial bool QueryFullProcessImageName(
 307              IntPtr hProcess, QUERY_FULL_PROCESS_NAME_FLAGS dwFlags, [Out] char[] lpExeName, ref uint lpdwSize);
 308  
 309          [LibraryImport("shell32.dll", SetLastError = true)]
 310          internal static partial int SHQueryUserNotificationState(out USER_NOTIFICATION_STATE state);
 311  
 312          [StructLayout(LayoutKind.Sequential)]
 313          internal struct POINT
 314          {
 315              internal int x;
 316              internal int y;
 317          }
 318  
 319          [StructLayout(LayoutKind.Sequential)]
 320          internal struct CURSORINFO
 321          {
 322              public int cbSize;
 323              public int flags;
 324              public IntPtr hCursor;
 325              public POINT ptScreenPos;
 326          }
 327  
 328          [DllImport("user32.dll")]
 329          internal static extern bool GetCursorInfo(out CURSORINFO ci);
 330  
 331  #if CUSTOMIZE_LOGON_SCREEN
 332          [DllImport("kernel32", SetLastError = true)]
 333          internal static extern uint WaitForSingleObject(IntPtr handle, int milliseconds);
 334  
 335          internal const uint WAIT_OBJECT_0 = 0x00000000;
 336  
 337          [StructLayout(LayoutKind.Sequential)]
 338          internal struct PROCESS_BASIC_INFORMATION
 339          {
 340              public int ExitStatus;
 341              public int PebBaseAddress;
 342              public int AffinityMask;
 343              public int BasePriority;
 344              public uint UniqueProcessId;
 345              public uint InheritedFromUniqueProcessId;
 346          }
 347  
 348          [DllImport("kernel32.dll")]
 349          [return: MarshalAs(UnmanagedType.Bool)]
 350          internal static extern bool TerminateProcess(IntPtr hProcess, IntPtr exitCode);
 351  
 352          [DllImport("ntdll.dll")]
 353          internal static extern int NtQueryInformationProcess(
 354              IntPtr hProcess,
 355              int processInformationClass /* 0 */,
 356              ref PROCESS_BASIC_INFORMATION processBasicInformation,
 357              uint processInformationLength,
 358              out uint returnLength);
 359  #endif
 360  
 361  #if USE_GetSecurityDescriptorSacl
 362          internal enum SE_OBJECT_TYPE
 363          {
 364              SE_UNKNOWN_OBJECT_TYPE = 0,
 365              SE_FILE_OBJECT,
 366              SE_SERVICE,
 367              SE_PRINTER,
 368              SE_REGISTRY_KEY,
 369              SE_LMSHARE,
 370              SE_KERNEL_OBJECT,
 371              SE_WINDOW_OBJECT,
 372              SE_DS_OBJECT,
 373              SE_DS_OBJECT_ALL,
 374              SE_PROVIDER_DEFINED_OBJECT,
 375              SE_WMIGUID_OBJECT,
 376              SE_REGISTRY_WOW64_32KEY
 377          }
 378  
 379          [Flags]
 380          internal enum SECURITY_INFORMATION : uint
 381          {
 382              LABEL_SECURITY_INFORMATION = 0x00000010
 383          }
 384  
 385          [StructLayoutAttribute(LayoutKind.Explicit)]
 386          internal struct SECURITY_DESCRIPTOR
 387          {
 388              [FieldOffset(0)]
 389              public byte revision;
 390  
 391              [FieldOffset(1)]
 392              public byte size;
 393  
 394              [FieldOffset(2)]
 395              public short control;
 396  
 397              [FieldOffset(4)]
 398              public IntPtr owner;
 399  
 400              [FieldOffset(8)]
 401              public IntPtr group;
 402  
 403              [FieldOffset(12)]
 404              public IntPtr sacl;
 405  
 406              [FieldOffset(16)]
 407              public IntPtr dacl;
 408          }
 409  
 410          [StructLayout(LayoutKind.Sequential)]
 411          internal struct ACL { public byte AclRevision; public byte Sbz1; public int AclSize; public int AceCount; public int Sbz2; }
 412  
 413          [DllImport("advapi32", SetLastError = true)]
 414          [return: MarshalAs(UnmanagedType.Bool)]
 415          internal static extern bool ConvertStringSecurityDescriptorToSecurityDescriptor(string StringSecurityDescriptor,
 416          UInt32 StringSDRevision, out SECURITY_DESCRIPTOR SecurityDescriptor, out UInt64 SecurityDescriptorSize);
 417  
 418          [DllImport("advapi32.dll", SetLastError = true)]
 419          internal static extern int GetSecurityDescriptorSacl([MarshalAs(UnmanagedType.Struct)] ref SECURITY_DESCRIPTOR pSecurityDescriptor, int lpbSaclPresent, [MarshalAs(UnmanagedType.Struct)] ref ACL pSacl, int lpbSaclDefaulted);
 420  
 421          [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
 422          internal static extern uint SetNamedSecurityInfo(
 423              string pObjectName,
 424              SE_OBJECT_TYPE ObjectType,
 425              SECURITY_INFORMATION SecurityInfo,
 426              IntPtr psidOwner,
 427              IntPtr psidGroup,
 428              IntPtr pDacl,
 429              IntPtr pSacl);
 430  #endif
 431  
 432  #if SINGLE_PROCESS
 433          [DllImport("user32.dll", SetLastError = true)]
 434          [return: MarshalAs(UnmanagedType.Bool)]
 435          internal static extern bool SetThreadDesktop(IntPtr hDesktop);
 436  #endif
 437  
 438          [DllImport("user32.dll", SetLastError = true)]
 439          internal static extern IntPtr OpenInputDesktop(uint dwFlags, [MarshalAs(UnmanagedType.Bool)] bool fInherit, uint dwDesiredAccess);
 440  
 441          [DllImport("user32.dll", SetLastError = true)]
 442          [return: MarshalAs(UnmanagedType.Bool)]
 443          internal static extern bool GetUserObjectInformation(IntPtr hObj, int nIndex, [Out] byte[] pvInfo, int nLength, out uint lpnLengthNeeded);
 444  
 445          // [DllImport("user32.dll")]
 446          // [return: MarshalAs(UnmanagedType.Bool)]
 447          // internal static extern bool GetIconInfo(IntPtr hIcon, ref IconInfo pIconInfo);
 448  
 449          // [DllImport("gdi32.dll")]
 450          // internal static extern uint GetPixel(IntPtr hdc, int nXPos, int nYPos);
 451          [DllImport("gdi32.dll")]
 452          internal static extern uint SetPixel(IntPtr hdc, int X, int Y, uint crColor);
 453  
 454          // internal const int WM_CLOSE = 16;
 455          internal const int WM_SHOW_DRAG_DROP = 0x400;
 456  
 457          internal const int WM_HIDE_DRAG_DROP = 0x401;
 458          internal const int WM_CHECK_EXPLORER_DRAG_DROP = 0x402;
 459          internal const int WM_QUIT = 0x403;
 460          internal const int WM_SWITCH = 0x404;
 461          internal const int WM_HIDE_DD_HELPER = 0x405;
 462          internal const int WM_SHOW_SETTINGS_FORM = 0x406;
 463  
 464          internal static readonly IntPtr HWND_TOPMOST = new(-1);
 465  
 466          // internal static readonly IntPtr HWND_NOTOPMOST = new IntPtr(-2);
 467          // internal static readonly IntPtr HWND_TOP = new IntPtr(0);
 468          // internal static readonly IntPtr HWND_BOTTOM = new IntPtr(1);
 469          internal const uint SWP_NOSIZE = 0x0001;
 470  
 471          internal const uint SWP_NOMOVE = 0x0002;
 472          internal const uint SWP_NOZORDER = 0x0004;
 473          internal const uint SWP_NOREDRAW = 0x0008;
 474          internal const uint SWP_SHOWWINDOW = 0x0040;
 475          internal const uint SWP_HIDEWINDOW = 0x0080;
 476  
 477          internal const int UOI_FLAGS = 1;
 478          internal const int UOI_NAME = 2;
 479          internal const int UOI_TYPE = 3;
 480          internal const int UOI_USER_SID = 4;
 481          internal const uint DESKTOP_WRITEOBJECTS = 0x0080;
 482          internal const uint DESKTOP_READOBJECTS = 0x0001;
 483          internal const uint DF_ALLOWOTHERACCOUNTHOOK = 0x0001;
 484  
 485          // internal const UInt32 GENERIC_READ                     = 0x80000000;
 486          internal const uint GENERIC_WRITE = 0x40000000;
 487  
 488          // internal const UInt32 GENERIC_EXECUTE                  = 0x20000000;
 489          internal const uint GENERIC_ALL = 0x10000000;
 490  
 491          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
 492          internal struct RECT
 493          {
 494              internal int Left;
 495              internal int Top;
 496              internal int Right;
 497              internal int Bottom;
 498          }
 499  
 500          // size of a device name string
 501          internal const int CCHDEVICENAME = 32;
 502  
 503          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
 504          internal struct MonitorInfoEx
 505          {
 506              internal int cbSize;
 507              internal RECT rcMonitor;
 508              internal RECT rcWork;
 509              internal uint dwFlags;
 510  
 511              // [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCHDEVICENAME)]
 512              // internal string szDeviceName;
 513          }
 514  
 515          // We are WOW
 516          [DllImport(
 517              "user32.dll",
 518              CharSet = CharSet.Auto,
 519              CallingConvention = CallingConvention.StdCall,
 520              SetLastError = true)]
 521          internal static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
 522  
 523          [DllImport(
 524              "user32.dll",
 525              CharSet = CharSet.Auto,
 526              CallingConvention = CallingConvention.StdCall,
 527              SetLastError = true)]
 528          internal static extern int UnhookWindowsHookEx(int idHook);
 529  
 530          // In X64, we are running WOW
 531          [DllImport(
 532              "user32.dll",
 533              CharSet = CharSet.Auto,
 534              CallingConvention = CallingConvention.StdCall)]
 535          internal static extern int CallNextHookEx(int idHook, int nCode, int wParam, IntPtr lParam);
 536  
 537          // [DllImport("user32")]
 538          // internal static extern int ToAscii(int uVirtKey, int uScanCode, byte[] lpbKeyState, byte[] lpwTransKey, int fuState);
 539          private enum InputType
 540          {
 541              INPUT_MOUSE = 0,
 542              INPUT_KEYBOARD = 1,
 543              INPUT_HARDWARE = 2,
 544          }
 545  
 546          [Flags]
 547          internal enum MOUSEEVENTF
 548          {
 549              MOVE = 0x0001,
 550              LEFTDOWN = 0x0002,
 551              LEFTUP = 0x0004,
 552              RIGHTDOWN = 0x0008,
 553              RIGHTUP = 0x0010,
 554              MIDDLEDOWN = 0x0020,
 555              MIDDLEUP = 0x0040,
 556              XDOWN = 0x0080,
 557              XUP = 0x0100,
 558              WHEEL = 0x0800,
 559              HWHEEL = 0x1000,
 560              VIRTUALDESK = 0x4000,
 561              ABSOLUTE = 0x8000,
 562          }
 563  
 564          [Flags]
 565          internal enum KEYEVENTF
 566          {
 567              KEYDOWN = 0x0000,
 568              EXTENDEDKEY = 0x0001,
 569              KEYUP = 0x0002,
 570              UNICODE = 0x0004,
 571              SCANCODE = 0x0008,
 572          }
 573  
 574          // http://msdn.microsoft.com/en-us/library/ms646273(VS.85).aspx
 575          [StructLayout(LayoutKind.Sequential)]
 576          internal struct MOUSEINPUT
 577          {
 578              internal int dx;
 579              internal int dy;
 580              internal int mouseData;
 581              internal int dwFlags;
 582              internal int time;
 583              internal IntPtr dwExtraInfo;
 584          }
 585  
 586          [StructLayout(LayoutKind.Sequential)]
 587          internal struct KEYBDINPUT
 588          {
 589              internal short wVk;
 590              internal short wScan;
 591              internal int dwFlags;
 592              internal int time;
 593              internal IntPtr dwExtraInfo;
 594          }
 595  
 596          [StructLayout(LayoutKind.Sequential)]
 597          internal struct HARDWAREINPUT
 598          {
 599              internal int uMsg;
 600              internal short wParamL;
 601              internal short wParamH;
 602          }
 603  
 604          [StructLayout(LayoutKind.Explicit)]
 605          internal struct INPUT
 606          {
 607              [FieldOffset(0)]
 608              internal int type;
 609  
 610              [FieldOffset(4)]
 611              internal MOUSEINPUT mi;
 612  
 613              [FieldOffset(4)]
 614              internal KEYBDINPUT ki;
 615          }
 616  
 617          [StructLayout(LayoutKind.Explicit)]
 618          internal struct INPUT64
 619          {
 620              [FieldOffset(0)]
 621              internal int type;
 622  
 623              [FieldOffset(8)]
 624              internal MOUSEINPUT mi;
 625  
 626              [FieldOffset(8)]
 627              internal KEYBDINPUT ki;
 628          }
 629  
 630          [DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
 631          internal static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);
 632  
 633          [DllImport("user32.dll", EntryPoint = "SendInput", SetLastError = true)]
 634          internal static extern uint SendInput64(uint nInputs, INPUT64[] pInputs, int cbSize);
 635  
 636          internal static bool InjectMouseInputAvailable { get; set; }
 637  
 638          [DllImport("user32.dll", EntryPoint = "GetMessageExtraInfo", SetLastError = true)]
 639          internal static extern IntPtr GetMessageExtraInfo();
 640  
 641          [DllImport("user32.dll", EntryPoint = "LockWorkStation", SetLastError = true)]
 642          internal static extern uint LockWorkStation();
 643  
 644          // [DllImport("user32.dll")]
 645          // internal static extern void keybd_event(byte bVk, byte bScan, UInt32 dwFlags, int dwExtraInfo);
 646          [DllImport("user32.dll")]
 647          internal static extern uint MapVirtualKey(uint uCode, uint uMapType);
 648  
 649          [StructLayout(LayoutKind.Sequential)]
 650          internal struct LUID
 651          {
 652              internal int LowPart;
 653              internal int HighPart;
 654          } // end struct
 655  
 656          [StructLayout(LayoutKind.Sequential)]
 657          internal struct LUID_AND_ATTRIBUTES
 658          {
 659              internal LUID Luid;
 660              internal int Attributes;
 661          } // end struct
 662  
 663          [StructLayout(LayoutKind.Sequential)]
 664          internal struct TOKEN_PRIVILEGES
 665          {
 666              internal int PrivilegeCount;
 667  
 668              // LUID_AND_ATTRIBUTES
 669              [MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
 670              internal int[] Privileges;
 671          }
 672  
 673          internal const int READ_CONTROL = 0x00020000;
 674  
 675          internal const int STANDARD_RIGHTS_REQUIRED = 0x000F0000;
 676  
 677          internal const int STANDARD_RIGHTS_READ = READ_CONTROL;
 678          internal const int STANDARD_RIGHTS_WRITE = READ_CONTROL;
 679          internal const int STANDARD_RIGHTS_EXECUTE = READ_CONTROL;
 680  
 681          internal const int STANDARD_RIGHTS_ALL = 0x001F0000;
 682  
 683          internal const int SPECIFIC_RIGHTS_ALL = 0x0000FFFF;
 684  
 685          internal const int TOKEN_IMPERSONATE = 0x0004;
 686          internal const int TOKEN_QUERY_SOURCE = 0x0010;
 687          internal const int TOKEN_ADJUST_PRIVILEGES = 0x0020;
 688          internal const int TOKEN_ADJUST_GROUPS = 0x0040;
 689          internal const int TOKEN_ADJUST_SESSIONID = 0x0100;
 690  
 691          internal const int TOKEN_ALL_ACCESS_P = STANDARD_RIGHTS_REQUIRED |
 692              TOKEN_ASSIGN_PRIMARY |
 693              TOKEN_DUPLICATE |
 694              TOKEN_IMPERSONATE |
 695              TOKEN_QUERY |
 696              TOKEN_QUERY_SOURCE |
 697              TOKEN_ADJUST_PRIVILEGES |
 698              TOKEN_ADJUST_GROUPS |
 699              TOKEN_ADJUST_DEFAULT;
 700  
 701          internal const int TOKEN_ALL_ACCESS = TOKEN_ALL_ACCESS_P | TOKEN_ADJUST_SESSIONID;
 702  
 703          internal const int TOKEN_READ = STANDARD_RIGHTS_READ | TOKEN_QUERY;
 704  
 705          internal const int TOKEN_WRITE = STANDARD_RIGHTS_WRITE |
 706              TOKEN_ADJUST_PRIVILEGES |
 707              TOKEN_ADJUST_GROUPS |
 708              TOKEN_ADJUST_DEFAULT;
 709  
 710          internal const int TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE;
 711  
 712          internal const int CREATE_NEW_PROCESS_GROUP = 0x00000200;
 713          internal const int CREATE_UNICODE_ENVIRONMENT = 0x00000400;
 714  
 715          internal const int IDLE_PRIORITY_CLASS = 0x40;
 716          internal const int NORMAL_PRIORITY_CLASS = 0x20;
 717          internal const int HIGH_PRIORITY_CLASS = 0x80;
 718          internal const int REALTIME_PRIORITY_CLASS = 0x100;
 719  
 720          internal const int CREATE_NEW_CONSOLE = 0x00000010;
 721  
 722          internal const string SE_DEBUG_NAME = "SeDebugPrivilege";
 723          internal const string SE_RESTORE_NAME = "SeRestorePrivilege";
 724          internal const string SE_BACKUP_NAME = "SeBackupPrivilege";
 725  
 726          internal const int SE_PRIVILEGE_ENABLED = 0x0002;
 727  
 728          internal const int ERROR_NOT_ALL_ASSIGNED = 1300;
 729  
 730          [StructLayout(LayoutKind.Sequential)]
 731          internal struct PROCESSENTRY32
 732          {
 733              internal uint dwSize;
 734              internal uint cntUsage;
 735              internal uint th32ProcessID;
 736              internal IntPtr th32DefaultHeapID;
 737              internal uint th32ModuleID;
 738              internal uint cntThreads;
 739              internal uint th32ParentProcessID;
 740              internal int pcPriClassBase;
 741              internal uint dwFlags;
 742  
 743              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
 744              internal string szExeFile;
 745          }
 746  
 747          internal const uint TH32CS_SNAPPROCESS = 0x00000002;
 748  
 749          // internal static int INVALID_HANDLE_VALUE = -1;
 750          [DllImport("kernel32.dll", SetLastError = true)]
 751          [return: MarshalAs(UnmanagedType.Bool)]
 752          internal static extern bool CloseHandle(IntPtr hSnapshot);
 753  
 754          [StructLayout(LayoutKind.Sequential)]
 755          internal struct SECURITY_ATTRIBUTES
 756          {
 757              internal int Length;
 758              internal IntPtr lpSecurityDescriptor;
 759              internal bool bInheritHandle;
 760          }
 761  
 762          [StructLayout(LayoutKind.Sequential)]
 763          internal struct PROCESS_INFORMATION
 764          {
 765              internal IntPtr hProcess;
 766              internal IntPtr hThread;
 767              internal uint dwProcessId;
 768              internal uint dwThreadId;
 769          }
 770  
 771          [StructLayout(LayoutKind.Sequential)]
 772          internal struct STARTUPINFO
 773          {
 774              internal int cb;
 775              internal string lpReserved;
 776              internal string lpDesktop;
 777              internal string lpTitle;
 778              internal uint dwX;
 779              internal uint dwY;
 780              internal uint dwXSize;
 781              internal uint dwYSize;
 782              internal uint dwXCountChars;
 783              internal uint dwYCountChars;
 784              internal uint dwFillAttribute;
 785              internal uint dwFlags;
 786              internal short wShowWindow;
 787              internal short cbReserved2;
 788              internal IntPtr lpReserved2;
 789              internal IntPtr hStdInput;
 790              internal IntPtr hStdOutput;
 791              internal IntPtr hStdError;
 792          }
 793  
 794          [StructLayout(LayoutKind.Sequential)]
 795          internal struct SID_AND_ATTRIBUTES
 796          {
 797              internal IntPtr Sid;
 798              internal int Attributes;
 799          }
 800  
 801          [StructLayout(LayoutKind.Sequential)]
 802          internal struct TOKEN_MANDATORY_LABEL
 803          {
 804              internal SID_AND_ATTRIBUTES Label;
 805          }
 806  
 807          internal const int TOKEN_DUPLICATE = 0x0002;
 808          internal const int TOKEN_QUERY = 0x0008;
 809          internal const int TOKEN_ADJUST_DEFAULT = 0x0080;
 810          internal const int TOKEN_ASSIGN_PRIMARY = 0x0001;
 811          internal const uint MAXIMUM_ALLOWED = 0x2000000;
 812          internal const int SE_GROUP_INTEGRITY = 0x00000020;
 813  
 814          internal enum SECURITY_IMPERSONATION_LEVEL : int
 815          {
 816              SecurityAnonymous = 0,
 817              SecurityIdentification = 1,
 818              SecurityImpersonation = 2,
 819              SecurityDelegation = 3,
 820          }
 821  
 822          internal enum TOKEN_TYPE : int
 823          {
 824              TokenPrimary = 1,
 825              TokenImpersonation = 2,
 826          }
 827  
 828          internal enum TOKEN_INFORMATION_CLASS : int
 829          {
 830              TokenUser = 1,
 831              TokenGroups,
 832              TokenPrivileges,
 833              TokenOwner,
 834              TokenPrimaryGroup,
 835              TokenDefaultDacl,
 836              TokenSource,
 837              TokenType,
 838              TokenImpersonationLevel,
 839              TokenStatistics,
 840              TokenRestrictedSids,
 841              TokenSessionId,
 842              TokenGroupsAndPrivileges,
 843              TokenSessionReference,
 844              TokenSandBoxInert,
 845              TokenAuditPolicy,
 846              TokenOrigin,
 847              TokenElevationType,
 848              TokenLinkedToken,
 849              TokenElevation,
 850              TokenHasRestrictions,
 851              TokenAccessInformation,
 852              TokenVirtualizationAllowed,
 853              TokenVirtualizationEnabled,
 854              TokenIntegrityLevel,
 855              TokenUIAccess,
 856              TokenMandatoryPolicy,
 857              TokenLogonSid,
 858              MaxTokenInfoClass,
 859          }
 860  
 861          // [DllImport("kernel32.dll")]
 862          // internal static extern int Process32First(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
 863  
 864          // [DllImport("kernel32.dll")]
 865          // internal static extern int Process32Next(IntPtr hSnapshot, ref PROCESSENTRY32 lppe);
 866  
 867          // [DllImport("kernel32.dll", SetLastError = true)]
 868          // internal static extern IntPtr CreateToolhelp32Snapshot(UInt32 dwFlags, UInt32 th32ProcessID);
 869          [DllImport("Wtsapi32.dll", SetLastError = true)]
 870          internal static extern uint WTSQueryUserToken(uint SessionId, ref IntPtr phToken);
 871  
 872          [SuppressMessage("Microsoft.Globalization", "CA2101:SpecifyMarshalingForPInvokeStringArguments", MessageId = "1", Justification = "Dotnet port with style preservation")]
 873          [DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
 874          [return: MarshalAs(UnmanagedType.Bool)]
 875          internal static extern bool LookupPrivilegeValue(IntPtr lpSystemName, string lpname, [MarshalAs(UnmanagedType.Struct)] ref LUID lpLuid);
 876  
 877          // [DllImport("kernel32.dll")]
 878          // [return: MarshalAs(UnmanagedType.Bool)]
 879          // static extern bool ProcessIdToSessionId(UInt32 dwProcessId, ref UInt32 pSessionId);
 880          [DllImport("kernel32.dll")]
 881          internal static extern IntPtr OpenProcess(uint dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, uint dwProcessId);
 882  
 883          [DllImport("advapi32.dll", SetLastError = true)]
 884          [return: MarshalAs(UnmanagedType.Bool)]
 885          internal static extern bool AdjustTokenPrivileges(IntPtr TokenHandle, [MarshalAs(UnmanagedType.Bool)] bool DisableAllPrivileges, ref TOKEN_PRIVILEGES NewState, int BufferLength, IntPtr PreviousState, IntPtr ReturnLength);
 886  
 887          [DllImport("userenv.dll", SetLastError = true)]
 888          [return: MarshalAs(UnmanagedType.Bool)]
 889          internal static extern bool CreateEnvironmentBlock(ref IntPtr lpEnvironment, IntPtr hToken, [MarshalAs(UnmanagedType.Bool)] bool bInherit);
 890  
 891          [DllImport("advapi32.dll", SetLastError = true)]
 892          [return: MarshalAs(UnmanagedType.Bool)]
 893          internal static extern bool ImpersonateLoggedOnUser(IntPtr hToken);
 894  
 895          [DllImport("advapi32.dll", SetLastError = true)]
 896          [return: MarshalAs(UnmanagedType.Bool)]
 897          internal static extern bool RevertToSelf();
 898  
 899          internal delegate bool EnumMonitorsDelegate(IntPtr hMonitor, IntPtr hdcMonitor, ref RECT lprcMonitor, IntPtr dwData);
 900  
 901          internal delegate int HookProc(int nCode, int wParam, IntPtr lParam);
 902  
 903          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
 904          internal class MEMORYSTATUSEX
 905          {
 906              public uint dwLength;
 907              public uint dwMemoryLoad;
 908              public ulong ullTotalPhys;
 909              public ulong ullAvailPhys;
 910              public ulong ullTotalPageFile;
 911              public ulong ullAvailPageFile;
 912              public ulong ullTotalVirtual;
 913              public ulong ullAvailVirtual;
 914              public ulong ullAvailExtendedVirtual;
 915  
 916              public MEMORYSTATUSEX()
 917              {
 918                  dwLength = (uint)Marshal.SizeOf(typeof(MEMORYSTATUSEX));
 919              }
 920          }
 921  
 922          [return: MarshalAs(UnmanagedType.Bool)]
 923          [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
 924          internal static extern bool GlobalMemoryStatusEx([In, Out] MEMORYSTATUSEX lpBuffer);
 925  
 926          /*
 927          [DllImport("Netapi32.dll", CharSet = CharSet.Unicode, ExactSpelling = true)]
 928          internal extern static int NetUserGetInfo([MarshalAs(UnmanagedType.LPWStr)] string ServerName,
 929              [MarshalAs(UnmanagedType.LPWStr)] string UserName, int level,out IntPtr BufPtr);
 930  
 931          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
 932          internal struct USER_INFO_10
 933          {
 934              [MarshalAs(UnmanagedType.LPWStr)]
 935              public string usri10_name;
 936              [MarshalAs(UnmanagedType.LPWStr)]
 937              public string usri10_comment;
 938              [MarshalAs(UnmanagedType.LPWStr)]
 939              public string usri10_usr_comment;
 940              [MarshalAs(UnmanagedType.LPWStr)]
 941              public string usri10_full_name;
 942          }
 943  
 944          [DllImport("Netapi32.dll", SetLastError = true)]
 945          internal static extern int NetApiBufferFree(IntPtr Buffer);
 946          */
 947  
 948          internal enum EXTENDED_NAME_FORMAT
 949          {
 950              NameUnknown = 0,
 951              NameFullyQualifiedDN = 1,
 952              NameSamCompatible = 2,
 953              NameDisplay = 3,
 954              NameUniqueId = 6,
 955              NameCanonical = 7,
 956              NameUserPrincipal = 8,
 957              NameCanonicalEx = 9,
 958              NameServicePrincipal = 10,
 959              NameDnsDomain = 12,
 960          }
 961  
 962          internal enum MONITOR_FROM_WINDOW_FLAGS : uint
 963          {
 964              DEFAULT_TO_NULL = 0x00000000,
 965              DEFAULT_TO_PRIMARY = 0x00000001,
 966              DEFAULT_TO_NEAREST = 0x00000002,
 967          }
 968  
 969          internal enum QUERY_FULL_PROCESS_NAME_FLAGS : uint
 970          {
 971              DEFAULT = 0x00000000,
 972              PROCESS_NAME_NATIVE = 0x00000001,
 973          }
 974  
 975          internal enum USER_NOTIFICATION_STATE
 976          {
 977              NOT_PRESENT = 1,
 978              BUSY = 2,
 979              RUNNING_D3D_FULL_SCREEN = 3,
 980              PRESENTATION_MODE = 4,
 981              ACCEPTS_NOTIFICATIONS = 5,
 982              QUIET_TIME = 6,
 983              APP = 7,
 984          }
 985  
 986          [DllImport("secur32.dll", CharSet = CharSet.Unicode)]
 987          [return: MarshalAs(UnmanagedType.I1)]
 988          internal static extern bool GetUserNameEx(int nameFormat, StringBuilder userName, ref uint userNameSize);
 989  
 990          [DllImport("Shcore.dll", SetLastError = true)]
 991          internal static extern int GetDpiForMonitor(IntPtr hMonitor, uint dpiType, out uint dpiX, out uint dpiY);
 992  
 993          private static string GetDNSDomain()
 994          {
 995              if (Environment.OSVersion.Platform != PlatformID.Win32NT)
 996              {
 997                  return null;
 998              }
 999  
1000              StringBuilder userName = new(1024);
1001              uint userNameSize = (uint)userName.Capacity;
1002  
1003              if (GetUserNameEx((int)EXTENDED_NAME_FORMAT.NameDnsDomain, userName, ref userNameSize))
1004              {
1005                  string[] nameParts = userName.ToString()
1006                      .Split('\\');
1007                  return nameParts.Length != 2 ? null : nameParts[0];
1008              }
1009  
1010              return null;
1011          }
1012  
1013          /// <summary>
1014          /// Use this method to figure out if your code is running on a Microsoft computer.
1015          /// </summary>
1016          /// <returns>True if running on a Microsoft computer; otherwise, false.</returns>
1017          internal static bool IsRunningAtMicrosoft()
1018          {
1019              string domain = GetDNSDomain();
1020  
1021              return !string.IsNullOrEmpty(domain) && domain.EndsWith("microsoft.com", true, System.Globalization.CultureInfo.CurrentCulture);
1022          }
1023  
1024          private NativeMethods()
1025          {
1026          }
1027      }
1028  }