Jump to content

I need really tight loops


Recommended Posts

Is there a way to make the following loop go faster?

While $State=1
MemWrite($OpenProcess, $TsAddress, 'int', 2 , 1)
Wend


Func MemWrite($i_hProcess, $i_lpBaseAddress, $s_Type ,$v_Inject, $i_nSize)
   Local $hDll = DllOpen("kernel32.dll")
   If @error Then
      SetError(1)
      Return 0
   EndIf
   $v_lpNumberOfBytesRead = ''
   Local $v_Struct = DllStructCreate ($s_Type&'[' & $i_nSize & ']')
   DllStructSetData ($v_Struct, 1, $v_Inject)
    
   $i_Call = DllCall($hDll, 'int', 'WriteProcessMemory', 'int', $i_hProcess, 'int', $i_lpBaseAddress, 'int', DllStructGetPtr ($v_Struct, 1), 'int', $i_nSize, 'int', $v_lpNumberOfBytesRead)
   If @error Then
      SetError(1)
      Return 0
   EndIf
   $v_Struct=0
   DllClose($hDll)
   Return $i_Call[0]
EndFunc

I need that MemWrite to happen at an extremely fast pase... any suggestions?

Edited by faldo
Link to comment
Share on other sites

Maybe this is faster:

Global $hDll = DllOpen("kernel32.dll")
While $State = 1
    MemWrite($OpenProcess, $TsAddress, 'int', 2, 1)
WEnd
DllClose($hDll)

Func MemWrite($i_hProcess, $i_lpBaseAddress, $s_Type, $v_Inject, $i_nSize)
    If @error Then
        SetError(1)
        Return 0
    EndIf
    $v_lpNumberOfBytesRead = ''
    Local $v_Struct = DllStructCreate($s_Type & '[' & $i_nSize & ']')
    DllStructSetData($v_Struct, 1, $v_Inject)
    $i_Call = DllCall($hDll, 'int', 'WriteProcessMemory', 'int', $i_hProcess, 'int', $i_lpBaseAddress, 'int', DllStructGetPtr($v_Struct, 1), 'int', $i_nSize, 'int', $v_lpNumberOfBytesRead)
    If @error Then
        SetError(1)
        Return 0
    EndIf
    $v_Struct = 0
    Return $i_Call[0]
EndFunc  ;==>MemWrite
Link to comment
Share on other sites

Move the DllOpen and DllClose out of the function to the header and after the loop accordingly. I don't know how faster it will be but it'd save you some unnecessary code line execution over and over...

If you're calling this function always with the same $s_Type, $v_Inject, $i_nSize parameters then it'd save some time to remove them all and declare the DllStructCreate variable as a global one and...

Link to comment
Share on other sites

If you're calling this function always with the same $s_Type, $v_Inject, $i_nSize parameters then it'd save some time to remove them all and declare the DllStructCreate variable as a global one and...

My second thought :P

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...