Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

CaptureProcess.cs 6.6 KiB

8 miesięcy temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using EasyHook;
  3. using System.Runtime.Remoting;
  4. using System.Runtime.Remoting.Channels.Ipc;
  5. using RenderHookAPI.Interface;
  6. using System.Diagnostics;
  7. using System.Threading;
  8. using RenderHookAPI.Hook;
  9. using System.Windows.Forms;
  10. namespace RenderHookAPI
  11. {
  12. public class CaptureProcess : IDisposable
  13. {
  14. /// <summary>
  15. /// Must be null to allow a random channel name to be generated
  16. /// </summary>
  17. string _channelName = null;
  18. private IpcServerChannel _screenshotServer;
  19. private CaptureInterface _serverInterface;
  20. public Process Process { get; set; }
  21. /// <summary>
  22. /// Prepares capturing in the target process. Note that the process must not already be hooked, and must have a <see cref="Process.MainWindowHandle"/>.
  23. /// </summary>
  24. /// <param name="process">The process to inject into</param>
  25. /// <exception cref="ProcessHasNoWindowHandleException">Thrown if the <paramref name="process"/> does not have a window handle. This could mean that the process does not have a UI, or that the process has not yet finished starting.</exception>
  26. /// <exception cref="ProcessAlreadyHookedException">Thrown if the <paramref name="process"/> is already hooked</exception>
  27. /// <exception cref="InjectionFailedException">Thrown if the injection failed - see the InnerException for more details.</exception>
  28. /// <remarks>The target process will have its main window brought to the foreground after successful injection.</remarks>
  29. public CaptureProcess(Process process, CaptureConfig config, CaptureInterface captureInterface)
  30. {
  31. // If the process doesn't have a mainwindowhandle yet, skip it (we need to be able to get the hwnd to set foreground etc)
  32. if (process.MainWindowHandle == IntPtr.Zero)
  33. {
  34. throw new ProcessHasNoWindowHandleException();
  35. }
  36. // Skip if the process is already hooked (and we want to hook multiple applications)
  37. if (HookManager.IsHooked(process.Id))
  38. {
  39. throw new ProcessAlreadyHookedException();
  40. }
  41. captureInterface.ProcessId = process.Id;
  42. _serverInterface = captureInterface;
  43. //_serverInterface = new CaptureInterface() { ProcessId = process.Id };
  44. // Initialise the IPC server (with our instance of _serverInterface)
  45. _screenshotServer = RemoteHooking.IpcCreateServer<CaptureInterface>(
  46. ref _channelName,
  47. WellKnownObjectMode.Singleton,
  48. _serverInterface);
  49. try
  50. {
  51. // Inject DLL into target process
  52. RemoteHooking.Inject(
  53. process.Id,
  54. InjectionOptions.Default,
  55. typeof(CaptureInterface).Assembly.Location,//"Capture.dll", // 32-bit version (the same because AnyCPU) could use different assembly that links to 32-bit C++ helper dll
  56. typeof(CaptureInterface).Assembly.Location, //"Capture.dll", // 64-bit version (the same because AnyCPU) could use different assembly that links to 64-bit C++ helper dll
  57. // the optional parameter list...
  58. _channelName, // The name of the IPC channel for the injected assembly to connect to
  59. config
  60. );
  61. }
  62. catch (Exception e)
  63. {
  64. throw new InjectionFailedException(e);
  65. }
  66. HookManager.AddHookedProcess(process.Id);
  67. Process = process;
  68. // Ensure the target process is in the foreground,
  69. // this prevents an issue where the target app appears to be in
  70. // the foreground but does not receive any user inputs.
  71. // Note: the first Alt+Tab out of the target application after injection
  72. // may still be an issue - switching between windowed and
  73. // fullscreen fixes the issue however (see ScreenshotInjection.cs for another option)
  74. BringProcessWindowToFront();
  75. }
  76. public CaptureInterface CaptureInterface
  77. {
  78. get { return _serverInterface; }
  79. }
  80. ~CaptureProcess()
  81. {
  82. Dispose(false);
  83. }
  84. #region Private methods
  85. /// <summary>
  86. /// Bring the target window to the front and wait for it to be visible
  87. /// </summary>
  88. /// <remarks>If the window does not come to the front within approx. 30 seconds an exception is raised</remarks>
  89. public void BringProcessWindowToFront()
  90. {
  91. if (this.Process == null)
  92. return;
  93. IntPtr handle = this.Process.MainWindowHandle;
  94. int i = 0;
  95. while (!NativeMethods.IsWindowInForeground(handle))
  96. {
  97. if (i == 0)
  98. {
  99. // Initial sleep if target window is not in foreground - just to let things settle
  100. Thread.Sleep(250);
  101. }
  102. if (NativeMethods.IsIconic(handle))
  103. {
  104. // Minimized so send restore
  105. NativeMethods.ShowWindow(handle, NativeMethods.WindowShowStyle.Restore);
  106. }
  107. else
  108. {
  109. // Already Maximized or Restored so just bring to front
  110. NativeMethods.SetForegroundWindow(handle);
  111. }
  112. Thread.Sleep(250);
  113. // Check if the target process main window is now in the foreground
  114. if (NativeMethods.IsWindowInForeground(handle))
  115. {
  116. // Leave enough time for screen to redraw
  117. Thread.Sleep(1000);
  118. return;
  119. }
  120. // Prevent an infinite loop
  121. if (i > 120) // about 30secs
  122. {
  123. throw new Exception("Could not set process window to the foreground");
  124. }
  125. i++;
  126. }
  127. }
  128. #endregion
  129. #region IDispose
  130. private bool _disposed = false;
  131. public void Dispose()
  132. {
  133. Dispose(true);
  134. GC.SuppressFinalize(this);
  135. }
  136. protected virtual void Dispose(bool disposing)
  137. {
  138. if (!_disposed)
  139. {
  140. if (disposing)
  141. {
  142. // Disconnect the IPC (which causes the remote entry point to exit)
  143. _serverInterface.Disconnect();
  144. }
  145. _disposed = true;
  146. }
  147. }
  148. #endregion
  149. }
  150. }