You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

8 月之前
8 月之前
8 月之前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. var injectionDONE = MessageBox.Show("Injection Finish");
  62. }
  63. catch (Exception e)
  64. {
  65. var injectionFAIL = MessageBox.Show($"Injection FAIL WITH ERROR {e.Message}");
  66. throw new InjectionFailedException(e);
  67. }
  68. HookManager.AddHookedProcess(process.Id);
  69. Process = process;
  70. // Ensure the target process is in the foreground,
  71. // this prevents an issue where the target app appears to be in
  72. // the foreground but does not receive any user inputs.
  73. // Note: the first Alt+Tab out of the target application after injection
  74. // may still be an issue - switching between windowed and
  75. // fullscreen fixes the issue however (see ScreenshotInjection.cs for another option)
  76. BringProcessWindowToFront();
  77. }
  78. public CaptureInterface CaptureInterface
  79. {
  80. get { return _serverInterface; }
  81. }
  82. ~CaptureProcess()
  83. {
  84. Dispose(false);
  85. }
  86. #region Private methods
  87. /// <summary>
  88. /// Bring the target window to the front and wait for it to be visible
  89. /// </summary>
  90. /// <remarks>If the window does not come to the front within approx. 30 seconds an exception is raised</remarks>
  91. public void BringProcessWindowToFront()
  92. {
  93. if (this.Process == null)
  94. return;
  95. IntPtr handle = this.Process.MainWindowHandle;
  96. int i = 0;
  97. while (!NativeMethods.IsWindowInForeground(handle))
  98. {
  99. if (i == 0)
  100. {
  101. // Initial sleep if target window is not in foreground - just to let things settle
  102. Thread.Sleep(250);
  103. }
  104. if (NativeMethods.IsIconic(handle))
  105. {
  106. // Minimized so send restore
  107. NativeMethods.ShowWindow(handle, NativeMethods.WindowShowStyle.Restore);
  108. }
  109. else
  110. {
  111. // Already Maximized or Restored so just bring to front
  112. NativeMethods.SetForegroundWindow(handle);
  113. }
  114. Thread.Sleep(250);
  115. // Check if the target process main window is now in the foreground
  116. if (NativeMethods.IsWindowInForeground(handle))
  117. {
  118. // Leave enough time for screen to redraw
  119. Thread.Sleep(1000);
  120. return;
  121. }
  122. // Prevent an infinite loop
  123. if (i > 120) // about 30secs
  124. {
  125. throw new Exception("Could not set process window to the foreground");
  126. }
  127. i++;
  128. }
  129. }
  130. #endregion
  131. #region IDispose
  132. private bool _disposed = false;
  133. public void Dispose()
  134. {
  135. Dispose(true);
  136. GC.SuppressFinalize(this);
  137. }
  138. protected virtual void Dispose(bool disposing)
  139. {
  140. if (!_disposed)
  141. {
  142. if (disposing)
  143. {
  144. // Disconnect the IPC (which causes the remote entry point to exit)
  145. _serverInterface.Disconnect();
  146. }
  147. _disposed = true;
  148. }
  149. }
  150. #endregion
  151. }
  152. }