冰楓論壇

標題: [VB.NET] MapleFunction 一個方便 VB.NET 使用的楓之谷模組 [打印本頁]

作者: ai1118    時間: 2014-7-14 09:43
標題: [VB.NET] MapleFunction 一個方便 VB.NET 使用的楓之谷模組
MapleFunction.vb
  1. Module MapleFunction
  2.     '----------------------------------系統API宣告-----------------------------------'
  3.     Private Declare Function OpenProcess Lib "kernel32" (ByVal Access As Integer, ByVal Handle As Boolean, ByVal ProcessId As Integer) As IntPtr
  4.     Private Declare Function WriteProcessMemory Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByVal lpBuffer() As Byte, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
  5.     Private Declare Function ReadProcessMemory Lib "kernel32" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
  6.     '----------------------------------啟用所有CheckBox-----------------------------------'
  7.     Public Sub AllCrackMemoryFunctionCheckBoxEnable(ByVal ParentControls As System.Windows.Forms.Control.ControlCollection)
  8.         For Each i As Control In ParentControls
  9.             If i.Name.Contains("CheckBox") Then i.Enabled = True
  10.         Next
  11.     End Sub
  12.     Structure MapleStoryProcess
  13.         Shared WriteMapleStoryIntptr As IntPtr = IntPtr.Zero
  14.         Shared MProcess As Process = Nothing
  15.         'Public ID As Integer = Nothing '如VB2008編譯上出錯,請把這行的註解拿掉.
  16.         '----------------------------------自定義指標格式-----------------------------------'
  17.         Structure Pointer
  18.             Public Address As Integer
  19.             Public Offset
  20.         End Structure
  21.         '----------------------------------讀取楓之谷遊戲-----------------------------------'
  22.         Public Function LoadMemory() As Boolean
  23.             Try
  24.                 Dim MapleStoryProcess As Process = Process.GetProcessesByName("MapleStory")(0)
  25.                 If MapleStoryProcess.MainWindowHandle.Equals(IntPtr.Zero) = False Then
  26.                     WriteMapleStoryIntptr = OpenProcess(&H1F0FFF, False, MapleStoryProcess.Id)
  27.                     MProcess = MapleStoryProcess
  28.                     Return True
  29.                 End If
  30.                 Return False
  31.             Catch ex As Exception
  32.                 Return False
  33.             End Try
  34.         End Function
  35.         '--------------------------------讀取楓之谷遊戲(可指定Handle)------------------------'
  36.         Public Function LoadMemory(ByVal Handle As IntPtr) As Boolean
  37.             Try
  38.                 Dim AllProcessInHandle As Process() = Process.GetProcesses
  39.                 For Each P As Process In AllProcessInHandle
  40.                     If P.MainWindowHandle = Handle Then
  41.                         WriteMapleStoryIntptr = OpenProcess(&H1F0FFF, False, P.Id)
  42.                         MProcess = P
  43.                         Return True
  44.                     End If
  45.                 Next
  46.                 Return False
  47.             Catch ex As Exception
  48.                 Return False
  49.             End Try
  50.         End Function
  51.         Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Integer
  52.         Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Integer, ByRef lpdwProcessId As Integer) As Integer
  53.         '--------------------------------讀取楓之谷遊戲(指定視窗)------------------------'
  54.         Public Function OpenProcessByWindow(ByVal lpWindowName As String, Optional ByVal lpClassName As String = vbNullString) As Boolean
  55.             Try
  56.                 Dim Pid As Integer
  57.                 GetWindowThreadProcessId(FindWindow(lpClassName, lpWindowName), Pid)
  58.                 Dim MapleStoryProcess As Process = Process.GetProcessById(Pid)
  59.                 If MapleStoryProcess.MainWindowHandle.Equals(IntPtr.Zero) = False Then
  60.                     WriteMapleStoryIntptr = OpenProcess(&H1F0FFF, False, MapleStoryProcess.Id)
  61.                     MProcess = MapleStoryProcess
  62.                     Return True
  63.                 End If
  64.                 Return False
  65.             Catch ex As Exception
  66.                 Return False
  67.             End Try

  68.         End Function
  69.         '----------------------------------寫入遊戲記憶體-----------------------------------'
  70.         Public Function WriteByte(ByVal Address As Integer, ByVal ArrayOfByte As Byte())
  71.             On Error Resume Next
  72.             If WriteMapleStoryIntptr.Equals(IntPtr.Zero) Then
  73.                 MsgBox("Application Can't Catch MapleStory Process.", MsgBoxStyle.Critical, "Error")
  74.                 Return False
  75.             End If
  76.             WriteProcessMemory(WriteMapleStoryIntptr, Address, ArrayOfByte, ArrayOfByte.Length, False)
  77.             Return True
  78.         End Function
  79.         Private Declare Function WriteProcessMemoryAPI Lib "kernel32" Alias "WriteProcessMemory" (ByVal hProcess As IntPtr, ByVal lpBaseAddress As Integer, ByRef lpBuffer As Integer, ByVal nSize As Integer, ByVal lpNumberOfBytesWritten As Integer) As Integer
  80.         Public Function WriteLong(ByVal lpAddress As Integer, ByVal lpValue As Integer) As Integer
  81.             Try
  82.                 Return WriteProcessMemoryAPI(WriteMapleStoryIntptr, lpAddress, lpValue, Len(lpValue), False)
  83.             Catch ex As Exception
  84.                 Return 0
  85.             End Try
  86.         End Function

  87.         Public Function MakeJmp(ByVal lpAddress As Long, ByVal lpJmpAddress As Long, Optional ByVal lpNops As Long = 0) As Long
  88.             Dim JmpByte As Byte() = {&HE9}
  89.             MakeJmp = CBool(WriteByte(lpAddress, JmpByte)) And CBool(WriteLong(lpAddress + 1, lpJmpAddress - lpAddress - 5))
  90.             If lpNops = 0 Then Exit Function
  91.             Return MakeJmp
  92.         End Function

  93.         Public Function MakeCall(ByVal lpAddress As Long, ByVal lpCallAddress As Long, Optional ByVal lpNops As Long = 0) As Long
  94.             Dim CallByte As Byte() = {&HE8}
  95.             MakeCall = CBool(WriteByte(lpAddress, CallByte)) And CBool(WriteLong(lpAddress + 1, lpCallAddress - lpAddress - 5))
  96.             If lpNops = 0 Then Exit Function
  97.             Return MakeCall
  98.         End Function
  99.         '----------------------------------讀取地址的Value-----------------------------------'
  100.         Public Function GetValue(ByVal Address As Integer) As Integer
  101.             On Error Resume Next
  102.             If WriteMapleStoryIntptr.Equals(IntPtr.Zero) Then
  103.                 MsgBox("Application Can't Catch MapleStory Process.", MsgBoxStyle.Critical, "Error")
  104.                 Return False
  105.             End If
  106.             Dim GetRetValue As Integer = vbNullString
  107.             ReadProcessMemory(WriteMapleStoryIntptr, Address, GetRetValue, 4, False)
  108.             Return GetRetValue
  109.         End Function

  110.         Public Function ReadLong(ByVal lpAddress As Long) As Long
  111.             Dim Value As Integer = vbNullString
  112.             ReadProcessMemory(WriteMapleStoryIntptr, lpAddress, Value, 4, False)
  113.             Return Value
  114.         End Function

  115.         '----------------------------------讀取指標的Value-----------------------------------'
  116.         Public Function GetPointerValue(ByVal Pointer As MapleStoryProcess.Pointer) As Integer
  117.             On Error Resume Next
  118.             If WriteMapleStoryIntptr.Equals(IntPtr.Zero) Then
  119.                 MsgBox("Application Can't Catch MapleStory Process.", MsgBoxStyle.Critical, "Error")
  120.                 Return False
  121.             End If
  122.             Dim GetPointerMain, GetTheTrueValue As Integer
  123.             ReadProcessMemory(WriteMapleStoryIntptr, "&H" + Pointer.Address, GetPointerMain, 4, False)
  124.             ReadProcessMemory(WriteMapleStoryIntptr, GetPointerMain + "&H" + Pointer.Offset, GetTheTrueValue, 4, False)
  125.             Return GetTheTrueValue
  126.         End Function

  127.         Public Function ReadPointer(ByVal lpAddress As Long, ByVal lpOffset As Long) As Long
  128.             Return ReadLong(ReadLong(lpAddress) + lpOffset)
  129.         End Function
  130.         '----------------------------------十進位轉換16進位(Hex)-----------------------------------'
  131.         Public Function IntegerHex(ByVal InPutInt As String) As Integer
  132.             On Error Resume Next
  133.             Return Microsoft.VisualBasic.Hex(InPutInt)
  134.         End Function
  135.         '----------------------------------16進位轉十進位(UnHex)-----------------------------------'
  136.         Public Function IntegerUnHex(ByVal InPutInt As String) As Integer
  137.             On Error Resume Next
  138.             Return CLng("&H" & InPutInt)
  139.             End
  140.         End Function
  141.         '----------------------------------結束遊戲<強制>-------------------------------------------'
  142.         Public Function KillMapleStory() As Boolean
  143.             Try
  144.                 MProcess.Kill()
  145.                 Return True
  146.             Catch ex As Exception
  147.                 Return False
  148.             End Try
  149.         End Function
  150.         '---------------------------------結束遊戲<普通>--------------------------------------------'
  151.         Public Function EndMapleStory() As Boolean
  152.             Try
  153.                 MProcess.CloseMainWindow()
  154.                 Return True
  155.             Catch ex As Exception
  156.                 Return False
  157.             End Try
  158.         End Function
  159.         '-------------------------------確認遊戲是否存在---------------------------------------------'
  160.         Public Function MapleStoryCloseOrNot() As Boolean
  161.             Return MProcess.HasExited
  162.         End Function
  163.         '------------------------------- Auto Key Press ---------------------------------------------'
  164.         Private Declare Auto Function PostMessage Lib "user32.dll" (ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Boolean
  165.         Private Declare Function MapVirtualKey Lib "user32" Alias "MapVirtualKeyA" (ByVal wCode As Integer, ByVal wMapType As Integer) As Integer
  166.         Private Declare Function ExitWindowsEx Lib "user32 " (ByVal uFlags As Integer, ByVal dwReserved As Integer) As Integer
  167.         Private Const WM_KEYDOWN As Integer = &H100
  168.         Dim hModuleNoFree As Integer

  169.         Public Function RingPst(ByVal HWnd As Integer, ByVal KeyType As String, ByVal KeyCode As String)
  170.             If KeyCode = "關閉電腦" Then ExitWindowsEx(1, 0)
  171.             Dim myKeyValue As Integer = toKeyValue(KeyCode)
  172.             Select Case KeyType
  173.                 Case "Press"
  174.                     PostMessage(HWnd, &H100, myKeyValue, MakeKeyLparam(myKeyValue, &H100))
  175.                     PostMessage(HWnd, &H101, myKeyValue, MakeKeyLparam(myKeyValue, &H101))
  176.                     Return 1
  177.                 Case "Down"
  178.                     PostMessage(HWnd, &H100, myKeyValue, MakeKeyLparam(myKeyValue, &H100))
  179.                     Return 1
  180.                 Case "Up"
  181.                     PostMessage(HWnd, &H101, myKeyValue, MakeKeyLparam(myKeyValue, &H101))
  182.                     Return 1
  183.                 Case Else
  184.                     Return 0
  185.             End Select
  186.         End Function

  187.         Private Function toKeyValue(ByVal KeyCode As String) As Integer
  188.             Dim strKey() As String = {"None", "Enter", "Shift", "Ctrl", "Alt", "Space", "PageUp", "PageDown", "Insert", "Delete", "Home", "End", "Left", "Up", "Right", "Down", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8", "F9", "F10", "F11", "F12", "ESCAPE"}
  189.             Dim intKey() As Integer = {0, &HD, &H10, &H11, &H12, &H20, &H21, &H22, &H2D, &H2E, &H24, &H23, &H25, &H26, &H27, &H28, &H30, &H31, &H32, &H33, &H34, &H35, &H36, &H37, &H38, &H39, &H41, &H42, &H43, &H44, &H45, &H46, &H47, &H48, &H49, &H4A, &H4B, &H4C, &H4D, &H4E, &H4F, &H50, &H51, &H52, &H53, &H54, &H55, &H56, &H57, &H58, &H59, &H5A, &H70, &H71, &H72, &H73, &H74, &H75, &H76, &H77, &H78, &H79, &H7A, &H7B, &H1B}
  190.             For i = 0 To strKey.Length - 1
  191.                 If strKey(i) = KeyCode Then Return intKey(i)
  192.             Next
  193.             Return False
  194.         End Function
  195.         Private Function MakeKeyLparam(ByVal VirtualKey As Integer, ByVal flag As Integer) As Integer
  196.             '參數VirtualKey表示按鍵虛擬碼,flag表示是按下鍵還是釋放鍵,用WM_KEYDOWN和WM_KEYUP這兩個常數表示
  197.             Dim s As String
  198.             Dim Firstbyte As String 'lparam參數的24-31位
  199.             If flag = WM_KEYDOWN Then '如果是按下鍵
  200.                 Firstbyte = "00"
  201.             Else
  202.                 Firstbyte = "C0" '如果是釋放鍵
  203.             End If
  204.             Dim Scancode As Long
  205.             '獲得鍵的掃描碼
  206.             Scancode = MapVirtualKey(VirtualKey, 0)
  207.             Dim Secondbyte As String 'lparam參數的16-23位元,即虛擬鍵掃描碼
  208.             Secondbyte = Right("00" & Hex(Scancode), 2)
  209.             s = Firstbyte & Secondbyte & "0001" '0001為lparam參數的0-15位,即發送次數和其他擴展資訊
  210.             MakeKeyLparam = Val("&H" & s)
  211.         End Function
  212.         '------------------------------- 申請記憶體 ---------------------------------------------'
  213.         Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As IntPtr, ByVal lpAddress As IntPtr, ByVal dwSize As IntPtr, ByVal flAllocationType As Integer, ByVal flProtect As Integer) As IntPtr
  214.         Public Function Alloc(ByVal lpSize As Integer, Optional ByVal lpAddress As Integer = 0) As Integer
  215.             Const MEM_COMMIT As Integer = &H1000
  216.             Const PAGE_EXECUTE_READWRITE As Integer = &H40
  217.             Dim pBlob As IntPtr = VirtualAllocEx(WriteMapleStoryIntptr, New IntPtr(lpAddress), New IntPtr(lpSize), MEM_COMMIT, PAGE_EXECUTE_READWRITE)
  218.             If pBlob = IntPtr.Zero Then Throw New Exception
  219.             Return pBlob.ToInt32
  220.         End Function
  221.         '------------------------------- Address2Aob ---------------------------------------------'
  222.         Public Function Adr2Aob(ByVal Address As Integer) As String
  223.             Dim tmpAOB As String = Hex(Address).PadLeft(8, "0")
  224.             Dim reAOB As String = Nothing
  225.             For i = 1 To 7
  226.                 If (i Mod 2) = 1 Then
  227.                     reAOB = Mid(tmpAOB, i, 2) & " " & reAOB
  228.                 End If
  229.             Next
  230.             Return Trim(reAOB)
  231.         End Function
  232.     End Structure
  233. End Module



複製代碼
[size=100%]來源: knowlet3389.blogspot.tw



作者: listento555    時間: 2015-2-27 19:51
感謝





歡迎光臨 冰楓論壇 (https://bingfong.com/) Powered by 冰楓