Author Topic: Rewrite of PeepingTom library (Peek/Poke replacement)  (Read 3404 times)

0 Members and 1 Guest are viewing this topic.

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Rewrite of PeepingTom library (Peek/Poke replacement)
« on: February 17, 2021, 11:51:22 pm »
I had written a library a while back called "PeepingTom". I decided to open it today and I noticed I had a lot of wrong variables and an unnecessary declaration of an entire TYPE for 32 Bit. So, I've cleaned up the code quite a bit, brought it to the new 1.5 DIM syntax (which means that dev build 1.5 is required), and fixed all the variables. Everything should be the exact right sizes and types. Also, following the new CamelCase style of 1.5, all functions have been renamed using this standard. Below is a screenshot showing example code running for each function. This is Windows only right now but I am still looking at what can be done in Linux and Mac. I'd love for it to be cross-platform. I have a feeling @NOVARSEG would be interested in this code.

 
Screenshot 2021-02-17 235021.png


And the code, (The $INCLUDE starts at the "Begin $INCLUDE" comment):
Code: QB64: [Select]
  1.  
  2. $If VERSION < 1.5 Then
  3.     $ERROR Requires QB64 v1.5 or greater
  4.  
  5. exe = Mid$(Command$(0), _InStrRev(Command$(0), "\") + 1)
  6. Dim As Integer pokey
  7.  
  8. Dim As _Byte testbyte
  9. testbyte = -126
  10. Print "Value at _Offset(testbyte):", , PeekByte(exe, _Offset(testbyte))
  11. pokey = PokeByte(exe, _Offset(testbyte), -108)
  12. Print "Value after PokeByte:", , testbyte
  13.  
  14. Dim As _Unsigned _Byte testunsignedbyte
  15. testunsignedbyte = 243
  16. Print "Value at _Offset(testunsignedbyte):", PeekUnsignedByte(exe, _Offset(testunsignedbyte))
  17. pokey = PokeUnsignedByte(exe, _Offset(testunsignedbyte), 204)
  18. Print "Value after PokeUnsignedByte:", testunsignedbyte
  19.  
  20. Dim As Integer testint
  21. testint = -5005
  22. Print "Value at _Offset(testint):", , PeekInt(exe, _Offset(testint))
  23. pokey = PokeInt(exe, _Offset(testint), -4080)
  24. Print "Value after PokeInt:", , testint
  25.  
  26. Dim As _Unsigned Integer testunsignedint
  27. testunsignedint = 5234
  28. Print "Value at _Offset(testunsignedint):", PeekUnsignedInt(exe, _Offset(testunsignedint))
  29. pokey = PokeUnsignedInt(exe, _Offset(testunsignedint), 5500)
  30. Print "Value after PokeUnsignedInt:", , testunsignedint
  31.  
  32. Dim As Long testlong
  33. testlong = -55000
  34. Print "Value at _Offset(testlong):", , PeekLong(exe, _Offset(testlong))
  35. pokey = PokeLong(exe, _Offset(testlong), -32872)
  36. Print "Value after PokeLong:", , testlong
  37.  
  38. Dim As _Unsigned Long testunsignedlong
  39. testunsignedlong = 56985
  40. Print "Value at _Offset(testunsignedlong:", PeekUnsignedLong(exe, _Offset(testunsignedlong))
  41. pokey = PokeUnsignedLong(exe, _Offset(testunsignedlong), 57234)
  42. Print "Value after PokeUnsignedLong:", testunsignedlong
  43.  
  44. Dim As _Integer64 testint64
  45. testint64 = -58698560
  46. Print "Value at _Offset(testint64):", , PeekInt64(exe, _Offset(testint64))
  47. pokey = PokeInt64(exe, _Offset(testint64), -98758763)
  48. Print "Value after PokeInt64:", , testint64
  49.  
  50. Dim As _Unsigned _Integer64 testunsignedint64
  51. testunsignedint64 = 5854324590
  52. Print "Value at _Offset(testunsignedint64):", PeekUnsignedInt64(exe, _Offset(testunsignedint64))
  53. pokey = PokeUnsignedInt64(exe, _Offset(testunsignedint64), 3248506902)
  54. Print "Value after PokeUnsignedInt64:", testunsignedint64
  55.  
  56. Dim As String teststring
  57. teststring = "This is a test of peeking a string stored at _Offset(teststring)" + Chr$(0)
  58. Print PeekString(exe, _Offset(teststring))
  59. pokey = PokeString(exe, _Offset(teststring), "This is the value of the string after poking")
  60. Print teststring
  61.  
  62.  
  63. 'Begin $INCLUDE
  64.  
  65. Type PROCESSENTRY32
  66.     dwSize As Long
  67.     cntUsage As Long
  68.     th32ProcessID As Long
  69.     $If 64BIT Then
  70.         padding As Long
  71.     $End If
  72.     th32DefaultHeapID As _Unsigned _Offset
  73.     th32ModuleID As Long
  74.     cntThreads As Long
  75.     th32ParentProcessID As Long
  76.     pcPriClassBase As Long
  77.     dwFlags As Long
  78.     szExeFile As String * 260
  79.  
  80. Const PROCESS_VM_READ = &H0010
  81. Const PROCESS_QUERY_INFORMATION = &H0400
  82. Const PROCESS_VM_WRITE = &H0020
  83. Const PROCESS_VM_OPERATION = &H0008
  84. Const STANDARD_RIGHTS_REQUIRED = &H000F0000
  85. Const SYNCHRONIZE = &H00100000
  86. Const PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or SYNCHRONIZE Or &HFFFF
  87.  
  88. Const TH32CS_INHERIT = &H80000000
  89. Const TH32CS_SNAPHEAPLIST = &H00000001
  90. Const TH32CS_SNAPMODULE = &H00000008
  91. Const TH32CS_SNAPMODULE32 = &H00000010
  92. Const TH32CS_SNAPPROCESS = &H00000002
  93. Const TH32CS_SNAPTHREAD = &H00000004
  94. Const TH32CS_SNAPALL = TH32CS_SNAPHEAPLIST Or TH32CS_SNAPMODULE Or TH32CS_SNAPPROCESS Or TH32CS_SNAPTHREAD
  95.  
  96. Const TOM_TRUE = -1
  97. Const TOM_FALSE = 0
  98.  
  99.     Function CreateToolhelp32Snapshot%& (ByVal dwFlags As Long, Byval th32ProcessID As Long)
  100.     Function Process32First% (ByVal hSnapshot As _Offset, Byval lppe As _Offset)
  101.     Function Process32Next% (ByVal hSnapshot As _Offset, Byval lppe As _Offset)
  102.     Function OpenProcess%& (ByVal dwDesiredAccess As Long, Byval bInheritHandle As _Byte, Byval dwProcessId As Long)
  103.     Function ReadProcessMemory% (ByVal hProcess As _Offset, Byval lpBaseAddress As _Offset, Byval lpBuffer As _Offset, Byval nSize As Long, Byval lpNumberOfBytesRead As _Offset)
  104.     Function WriteProcessMemory% (ByVal hProcess As _Offset, Byval lpBaseAddress As _Offset, Byval lpBuffer As _Offset, Byval nSize As Long, Byval lpNumberOfBytesWritten As _Offset)
  105.     Function CloseHandle% (ByVal hObject As _Offset)
  106.  
  107.     Function strlen& (ByVal ptr As _Unsigned _Offset)
  108.  
  109. Function PeekByte%% (process As String, address As _Unsigned _Offset)
  110.     Dim As _Offset hProcessSnap, hProcess
  111.     Dim As PROCESSENTRY32 pe32
  112.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  113.     pe32.dwSize = Len(pe32)
  114.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  115.         While Process32Next(hProcessSnap, _Offset(pe32))
  116.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  117.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  118.                 Dim As Integer memo
  119.                 Dim As _Byte result
  120.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 1, 0)
  121.                 Exit While
  122.             End If
  123.         Wend
  124.     End If
  125.     Dim As Integer closeh
  126.     closeh = CloseHandle(hProcessSnap)
  127.     closeh = CloseHandle(hProcess)
  128.     PeekByte = result
  129.  
  130. Function PokeByte% (process As String, address As _Unsigned _Offset, value As _Byte)
  131.     Dim As _Offset hProcessSnap
  132.     Dim As _Offset hProcess
  133.     Dim As PROCESSENTRY32 pe32
  134.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  135.     pe32.dwSize = Len(pe32)
  136.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  137.         While Process32Next(hProcessSnap, _Offset(pe32))
  138.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  139.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  140.                 Dim memo As Integer
  141.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 1, 0)
  142.                 Exit While
  143.             End If
  144.         Wend
  145.     End If
  146.     Dim closeh As Long
  147.     closeh = CloseHandle(hProcessSnap)
  148.     closeh = CloseHandle(hProcess)
  149.     PokeByte = memo
  150.  
  151. Function PeekUnsignedByte~%% (process As String, address As _Unsigned _Offset)
  152.     Dim As _Offset hProcessSnap, hProcess
  153.     Dim As PROCESSENTRY32 pe32
  154.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  155.     pe32.dwSize = Len(pe32)
  156.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  157.         While Process32Next(hProcessSnap, _Offset(pe32))
  158.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  159.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  160.                 Dim As Integer memo
  161.                 Dim As _Unsigned _Byte result
  162.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 1, 0)
  163.                 Exit While
  164.             End If
  165.         Wend
  166.     End If
  167.     Dim As Integer closeh
  168.     closeh = CloseHandle(hProcessSnap)
  169.     closeh = CloseHandle(hProcess)
  170.     PeekUnsignedByte = result
  171.  
  172. Function PokeUnsignedByte% (process As String, address As _Unsigned _Offset, value As _Unsigned _Byte)
  173.     Dim As _Offset hProcessSnap
  174.     Dim As _Offset hProcess
  175.     Dim As PROCESSENTRY32 pe32
  176.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  177.     pe32.dwSize = Len(pe32)
  178.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  179.         While Process32Next(hProcessSnap, _Offset(pe32))
  180.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  181.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  182.                 Dim memo As Integer
  183.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 1, 0)
  184.                 Exit While
  185.             End If
  186.         Wend
  187.     End If
  188.     Dim closeh As Long
  189.     closeh = CloseHandle(hProcessSnap)
  190.     closeh = CloseHandle(hProcess)
  191.     PokeUnsignedByte = memo
  192.  
  193. Function PeekInt% (process As String, address As _Unsigned _Offset)
  194.     Dim As _Offset hProcessSnap, hProcess
  195.     Dim As PROCESSENTRY32 pe32
  196.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  197.     pe32.dwSize = Len(pe32)
  198.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  199.         While Process32Next(hProcessSnap, _Offset(pe32))
  200.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  201.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  202.                 Dim As Integer memo
  203.                 Dim As Integer result
  204.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 2, 0)
  205.                 Exit While
  206.             End If
  207.         Wend
  208.     End If
  209.     Dim As Integer closeh
  210.     closeh = CloseHandle(hProcessSnap)
  211.     closeh = CloseHandle(hProcess)
  212.     PeekInt = result
  213.  
  214. Function PokeInt% (process As String, address As _Unsigned _Offset, value As Integer)
  215.     Dim As _Offset hProcessSnap, hProcess
  216.     Dim As PROCESSENTRY32 pe32
  217.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  218.     pe32.dwSize = Len(pe32)
  219.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  220.         While Process32Next(hProcessSnap, _Offset(pe32))
  221.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  222.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  223.                 Dim As Integer memo
  224.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 2, 0)
  225.                 Exit While
  226.             End If
  227.         Wend
  228.     End If
  229.     Dim As Integer closeh
  230.     closeh = CloseHandle(hProcessSnap)
  231.     closeh = CloseHandle(hProcess)
  232.     PokeInt = memo
  233.  
  234. Function PeekUnsignedInt~% (process As String, address As _Unsigned _Offset)
  235.     Dim As _Offset hProcessSnap, hProcess
  236.     Dim As PROCESSENTRY32 pe32
  237.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  238.     pe32.dwSize = Len(pe32)
  239.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  240.         While Process32Next(hProcessSnap, _Offset(pe32))
  241.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  242.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  243.                 Dim As Integer memo
  244.                 Dim As _Unsigned Integer result
  245.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 2, 0)
  246.                 Exit While
  247.             End If
  248.         Wend
  249.     End If
  250.     Dim As Integer closeh
  251.     closeh = CloseHandle(hProcessSnap)
  252.     closeh = CloseHandle(hProcess)
  253.     PeekUnsignedInt = result
  254.  
  255. Function PokeUnsignedInt% (process As String, address As _Unsigned _Offset, value As _Unsigned Integer)
  256.     Dim As _Offset hProcessSnap, hProcess
  257.     Dim As PROCESSENTRY32 pe32
  258.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  259.     pe32.dwSize = Len(pe32)
  260.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  261.         While Process32Next(hProcessSnap, _Offset(pe32))
  262.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  263.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  264.                 Dim As Integer memo
  265.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 2, 0)
  266.                 Exit While
  267.             End If
  268.         Wend
  269.     End If
  270.     Dim As Integer closeh
  271.     closeh = CloseHandle(hProcessSnap)
  272.     closeh = CloseHandle(hProcess)
  273.     PokeUnsignedInt = memo
  274.  
  275. Function PeekLong& (process As String, address As _Unsigned _Offset)
  276.     Dim As _Offset hProcessSnap, hProcess
  277.     Dim As PROCESSENTRY32 pe32
  278.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  279.     pe32.dwSize = Len(pe32)
  280.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  281.         While Process32Next(hProcessSnap, _Offset(pe32))
  282.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  283.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  284.                 Dim As Integer memo
  285.                 Dim As Long result
  286.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 4, 0)
  287.                 Exit While
  288.             End If
  289.         Wend
  290.     End If
  291.     Dim As Integer closeh
  292.     closeh = CloseHandle(hProcessSnap)
  293.     closeh = CloseHandle(hProcess)
  294.     PeekLong = result
  295.  
  296. Function PokeLong% (process As String, address As _Unsigned _Offset, value As Long)
  297.     Dim As _Offset hProcessSnap, hProcess
  298.     Dim As PROCESSENTRY32 pe32
  299.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  300.     pe32.dwSize = Len(pe32)
  301.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  302.         While Process32Next(hProcessSnap, _Offset(pe32))
  303.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  304.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  305.                 Dim As Integer memo
  306.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 4, 0)
  307.                 Exit While
  308.             End If
  309.         Wend
  310.     End If
  311.     Dim As Integer closeh
  312.     closeh = CloseHandle(hProcessSnap)
  313.     closeh = CloseHandle(hProcess)
  314.     PokeLong = memo
  315.  
  316. Function PeekUnsignedLong~& (process As String, address As _Unsigned _Offset)
  317.     Dim As _Offset hProcessSnap, hProcess
  318.     Dim As PROCESSENTRY32 pe32
  319.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  320.     pe32.dwSize = Len(pe32)
  321.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  322.         While Process32Next(hProcessSnap, _Offset(pe32))
  323.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  324.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  325.                 Dim As Integer memo
  326.                 Dim As _Unsigned Long result
  327.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 4, 0)
  328.                 Exit While
  329.             End If
  330.         Wend
  331.     End If
  332.     Dim As Integer closeh
  333.     closeh = CloseHandle(hProcessSnap)
  334.     closeh = CloseHandle(hProcess)
  335.     PeekUnsignedLong = result
  336.  
  337. Function PokeUnsignedLong% (process As String, address As _Unsigned _Offset, value As _Unsigned Long)
  338.     Dim As _Offset hProcessSnap, hProcess
  339.     Dim As PROCESSENTRY32 pe32
  340.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  341.     pe32.dwSize = Len(pe32)
  342.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  343.         While Process32Next(hProcessSnap, _Offset(pe32))
  344.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  345.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  346.                 Dim As Integer memo
  347.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 4, 0)
  348.                 Exit While
  349.             End If
  350.         Wend
  351.     End If
  352.     Dim As Integer closeh
  353.     closeh = CloseHandle(hProcessSnap)
  354.     closeh = CloseHandle(hProcess)
  355.     PokeUnsignedLong = memo
  356.  
  357. Function PeekInt64&& (process As String, address As _Unsigned _Offset)
  358.     Dim As _Offset hProcessSnap, hProcess
  359.     Dim As PROCESSENTRY32 pe32
  360.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  361.     pe32.dwSize = Len(pe32)
  362.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  363.         While Process32Next(hProcessSnap, _Offset(pe32))
  364.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  365.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  366.                 Dim As Integer memo
  367.                 Dim As _Integer64 result
  368.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 8, 0)
  369.                 Exit While
  370.             End If
  371.         Wend
  372.     End If
  373.     Dim As Integer closeh
  374.     closeh = CloseHandle(hProcessSnap)
  375.     closeh = CloseHandle(hProcess)
  376.     PeekInt64 = result
  377.  
  378. Function PokeInt64% (process As String, address As _Unsigned _Offset, value As _Integer64)
  379.     Dim As _Offset hProcessSnap, hProcess
  380.     Dim As PROCESSENTRY32 pe32
  381.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  382.     pe32.dwSize = Len(pe32)
  383.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  384.         While Process32Next(hProcessSnap, _Offset(pe32))
  385.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  386.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  387.                 Dim As Integer memo
  388.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 8, 0)
  389.                 Exit While
  390.             End If
  391.         Wend
  392.     End If
  393.     Dim As Integer closeh
  394.     closeh = CloseHandle(hProcessSnap)
  395.     closeh = CloseHandle(hProcess)
  396.     PokeInt64 = memo
  397.  
  398. Function PeekUnsignedInt64~&& (process As String, address As _Unsigned _Offset)
  399.     Dim As _Offset hProcessSnap, hProcess
  400.     Dim As PROCESSENTRY32 pe32
  401.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  402.     pe32.dwSize = Len(pe32)
  403.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  404.         While Process32Next(hProcessSnap, _Offset(pe32))
  405.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  406.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  407.                 Dim As Integer memo
  408.                 Dim As _Unsigned _Integer64 result
  409.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), 8, 0)
  410.                 Exit While
  411.             End If
  412.         Wend
  413.     End If
  414.     Dim As Integer closeh
  415.     closeh = CloseHandle(hProcessSnap)
  416.     closeh = CloseHandle(hProcess)
  417.     PeekUnsignedInt64 = result
  418.  
  419. Function PokeUnsignedInt64% (process As String, address As _Unsigned _Offset, value As _Unsigned _Integer64)
  420.     Dim As _Offset hProcessSnap, hProcess
  421.     Dim As PROCESSENTRY32 pe32
  422.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  423.     pe32.dwSize = Len(pe32)
  424.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  425.         While Process32Next(hProcessSnap, _Offset(pe32))
  426.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  427.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  428.                 Dim As Integer memo
  429.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), 8, 0)
  430.                 Exit While
  431.             End If
  432.         Wend
  433.     End If
  434.     Dim As Integer closeh
  435.     closeh = CloseHandle(hProcessSnap)
  436.     closeh = CloseHandle(hProcess)
  437.     PokeUnsignedInt64 = memo
  438.  
  439. Function PeekString$ (process As String, address As _Unsigned _Offset)
  440.     Dim As _Offset hProcessSnap, hProcess
  441.     Dim As PROCESSENTRY32 pe32
  442.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  443.     pe32.dwSize = Len(pe32)
  444.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  445.         While Process32Next(hProcessSnap, _Offset(pe32))
  446.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  447.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  448.                 Dim As Integer memo
  449.                 Dim As String result
  450.                 result = Space$(strlen(address))
  451.                 memo = ReadProcessMemory(hProcess, address, _Offset(result), Len(result), 0)
  452.                 Exit While
  453.             End If
  454.         Wend
  455.     End If
  456.     Dim As Integer closeh
  457.     closeh = CloseHandle(hProcessSnap)
  458.     closeh = CloseHandle(hProcess)
  459.     PeekString = result
  460.  
  461. Function PokeString% (process As String, address As _Unsigned _Offset, value As String)
  462.     Dim As _Offset hProcessSnap, hProcess
  463.     Dim As PROCESSENTRY32 pe32
  464.     hProcessSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
  465.     pe32.dwSize = Len(pe32)
  466.     If Process32First(hProcessSnap, _Offset(pe32)) Then
  467.         While Process32Next(hProcessSnap, _Offset(pe32))
  468.             If _StrCmp(Left$(pe32.szExeFile, InStr(pe32.szExeFile, ".exe" + Chr$(0)) + 3), process) = 0 Then
  469.                 hProcess = OpenProcess(PROCESS_VM_READ Or PROCESS_QUERY_INFORMATION Or PROCESS_VM_WRITE Or PROCESS_VM_OPERATION, TOM_FALSE, pe32.th32ProcessID)
  470.                 Dim As Integer memo
  471.                 Dim As Long lenaddress
  472.                 lenaddress = strlen(address)
  473.                 If Right$(value, 1) <> Chr$(0) Then
  474.                     value = value + Chr$(0)
  475.                 End If
  476.                 If lenaddress > Len(value) Then
  477.                     Dim As Long i
  478.                     For i = 1 To lenaddress
  479.                         value = value + Chr$(0)
  480.                     Next
  481.                 End If
  482.                 memo = WriteProcessMemory(hProcess, address, _Offset(value), Len(value), 0)
  483.                 Exit While
  484.             End If
  485.         Wend
  486.     End If
  487.     Dim As Integer closeh
  488.     closeh = CloseHandle(hProcessSnap)
  489.     closeh = CloseHandle(hProcess)
  490.     PokeString = memo
  491. 'End $INCLUDE
« Last Edit: February 18, 2021, 09:43:38 am by SpriggsySpriggs »
Shuwatch!

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: Rewrite of PeepingTom library (Peek/Poke replacement)
« Reply #1 on: February 18, 2021, 12:32:21 am »
So exactly how will that pop my CD try open?

Actually, I do miss the Atari days, where you could peek/poke to redefine characters in any 8x8 pixel form you wanted. Also, it would be nice to poke in the key-presses that _SENDKEY doesn't support. QB64 does still support a limited use of these old memory addresses, like 1047, which I use to detect Shift, and Alt key combinations.

Interesting stuff, but I'll have to wait until v1.5 comes out to take it for a spin.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Rewrite of PeepingTom library (Peek/Poke replacement)
« Reply #2 on: February 18, 2021, 12:41:42 am »
@Pete This is designed to read/write memory from any process in Windows. I have that first argument of "process As String" so that the correct process is grabbed. I used the file I'm running because I didn't make a different exe to run at the same time but I know the code works because I used it myself for a game trainer I made a while back. As for the CD tray, I have no clue. In this case, you'd need to know the a process name that is capable of opening the tray and then set the value in memory to cause the action. As far as waiting for 1.5, you can actually grab the dev build from the site and run it that way if you want. That, or the DIM statements can just be rearranged to match the 1.4 standard. That's really the only thing keeping you from using it in 1.4
« Last Edit: February 18, 2021, 12:43:03 am by SpriggsySpriggs »
Shuwatch!

Offline SpriggsySpriggs

  • Forum Resident
  • Posts: 1145
  • Larger than life
    • View Profile
    • GitHub
Re: Rewrite of PeepingTom library (Peek/Poke replacement)
« Reply #3 on: February 18, 2021, 10:01:45 am »
I suppose it should be mentioned that if you are running multiple instances of the same program that they'd have the same process name. That being said, it isn't normal that you would try writing to a process that may be running multiple instances. All that can be done as a proactive measure is to be careful and use this sparingly. If you start changing memory all willy-nilly then you could have some issues. Also, it's possible that you might require admin rights to read/write to a process that isn't the EXE containing this code. This is expected and normal. If you find that you are unable to change data in another application then you will need to launch the executable with this code as administrator. I made an update to the original post with some new lines in the PokeString% function because of needing to null-terminate the string that the address is being changed to. Without that, you will end up grabbing data from elsewhere on your machine.
Shuwatch!