Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.IO;
- using System.Runtime.InteropServices;
- using System.Threading;
- using EasyHook;
- using Tao.OpenGl;
- using PixelFormat = System.Drawing.Imaging.PixelFormat;
- namespace ScreenTaker
- {
- public class Main : IEntryPoint
- {
- readonly GlMon.GlMonInterface Interface;
- public LocalHook CreateBufferHook;
- public DateTime OldDateTime = DateTime.Now;
- public bool NeedTakeScreen;
- public Main(
- RemoteHooking.IContext InContext,
- string InChannelName)
- {
- Interface = RemoteHooking.IpcConnectClient<GlMon.GlMonInterface>(InChannelName);
- Interface.Ping();
- }
- public void Run(RemoteHooking.IContext InContext,
- string inChannelName)
- {
- if (inChannelName == null) throw new ArgumentNullException(nameof(inChannelName));
- try
- {
- CreateBufferHook = LocalHook.Create(LocalHook.GetProcAddress("opengl32.dll", "wglSwapBuffers"),
- new DwglSwapBuffers(SwapBuffers_Hooked), this);
- CreateBufferHook.ThreadACL.SetExclusiveACL(new[] {0});
- }
- catch (Exception extInfo)
- {
- Interface.ReportException(extInfo);
- return;
- }
- Interface.IsInstalled(RemoteHooking.GetCurrentProcessId());
- RemoteHooking.WakeUpProcess();
- while (true)
- {
- if (DateTime.Now > OldDateTime + new TimeSpan(0, 0, 2))
- {
- NeedTakeScreen = true;
- }
- Interface.Ping();
- Thread.Sleep(100);
- }
- }
- [UnmanagedFunctionPointer(CallingConvention.StdCall, CharSet = CharSet.Unicode, SetLastError = true)]
- delegate IntPtr DwglSwapBuffers(IntPtr hdc);
- [DllImport("opengl32.dll", CharSet = CharSet.Unicode, SetLastError = true,
- CallingConvention = CallingConvention.StdCall)]
- static extern IntPtr wglSwapBuffers(IntPtr hdc);
- // this is where we are intercepting all file accesses!
- static IntPtr SwapBuffers_Hooked(IntPtr hdc)
- {
- Main This = (Main) HookRuntimeInfo.Callback;
- try
- {
- /** apitrace dump is
- *
- 11288 wglSwapBuffers(hdc = 0xb0013e0a) = TRUE
- 11289 glFlush()
- 11290 wglMakeCurrent(hdc = 0xf601374b, hglrc = 0x10004) = TRUE
- 11291 glDisable(cap = GL_SCISSOR_TEST)
- 11292 glClear(mask = GL_COLOR_BUFFER_BIT)
- 11293 glEnable(cap = GL_SCISSOR_TEST)
- 11294 glDisable(cap = GL_BLEND)
- 11295 glBindTexture(target = GL_TEXTURE_2D, texture = 20)
- 11296 glPushClientAttrib(mask = GL_CLIENT_VERTEX_ARRAY_BIT)
- 11297 glPushAttrib(mask = GL_TRANSFORM_BIT)
- 11298 glMatrixMode(mode = GL_PROJECTION)
- 11299 glPushMatrix()
- 11300 glLoadIdentity()
- 11301 glGetIntegerv(pname = GL_VIEWPORT, params = {0, 0, 860, 720})
- 11302 glOrtho(left = 0, right = 860, bottom = 0, top = 720, zNear = 0, zFar = -1)
- 11303 glMatrixMode(mode = GL_TEXTURE)
- 11304 glPushMatrix()
- 11305 glLoadIdentity()
- 11306 glMatrixMode(mode = GL_MODELVIEW)
- 11307 glPushMatrix()
- * **/
- if (This.NeedTakeScreen)
- {
- int[] glViewport = new int[4];
- Gl.glGetIntegerv(Gl.GL_VIEWPORT, glViewport);
- Bitmap bitmap = new Bitmap(glViewport[2], glViewport[3]);
- Rectangle bounds = new Rectangle(0, 0, glViewport[2], glViewport[3]);
- BitmapData bmpData = bitmap.LockBits(bounds, ImageLockMode.ReadWrite,
- PixelFormat.Format32bppRgb);
- Gl.glReadBuffer(Gl.GL_FRONT);
- Gl.glReadPixels(0, 0, bounds.Width, bounds.Height, Gl.GL_BGR,
- Gl.GL_UNSIGNED_BYTE, bmpData.Scan0);
- bitmap.UnlockBits(bmpData);
- using (var fs = new FileStream(Path.GetTempPath() + "SCT_screenshot.png", FileMode.Create))
- {
- bitmap.Save(fs, ImageFormat.Png);
- fs.Flush();
- fs.Close();
- }
- This.NeedTakeScreen = false;
- This.OldDateTime = DateTime.Now;
- }
- }
- catch (Exception ex)
- {
- This.Interface.ReportException(ex);
- }
- return wglSwapBuffers(hdc);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement