2using System.Collections.Generic;
4using System.Runtime.InteropServices;
6using System.Threading.Tasks;
7using System.Diagnostics;
13 [DllImport(
"User32.dll")]
14 public static extern short GetAsyncKeyState(System.Windows.Forms.Keys vKey);
15 public const int WM_KEYDOWN = 0x100;
16 public const int WM_KEYUP = 0x101;
24 public static bool GetKeyState(System.Windows.Forms.Keys vKey)
26 int si = (int)GetAsyncKeyState(vKey);
33 [DllImport(
"user32.dll")]
34 public static extern IntPtr GetForegroundWindow();
36 [DllImport(
"user32.dll")]
37 public static extern int GetWindowText(IntPtr hWnd, StringBuilder text,
int count);
38 [DllImport(
"user32.dll")]
39 public static extern bool SetForegroundWindow(IntPtr hWnd);
40 [DllImport(
"user32.dll")]
41 public static extern void mouse_event(
int dwFlags,
int dx,
int dy,
int dwData,
int dwExtraInfo);
43 [StructLayout(LayoutKind.Sequential)]
51 public const int MOUSEEVENTF_ABSOLUTE = 0x8000;
52 public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
53 public const int MOUSEEVENTF_LEFTUP = 0x0004;
54 public const int MOUSEEVENTF_MIDDLEDOWN = 0x0020;
55 public const int MOUSEEVENTF_MIDDLEUP=0x0040;
56 public const int MOUSEEVENTF_MOVE=0x0001;
57 public const int MOUSEEVENTF_RIGHTDOWN=0x0008;
58 public const int MOUSEEVENTF_RIGHTUP=0x0010;
59 public const int MOUSEEVENTF_WHEEL = 0x0800;
60 public const int MOUSEEVENTF_XDOWN=0x0080;
61 public const int MOUSEEVENTF_XUP=0x0100;
62 public const int MOUSEEVENTF_HWHEEL=0x01000;
64 [DllImport(
"user32.dll")]
65 public static extern bool GetWindowRect(IntPtr hwnd, ref
Rect rectangle);
70 mouse_event(MOUSEEVENTF_WHEEL, 0, 0, 120, 0);
75 mouse_event(MOUSEEVENTF_WHEEL, 0, 0, -120, 0);
83 Process pr = Process.GetProcessesByName(process)[0];
84 SetForegroundWindow(pr.MainWindowHandle);
92 const int nChars = 256;
93 StringBuilder Buff =
new StringBuilder(nChars);
94 IntPtr handle = GetForegroundWindow();
96 if (GetWindowText(handle, Buff, nChars) > 0)
98 return Buff.ToString();
108 public enum MouseEventFlags
110 LeftDown = 0x00000002,
112 MiddleDown = 0x00000020,
113 MiddleUp = 0x00000040,
115 Absolute = 0x00008000,
116 RightDown = 0x00000008,
120 [DllImport(
"user32.dll", EntryPoint =
"SetCursorPos")]
121 [
return: MarshalAs(UnmanagedType.Bool)]
122 private static extern bool SetCursorPos(
int x,
int y);
124 [DllImport(
"user32.dll")]
125 [
return: MarshalAs(UnmanagedType.Bool)]
126 private static extern bool GetCursorPos(out
MousePoint lpMousePoint);
128 [DllImport(
"user32.dll")]
129 private static extern void mouse_event(
int dwFlags,
int dx,
int dy,
int dwData,
int dwExtraInfo);
131 public static void LeftClick()
133 MouseEvent(MouseEventFlags.LeftDown | MouseEventFlags.LeftUp);
135 public static void RightClick()
137 MouseEvent(MouseEventFlags.RightDown | MouseEventFlags.RightUp);
139 public static void LeftDown()
143 public static void RightDown()
147 public static void LeftUp()
151 public static void RightUp()
170 SetCursorPos(point.X, point.Y);
180 var gotPoint = GetCursorPos(out currentMousePoint);
181 if (!gotPoint) { currentMousePoint =
new MousePoint(0, 0); }
182 return currentMousePoint;
201 [StructLayout(LayoutKind.Sequential)]
214 static public class FileUtil
216 [StructLayout(LayoutKind.Sequential)]
217 struct RM_UNIQUE_PROCESS
219 public int dwProcessId;
220 public System.Runtime.InteropServices.ComTypes.FILETIME ProcessStartTime;
223 const int RmRebootReasonNone = 0;
224 const int CCH_RM_MAX_APP_NAME = 255;
225 const int CCH_RM_MAX_SVC_NAME = 63;
238 [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
239 struct RM_PROCESS_INFO
241 public RM_UNIQUE_PROCESS Process;
243 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_APP_NAME + 1)]
244 public string strAppName;
246 [MarshalAs(UnmanagedType.ByValTStr, SizeConst = CCH_RM_MAX_SVC_NAME + 1)]
247 public string strServiceShortName;
249 public RM_APP_TYPE ApplicationType;
250 public uint AppStatus;
251 public uint TSSessionId;
252 [MarshalAs(UnmanagedType.Bool)]
253 public bool bRestartable;
256 [DllImport(
"rstrtmgr.dll", CharSet = CharSet.Unicode)]
257 static extern int RmRegisterResources(uint pSessionHandle,
259 string[] rgsFilenames,
260 UInt32 nApplications,
261 [In] RM_UNIQUE_PROCESS[] rgApplications,
263 string[] rgsServiceNames);
265 [DllImport(
"rstrtmgr.dll", CharSet = CharSet.Auto)]
266 static extern int RmStartSession(out uint pSessionHandle,
int dwSessionFlags,
string strSessionKey);
268 [DllImport(
"rstrtmgr.dll")]
269 static extern int RmEndSession(uint pSessionHandle);
271 [DllImport(
"rstrtmgr.dll")]
272 static extern int RmGetList(uint dwSessionHandle,
273 out uint pnProcInfoNeeded,
275 [In, Out] RM_PROCESS_INFO[] rgAffectedApps,
276 ref uint lpdwRebootReasons);
277 static public List<Process> Locking(
string path)
280 string key = Guid.NewGuid().ToString();
281 List<Process> processes =
new List<Process>();
283 int res = RmStartSession(out handle, 0, key);
286 throw new Exception(
"Could not begin restart session. Unable to determine file locker.");
290 const int ERROR_MORE_DATA = 234;
291 uint pnProcInfoNeeded = 0,
293 lpdwRebootReasons = RmRebootReasonNone;
295 string[] resources =
new string[] { path };
297 res = RmRegisterResources(handle, (uint)resources.Length, resources, 0,
null, 0,
null);
300 throw new Exception(
"Could not register resource.");
305 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo,
null, ref lpdwRebootReasons);
307 if (res == ERROR_MORE_DATA)
310 RM_PROCESS_INFO[] processInfo =
new RM_PROCESS_INFO[pnProcInfoNeeded];
311 pnProcInfo = pnProcInfoNeeded;
314 res = RmGetList(handle, out pnProcInfoNeeded, ref pnProcInfo, processInfo, ref lpdwRebootReasons);
318 processes =
new List<Process>((
int)pnProcInfo);
322 for (
int i = 0; i < pnProcInfo; i++)
326 processes.Add(Process.GetProcessById(processInfo[i].Process.dwProcessId));
329 catch (ArgumentException) { }
333 throw new Exception(
"Could not list processes locking resource.");
336 throw new Exception(
"Could not list processes locking resource. Failed to get size of result.");
340 RmEndSession(handle);
static void SetCursorPosition(int x, int y)
static void SetCursorPosition(MousePoint point)
static void MouseEvent(MouseEventFlags value)
static MousePoint GetCursorPosition()
static void MouseWheelDown()
MouseWheelDown() simulates a mouse wheel down event.
static bool GetKeyState(System.Windows.Forms.Keys vKey)
static void MouseWheelUp()
"MouseWheelUp()" is a function that simulates a mouse wheel up event.
static string GetActiveWindowTitle()
static void SetFocus(string process)