Ir para o conteúdo
  • Revista PROGRAMAR: Já está disponível a edição #60 da revista programar. Faz já o download aqui!

sYnced

c# + OpenGL

Mensagens Recomendadas

sYnced

Bem pessoal estou a usar opengl, vou mostrar todo o código que tenho desde a DLL para injectar no jogo e o código da DLL

Programa:

public Form1()
 {
	 InitializeComponent();
	 timer1.Tick += timer1_Tick;
	 timer1.Interval = 2 * 1000;//2segundos
	 //m_timer.Enable = true;
	 timer1.Enabled = true;
 }
 [DllImport("kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 public static extern int ReadProcessMemory(int hProcess, int lpBaseAddress, string lpBuffer, int nSize, ref int lpNumberOfBytesWritten);
 [DllImport("kernel32", EntryPoint = "LoadLibraryA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 public static extern int LoadLibrary(string lpLibFileName);
 [DllImport("kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 public static extern int WriteProcessMemory(System.IntPtr hProcess, int lpBaseAddress, string lpBuffer, int nSize, int lpNumberOfBytesWritten);
 [DllImport("kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 public static extern int VirtualAllocEx(System.IntPtr hProcess, int lpAddress, int dwSize, int flAllocationType, int flProtect);
 [DllImport("kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 public static extern int GetProcAddress(int hModule, string lpProcName);
 [DllImport("kernel32", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 public static extern int CreateRemoteThread(System.IntPtr hProcess, int lpThreadAttributes, int dwStackSize, int lpStartAddress, int lpParameter, int dwCreationFlags, int lpThreadId);
 [DllImport("Kernel32", EntryPoint = "GetModuleHandleA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int GetModuleHandle(string lpModuleName);
 [DllImport("kernel32.dll")]
 public static extern IntPtr OpenProcess(int dwDesiredAccess, bool bInheritHandle, int dwProcessId);
 [DllImport("kernel32", EntryPoint = "CloseHandle")]
 private static extern int CloseHandle(System.IntPtr hObject);
 [DllImport("user32", EntryPoint = "FindWindowA", ExactSpelling = true, CharSet = CharSet.Ansi, SetLastError = true)]
 private static extern int FindWindow(string lpClassName, string lpWindowName);
 void Inject()
 {
	 System.IntPtr TargetProcessHandle;
	 int TargetBufferSize;
	 String pszLibFileRemote;
	 int pfnStartAddr;
	 Process[] TargetProcess = Process.GetProcessesByName("hl");
	 TargetProcessHandle = OpenProcess(0x1F0FFF, false, TargetProcess[0].Id);
	 pszLibFileRemote = Application.StartupPath + ("\\dll.dll");
	 pfnStartAddr = GetProcAddress(GetModuleHandle("Kernel32.dll"), "LoadLibraryA");
	 TargetBufferSize = 1 + pszLibFileRemote.Length;
	 int Rtn;
	 int LoadLibParamAdr;
	 LoadLibParamAdr = VirtualAllocEx(TargetProcessHandle, 0, TargetBufferSize, 4096, 4);
	 Rtn = WriteProcessMemory(TargetProcessHandle, LoadLibParamAdr, pszLibFileRemote, TargetBufferSize, 0);
	 CreateRemoteThread(TargetProcessHandle, 0, 0, pfnStartAddr, LoadLibParamAdr, 0, 0);
	 CloseHandle(TargetProcessHandle);
 }
 Timer timer1 = new Timer();
 private void timer1_Tick(object sender, EventArgs e)
	 {
		 Process[] TargetProcess = Process.GetProcessesByName("hl");
		 if (TargetProcess.Length == 0)
		 {}else{
		 Inject();
		 timer1.Stop();
		 button1.Enabled = true;
	 }
	 }
 private void button1_Click(object sender, EventArgs e)
 {
	 System.Threading.Thread.Sleep(10000);
	 //dll.Class1.framebuffer();
	 pictureBox1.Image = dll.Class1.GrabScreenshot(this.ClientRectangle);
 }

DLL:

 // Creates a 1.0-compatible GraphicsContext with GraphicsMode.Default
 GameWindow window = new GameWindow();
 // Creates a 3.0-compatible GraphicsContext with 32bpp color, 24bpp depth
 // 8bpp stencil and 4x anti-aliasing.
 GLControl control = new GLControl(new GraphicsMode(32, 24, 8, 4), 3, 0, 0);
 public static Bitmap GrabScreenshot(Rectangle Rect)
 {
	 int width = 860;
	 int height = 600;
	 Bitmap bmp = new Bitmap(width, height);
	 System.Drawing.Imaging.BitmapData data =
		 bmp.LockBits(Rect, System.Drawing.Imaging.ImageLockMode.WriteOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
	 GL.ReadPixels(0, 0, width, height, PixelFormat.Bgr, PixelType.UnsignedByte, data.Scan0);
	 bmp.UnlockBits(data);
	 bmp.RotateFlip(RotateFlipType.RotateNoneFlipY);
	 return bmp;
 }
 /*private static Rectangle GetScreen()
 {
	 return Screen.GetWorkingArea(this.control);
 }*/
 private static class User32
 {
	 [structLayout(LayoutKind.Sequential)]
	 public struct Rect
	 {
		 public int left;
		 public int top;
		 public int right;
		 public int bottom;
	 }
	 [DllImport("user32.dll")]
	 public static extern IntPtr GetWindowRect(IntPtr hWnd, ref Rect rect);
 }
 public static void framebuffer()
 {
	 const int FboWidth = 512;
	 const int FboHeight = 512;
	 uint FboHandle;
	 uint ColorTexture;
	 uint DepthRenderbuffer;
	 // Create Color Texture
	 GL.GenTextures(1, out ColorTexture);
	 GL.BindTexture(TextureTarget.Texture2D, ColorTexture);
	 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
	 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
	 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.Clamp);
	 GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.Clamp);
	 GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.Rgba8, FboWidth, FboHeight, 0, PixelFormat.Rgba, PixelType.UnsignedByte, IntPtr.Zero);
	 // test for GL Error here (might be unsupported format)
	 GL.BindTexture(TextureTarget.Texture2D, 0); // prevent feedback, reading and writing to the same image is a bad idea
	 // Create Depth Renderbuffer
	 GL.Ext.GenRenderbuffers(1, out DepthRenderbuffer);
	 GL.Ext.BindRenderbuffer(RenderbufferTarget.RenderbufferExt, DepthRenderbuffer);
	 GL.Ext.RenderbufferStorage(RenderbufferTarget.RenderbufferExt, (RenderbufferStorage)All.DepthComponent32, FboWidth, FboHeight);
	 // test for GL Error here (might be unsupported format)
	 // Create a FBO and attach the textures
	 GL.Ext.GenFramebuffers(1, out FboHandle);
	 GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, FboHandle);
	 GL.Ext.FramebufferTexture2D(FramebufferTarget.FramebufferExt, FramebufferAttachment.ColorAttachment0Ext, TextureTarget.Texture2D, ColorTexture, 0);
	 GL.Ext.FramebufferRenderbuffer(FramebufferTarget.FramebufferExt, FramebufferAttachment.DepthAttachmentExt, RenderbufferTarget.RenderbufferExt, DepthRenderbuffer);
	 // now GL.Ext.CheckFramebufferStatus( FramebufferTarget.FramebufferExt ) can be called, check the end of this page for a snippet.
	 // since there's only 1 Color buffer attached this is not explicitly required
	 GL.DrawBuffer((DrawBufferMode)FramebufferAttachment.ColorAttachment0Ext);
	 GL.PushAttrib(AttribMask.ViewportBit); // stores GL.Viewport() parameters
	 GL.Viewport(0, 0, FboWidth, FboHeight);
	 // render whatever your heart desires, when done ...
	 GL.PopAttrib(); // restores GL.Viewport() parameters
	 GL.Ext.BindFramebuffer(FramebufferTarget.FramebufferExt, 0); // return to visible framebuffer
	 GL.DrawBuffer(DrawBufferMode.Back);
	 DrawBuffersEnum[] bufs = new DrawBuffersEnum[2] { (DrawBuffersEnum)FramebufferAttachment.ColorAttachment0Ext, (DrawBuffersEnum)FramebufferAttachment.ColorAttachment1Ext }; // fugly, will be addressed in 0.9.2
	 GL.DrawBuffers(bufs.Length, bufs);
 }

Vi o código em:

http://www.opentk.com/doc/graphics/save-opengl-rendering-to-disk

E até tenho lá um post:

http://www.opentk.com/node/3312

Depois de me dizerem o que tinha de fazer injecto código no CS 1.6 como podem ver, está o meu antigo código lá onde tinha mudado nada ou quase nada.

Quem me poder dar uma ajudinha nisto agradeço imenso.

Partilhar esta mensagem


Ligação para a mensagem
Partilhar noutros sites

Crie uma conta ou ligue-se para comentar

Só membros podem comentar

Criar nova conta

Registe para ter uma conta na nossa comunidade. É fácil!

Registar nova conta

Entra

Já tem conta? Inicie sessão aqui.

Entrar Agora

×

Aviso Sobre Cookies

Ao usar este site você aceita os nossos Termos de Uso e Política de Privacidade. Este site usa cookies para disponibilizar funcionalidades personalizadas. Para mais informações visite esta página.