elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: AIO elhacker.NET 2021 Compilación herramientas análisis y desinfección malware


+  Foro de elhacker.net
|-+  Programación
| |-+  Programación General
| | |-+  .NET (C#, VB.NET, ASP) (Moderador: kub0x)
| | | |-+  [Ayuda] Mi Class Overlay para Cheats genera un Alto Consumo de Ram.
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: [Ayuda] Mi Class Overlay para Cheats genera un Alto Consumo de Ram.  (Leído 3,642 veces)
**Aincrad**


Desconectado Desconectado

Mensajes: 668



Ver Perfil WWW
[Ayuda] Mi Class Overlay para Cheats genera un Alto Consumo de Ram.
« en: 16 Septiembre 2020, 16:20 pm »

Hola, soy yo de nuevo pidiendo una ayudita....

Mi clase que hice ya hace un tiempo me esta dando problema al laggear la PC , en proyectos medianamente grandes.

Por ende necesito algunos consejos. para restructurar el codigo y que consuma poca ram.

La clase Overlay es la que uso para que el cheat se mantenga Junto a la ventana del juego .
y tambien monitoriza cuando el juego se cierra. todo Asincronico.

Pero consume mucha ram , haber si alguien con mas conocimientos sobre optimizar codigo me podria ayudar :

Elektro otra ayudita porfa..

Uso:
'ProcessGame Nombre del proceso del juego
'Me El form el cual se va a superponer encima del juego.
Código
  1. Private AttachClienGame As New Overlay(ProcessGame, Me)

Overlay.vb

Código
  1. Imports System.Runtime.InteropServices
  2. Imports System.Net.Mail
  3. Imports System.Text
  4.  
  5.  
  6. Public Class Overlay
  7.  
  8. #Region " P/Invokes "
  9.  
  10.    <DllImport("user32.dll")> _
  11.    Public Shared Function GetWindowRect(ByVal hWnd As IntPtr, ByRef lpRect As RECT) As Boolean
  12.    End Function
  13.  
  14.    <DllImport("user32.dll", SetLastError:=True)> _
  15.    Private Shared Function GetForegroundWindow() As IntPtr
  16.    End Function
  17.  
  18.    <DllImport("user32.dll", SetLastError:=True)> _
  19.    Private Shared Function GetWindowThreadProcessId(ByVal hWnd As IntPtr, ByRef lpdwProcessId As UInteger) As Integer
  20.    End Function
  21.  
  22.    Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As IntPtr
  23.    Public Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As IntPtr, ByRef lpdwProcessId As Integer) As Integer
  24.    Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Integer, ByVal bInheritHandle As Integer, ByVal dwProcessId As Integer) As IntPtr
  25.    Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As IntPtr) As Integer
  26.  
  27. #End Region
  28.  
  29. #Region " Properties "
  30.  
  31.    Private Shared Monitor As Boolean = True
  32.    Public Property ActivateMonitoring As Boolean
  33.        Get
  34.            Return Monitor
  35.        End Get
  36.        Set(value As Boolean)
  37.            Monitor = value
  38.        End Set
  39.    End Property
  40.  
  41.    Private Shared GameWindowsTitle As String = String.Empty
  42.    Public ReadOnly Property GetWindowsTitle As String
  43.        Get
  44.            Return GameWindowsTitle
  45.        End Get
  46.    End Property
  47.  
  48.    Private Shared GameHandle As IntPtr = Nothing
  49.    Public ReadOnly Property GetGameHandle As IntPtr
  50.        Get
  51.            Return GameHandle
  52.        End Get
  53.    End Property
  54.  
  55.    Private Shared GameLocation As Point = Nothing
  56.    Public ReadOnly Property GetGameLocation As Point
  57.        Get
  58.            Return GameLocation
  59.        End Get
  60.    End Property
  61.  
  62.    Private Shared GameSize As Size = Nothing
  63.    Public ReadOnly Property GetGameSize As Size
  64.        Get
  65.            Return GameSize
  66.        End Get
  67.    End Property
  68.  
  69. #End Region
  70.  
  71. #Region " Structures "
  72.  
  73.    Public Structure RECT
  74.        Public Left As Integer
  75.        Public Top As Integer
  76.        Public Right As Integer
  77.        Public Bottom As Integer
  78.    End Structure
  79.  
  80. #End Region
  81.  
  82. #Region " Declare's "
  83.  
  84.    Public window_loc As RECT
  85.    Public screencenter(1) As Integer
  86.    Public hWnd As IntPtr = Nothing
  87.    Public pHandle As IntPtr = Nothing
  88.    Public processID As Integer = Nothing
  89.    Private ProcessName As String = String.Empty
  90.    Public Const PROCESS_VM_ALL As Integer = &H1F0FFF
  91.    Private MyAppProcess As String = Process.GetCurrentProcess().ProcessName
  92.    Private FormManagement As Form = Nothing
  93.  
  94. #End Region
  95.  
  96. #Region " Public Methods "
  97.  
  98.    Public Sub New(ByVal ProcName As String, Optional ByVal FormC As Form = Nothing)
  99.        ProcessName = ProcName
  100.        If ProcessName.ToLower.EndsWith(".exe") Then ProcessName = ProcessName.Substring(0, ProcessName.Length - 4)
  101.        FormManagement = FormC
  102.        Dim tsk As New Task(AddressOf AttachtClient, TaskCreationOptions.LongRunning)
  103.        tsk.Start()
  104.    End Sub
  105.  
  106. #End Region
  107.  
  108. #Region " Private Methods "
  109.  
  110.    Private Sub AttachtClient()
  111.        'On Error Resume Next
  112.        Do While True
  113.            If Monitor = True Then
  114.                If Not ProcessName = String.Empty Then
  115.  
  116.                    Dim proc() As Process = Process.GetProcessesByName(ProcessName)
  117.  
  118.                    If Not proc.Length = 0 Then
  119.  
  120.                        If IsGameAppFocus() = True Then
  121.  
  122.                            Dim windowname As String = proc(0).MainWindowTitle
  123.                            GameWindowsTitle = windowname
  124.  
  125.                            hWnd = FindWindow(vbNullString, windowname)
  126.  
  127.                            GetWindowThreadProcessId(hWnd, processID)
  128.  
  129.                            pHandle = OpenProcess(PROCESS_VM_ALL, 0, processID)
  130.                            GameHandle = pHandle
  131.  
  132.                            If hWnd = 0 Then Exit Do
  133.  
  134.                            GetWindowRect(hWnd, window_loc)
  135.  
  136.                            GameLocation = New Point((window_loc.Left + 10), (window_loc.Top + 35))
  137.  
  138.                            GameSize = New Point(((window_loc.Right - window_loc.Left) - 25), ((window_loc.Bottom - window_loc.Top) - 45))
  139.  
  140.                            If Not (FormManagement Is Nothing) Then
  141.  
  142.                                If FormManagement.Visible = True Then
  143.  
  144.                                    FormManagement.BeginInvoke(Sub()
  145.                                                                   FormManagement.Location = GameLocation
  146.                                                                   FormManagement.Size = GameSize
  147.                                                               End Sub)
  148.  
  149.                                End If
  150.  
  151.                            End If
  152.  
  153.                        Else
  154.  
  155.                            If FormManagement.Visible = True Then
  156.  
  157.                                FormManagement.BeginInvoke(Sub()
  158.                                                               FormManagement.Hide()
  159.                                                           End Sub)
  160.  
  161.                            End If
  162.  
  163.                        End If
  164.  
  165.                    Else
  166.  
  167.                        is_active(True)
  168.  
  169.                    End If
  170.                End If
  171.            End If
  172.        Loop
  173.    End Sub
  174.  
  175.    Private Function IsGameAppFocus() As Boolean
  176.        Dim ActiveProcess As Process = GetActiveProcess()
  177.        Dim IsFocusGame As Boolean = False
  178.        If ActiveProcess IsNot Nothing Then
  179.            Dim CurrentProcName As String = ActiveProcess.ProcessName
  180.  
  181.            If LCase(CurrentProcName) = LCase(MyAppProcess) Then
  182.                IsFocusGame = True
  183.            End If
  184.  
  185.            If LCase(CurrentProcName) = LCase(ProcessName) Then
  186.                IsFocusGame = True
  187.            End If
  188.  
  189.  
  190.            Return IsFocusGame
  191.  
  192.        End If
  193.  
  194.        Return IsFocusGame
  195.    End Function
  196.  
  197.    Private Function GetActiveProcess() As Process
  198.        Dim FocusedWindow As IntPtr = GetForegroundWindow()
  199.        If FocusedWindow = IntPtr.Zero Then Return Nothing
  200.  
  201.        Dim FocusedWindowProcessId As UInteger = 0
  202.        GetWindowThreadProcessId(FocusedWindow, FocusedWindowProcessId)
  203.  
  204.        If FocusedWindowProcessId = 0 Then Return Nothing
  205.        Return Process.GetProcessById(CType(FocusedWindowProcessId, Integer))
  206.    End Function
  207.  
  208.    Private Sub is_active(Optional ByVal exit_ As Boolean = True)
  209.        Dim p As Process() = Process.GetProcessesByName(ProcessName)
  210.        If p.Length = 0 And exit_ Then
  211.            Environment.Exit(0)
  212.        End If
  213.    End Sub
  214.  
  215. #End Region
  216.  
  217. End Class
  218.  


En línea



Eleкtro
Ex-Staff
*
Desconectado Desconectado

Mensajes: 9.810



Ver Perfil
Re: [Ayuda] Mi Class Overlay para Cheats genera un Alto Consumo de Ram.
« Respuesta #1 en: 16 Septiembre 2020, 19:10 pm »

He probado el código con varios procesos (y juegos) y a mi no me da ningún tipo de problema, practicamente no aumenta el consumo de memoria, con o sin form superpuesto a la ventana.

De todas formas tienes un bucle "infinito" sin ningún tipo de intervalo para dejar descansar a la CPU, además en cada iteración del bucle solicitas el open-handle del proceso y no liberas los recursos nativos en memoria... ¿para que tienes declarada la función CloseHandle en tu código si no la usas?. Empieza por ahí.

Además deberías eliminar todas esas declaraciones al estilo de VB6 y adaptarlas al estilo .NET. El valor de retorno de la función "OpenProcess" debería ser del tipo SafeAccessTokenHandle, aunque es perfectamente válido que uses la función CloseHandle para liberar los recursos, pero si lo haces como te digo basta con usar la instrucción Using o el método SafeAccessTokenHandle.Close(). Precisamente tienes un ejemplo de esto en el testamento de código que compartí contigo ayer en el otro tema que abriste (aunque con la función OpenThread).

Saludos!


En línea

**Aincrad**


Desconectado Desconectado

Mensajes: 668



Ver Perfil WWW
Re: [Ayuda] Mi Class Overlay para Cheats genera un Alto Consumo de Ram.
« Respuesta #2 en: 16 Septiembre 2020, 21:54 pm »

Perfecto. reduje en mas del 50% de lo que consumía.  gracias de nuevo bro.
También actualice los Pinvokes.

 ;-)
En línea



Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines