Advertisement
Guest User

Untitled

a guest
May 16th, 2018
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.03 KB | None | 0 0
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Imaging;
  4. using System.IO;
  5. using System.Runtime.InteropServices;
  6. using System.Threading;
  7. using EasyHook;
  8. using Tao.OpenGl;
  9. using PixelFormat = System.Drawing.Imaging.PixelFormat;
  10.  
  11. namespace ScreenTaker
  12. {
  13.     public class Main : IEntryPoint
  14.     {
  15.         readonly GlMon.GlMonInterface Interface;
  16.         public LocalHook CreateBufferHook;
  17.  
  18.         public DateTime OldDateTime = DateTime.Now;
  19.         public bool NeedTakeScreen;
  20.  
  21.         public Main(
  22.             RemoteHooking.IContext InContext,
  23.             string InChannelName)
  24.  
  25.         {
  26.             Interface = RemoteHooking.IpcConnectClient<GlMon.GlMonInterface>(InChannelName);
  27.             Interface.Ping();
  28.         }
  29.  
  30.         public void Run(RemoteHooking.IContext InContext,
  31.             string inChannelName)
  32.         {
  33.             if (inChannelName == null) throw new ArgumentNullException(nameof(inChannelName));
  34.             try
  35.             {
  36.                 CreateBufferHook = LocalHook.Create(LocalHook.GetProcAddress("opengl32.dll", "wglSwapBuffers"),
  37.                     new DwglSwapBuffers(SwapBuffers_Hooked), this);
  38.  
  39.                 CreateBufferHook.ThreadACL.SetExclusiveACL(new[] {0});
  40.             }
  41.             catch (Exception extInfo)
  42.             {
  43.                 Interface.ReportException(extInfo);
  44.                 return;
  45.             }
  46.  
  47.             Interface.IsInstalled(RemoteHooking.GetCurrentProcessId());
  48.  
  49.             RemoteHooking.WakeUpProcess();
  50.  
  51.             while (true)
  52.             {
  53.                 if (DateTime.Now > OldDateTime + new TimeSpan(0, 0, 2))
  54.                 {
  55.                     NeedTakeScreen = true;
  56.                 }
  57.                 Interface.Ping();
  58.                 Thread.Sleep(100);
  59.             }
  60.         }
  61.  
  62.         [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
  63.         delegate IntPtr DwglSwapBuffers(IntPtr hdc);
  64.  
  65.  
  66.  
  67.         [DllImport("opengl32.dll", CharSet = CharSet.Unicode, SetLastError = true,
  68.             CallingConvention = CallingConvention.StdCall)]
  69.         static extern IntPtr wglSwapBuffers(IntPtr hdc);
  70.  
  71.  
  72.         // this is where we are intercepting all file accesses!
  73.         static IntPtr SwapBuffers_Hooked(IntPtr hdc)
  74.         {
  75.             Main This = (Main) HookRuntimeInfo.Callback;
  76.             try
  77.             {
  78.                 /** apitrace dump is
  79.                  *                                      
  80.                     11288 wglSwapBuffers(hdc = 0xb0013e0a) = TRUE
  81.                     11289 glFlush()
  82.                     11290 wglMakeCurrent(hdc = 0xf601374b, hglrc = 0x10004) = TRUE
  83.                     11291 glDisable(cap = GL_SCISSOR_TEST)
  84.                     11292 glClear(mask = GL_COLOR_BUFFER_BIT)
  85.                     11293 glEnable(cap = GL_SCISSOR_TEST)
  86.                     11294 glDisable(cap = GL_BLEND)
  87.                     11295 glBindTexture(target = GL_TEXTURE_2D, texture = 20)
  88.                     11296 glPushClientAttrib(mask = GL_CLIENT_VERTEX_ARRAY_BIT)
  89.                     11297 glPushAttrib(mask = GL_TRANSFORM_BIT)
  90.                     11298 glMatrixMode(mode = GL_PROJECTION)
  91.                     11299 glPushMatrix()
  92.                     11300 glLoadIdentity()
  93.                     11301 glGetIntegerv(pname = GL_VIEWPORT, params = {0, 0, 860, 720})
  94.                     11302 glOrtho(left = 0, right = 860, bottom = 0, top = 720, zNear = 0, zFar = -1)
  95.                     11303 glMatrixMode(mode = GL_TEXTURE)
  96.                     11304 glPushMatrix()
  97.                     11305 glLoadIdentity()
  98.                     11306 glMatrixMode(mode = GL_MODELVIEW)
  99.                     11307 glPushMatrix()
  100.                                     * **/
  101.                 if (This.NeedTakeScreen)
  102.                 {
  103.                     int[] glViewport = new int[4];
  104.                     Gl.glGetIntegerv(Gl.GL_VIEWPORT, glViewport);
  105.                     Bitmap bitmap = new Bitmap(glViewport[2], glViewport[3]);
  106.                     Rectangle bounds = new Rectangle(0, 0, glViewport[2], glViewport[3]);
  107.                     BitmapData bmpData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite,
  108.                         PixelFormat.Format32bppRgb);
  109.                     Gl.glReadBuffer(Gl.GL_FRONT);
  110.                     Gl.glReadPixels(0, 0, bounds.Width, bounds.Height, Gl.GL_BGR,
  111.                         Gl.GL_UNSIGNED_BYTE, bmpData.Scan0);
  112.                     bitmap.UnlockBits(bmpData);
  113.  
  114.                     using (var fs = new FileStream(Path.GetTempPath() + "SCT_screenshot.png", FileMode.Create))
  115.                     {
  116.                         bitmap.Save(fs, ImageFormat.Png);
  117.                         fs.Flush();
  118.                         fs.Close();
  119.                     }
  120.                     This.NeedTakeScreen = false;
  121.                     This.OldDateTime = DateTime.Now;
  122.                 }
  123.             }
  124.             catch (Exception ex)
  125.             {
  126.                 This.Interface.ReportException(ex);
  127.             }
  128.             return wglSwapBuffers(hdc);
  129.         }
  130.     }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement