/ src / modules / PowerOCR / PowerOCR / Helpers / OSInterop.cs
OSInterop.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  using System;
  6  using System.Runtime.InteropServices;
  7  
  8  namespace PowerOCR;
  9  
 10  public static class OSInterop
 11  {
 12  #pragma warning disable SA1310 // Field names should not contain underscore
 13      public const int WH_KEYBOARD_LL = 13;
 14      public const int VK_SHIFT = 0x10;
 15      public const int VK_CONTROL = 0x11;
 16      public const int VK_MENU = 0x12;
 17      public const int VK_LWIN = 0x5B;
 18      public const int VK_RWIN = 0x5C;
 19      public const int VK_ESCAPE = 0x1B;
 20      public const int WM_HOTKEY = 0x0312;
 21      public const int WM_KEYDOWN = 0x0100;
 22      public const int WM_KEYUP = 0x0101;
 23  
 24  #pragma warning disable CA1401 // P/Invokes should not be visible
 25      [DllImport("user32.dll")]
 26      public static extern int GetSystemMetrics(int smIndex);
 27  
 28      public const int SM_CMONITORS = 80;
 29  
 30  #pragma warning restore SA1310 // Field names should not contain underscore
 31  
 32      [DllImport("user32.dll")]
 33      public static extern bool SystemParametersInfo(int nAction, int nParam, ref RECT rc, int nUpdate);
 34  
 35      [DllImport("user32.dll", CharSet = CharSet.Auto)]
 36      public static extern bool GetMonitorInfo(HandleRef hmonitor, [In, Out] MONITORINFOEX info);
 37  
 38      [DllImport("user32.dll")]
 39      public static extern IntPtr MonitorFromWindow(HandleRef handle, int flags);
 40  
 41      [DllImport("user32.dll")]
 42      public static extern bool ClipCursor(ref RECT lpRect);
 43  
 44      [DllImport("user32.dll")]
 45      public static extern bool ClipCursor([In] IntPtr lpRect);
 46  
 47      [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
 48      internal static extern bool FreeLibrary(IntPtr hModule);
 49  
 50      [DllImport("user32.dll")]
 51      internal static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk);
 52  
 53      [DllImport("user32.dll")]
 54      internal static extern bool UnregisterHotKey(IntPtr hWnd, int id);
 55  
 56      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
 57      internal static extern bool UnhookWindowsHookEx(IntPtr idHook);
 58  
 59      [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
 60      internal static extern IntPtr LoadLibrary(string lpFileName);
 61  
 62      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
 63      internal static extern IntPtr SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hMod, int dwThreadId);
 64  
 65      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
 66      internal static extern IntPtr CallNextHookEx(IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam);
 67  
 68      [DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall, SetLastError = true)]
 69      internal static extern short GetAsyncKeyState(int vKey);
 70  
 71      public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);
 72  
 73      [DllImport("user32.dll", SetLastError = true)]
 74      public static extern IntPtr SetForegroundWindow(IntPtr hWnd);
 75  
 76      [DllImport("user32.dll")]
 77      public static extern IntPtr GetForegroundWindow();
 78  
 79      [DllImport("user32.dll")]
 80      public static extern uint GetWindowThreadProcessId(IntPtr hWnd, IntPtr processId);
 81  
 82      [DllImport("user32.dll")]
 83      public static extern bool AttachThreadInput(uint idAttach, uint idAttachTo, bool fAttach);
 84  
 85  #pragma warning restore CA1401 // P/Invokes should not be visible
 86      [StructLayout(LayoutKind.Sequential)]
 87      internal struct LowLevelKeyboardInputEvent
 88      {
 89          /// <summary>
 90          /// A virtual-key code. The code must be a value in the range 1 to 254.
 91          /// </summary>
 92          public int VirtualCode;
 93  
 94          /// <summary>
 95          /// A hardware scan code for the key.
 96          /// </summary>
 97          public int HardwareScanCode;
 98  
 99          /// <summary>
100          /// The extended-key flag, event-injected Flags, context code, and transition-state flag. This member is specified as follows. An application can use the following values to test the keystroke Flags. Testing LLKHF_INJECTED (bit 4) will tell you whether the event was injected. If it was, then testing LLKHF_LOWER_IL_INJECTED (bit 1) will tell you whether or not the event was injected from a process running at lower integrity level.
101          /// </summary>
102          public int Flags;
103  
104          /// <summary>
105          /// The time stamp for this message, equivalent to what GetMessageTime would return for this message.
106          /// </summary>
107          public int TimeStamp;
108  
109          /// <summary>
110          /// Additional information associated with the message.
111          /// </summary>
112          public IntPtr AdditionalInformation;
113      }
114  
115      public struct RECT
116      {
117          public int Left;
118          public int Top;
119          public int Right;
120          public int Bottom;
121  
122          public int Width => Right - Left;
123  
124          public int Height => Bottom - Top;
125      }
126  
127      [StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Auto)]
128      public class MONITORINFOEX
129      {
130          public int CbSize { get; set; } = Marshal.SizeOf(typeof(MONITORINFOEX));
131  
132          public RECT RcMonitor { get; set; }
133  
134          public RECT RcWork { get; set; }
135  
136          [MarshalAs(UnmanagedType.ByValArray, SizeConst = 32)]
137          private char[] szDevice = new char[32];
138  
139          public int DwFlags { get; set; }
140      }
141  }