Attribute VB_Name = "Engine"
Public GameCap As String
Public GameCls As String

Private Const PROCESS_ALL_ACCESS = &H1F0FFF
Private Const PAGE_EXECUTE_READWRITE = &H40
Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hwnd As Long, lpdwProcessId As Long) As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function WriteProcessMemory Lib "kernel32" (ByVal hProcess As Long, ByVal lpBaseAddress As Any, lpBuffer As Any, ByVal nSize As Long, lpNumberOfBytesWritten As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal Classname As String, ByVal WindowName As String) As Long
Private Declare Function VirtualProtectEx Lib "kernel32" (ByVal hProcess As Long, lpAddress As Any, ByVal dwSize As Long, ByVal flNewProtect As Long, lpflOldProtect As Long) As Long

' A windows WriteProcessMemory for a single byte
Public Sub WriteByte(Address As Long, Value As Byte, WinCls As String, WinCap As String)
On Error Resume Next
    Dim hwnd As Long, pid As Long, phandle As Long, OldProtect As Long
    ' Window And Class
        GameCap = "Pacman 2.4"
        GameCls = vbNullString
    
    hwnd = FindWindow(GameCls, GameCap)
    If (hwnd <> 0) Then
        GetWindowThreadProcessId hwnd, pid
        phandle = OpenProcess(PROCESS_ALL_ACCESS, False, pid)
        If (phandle <> 0) Then
            ' VirtualProtectEx - Not Working
                'If VirtualProtectEx(phandle, Address, 1, PAGE_EXECUTE_READWRITE, OldProtect&) = 0 Then
                '    MsgBox "grrrr"
                '    Exit Sub
                'End If
            WriteProcessMemory phandle, Address, Value, 1, 0&
        End If
        CloseHandle phandle
        End If
End Sub
