convert ahk code into c++

Scraatch

Active member
Joined
Jan 14, 2017
Messages
76
Reaction score
2
Location
Germany
Hello,

i want to check if the player is spawned and i found this code in an autohotkey script and tried to convert it into c++ but with no success. I hope that someone can explain to me how to convert code such like this. Btw i am new at coding and learning c++.

Code:
isPlayerSpawned() {
    return checkHandles() && __DWORD(hGTA,  dwSAMP,  [SAMP_INFO_PTR,  SAMP_POOLS,  SAMP_POOL_PLAYER,  SAMP_LOCALPLAYER,  0x136])
}

all informations to this code
Code:
global dwSAMP                                   := 0x0
global SAMP_INFO_PTR                            := 0x21A0F8
global SAMP_POOLS                                := 0x3CD
global SAMP_POOL_PLAYER                            := 0x18
global SAMP_LOCALPLAYER                            := 0x22

checkHandles() {
    return !refreshGTA() || !refreshSAMP() || !refreshMemory() ? false : true
}

refreshGTA() {
    if (!(newPID := getPID("GTA:SA:MP"))) {
        if (hGTA) {
            virtualFreeEx(hGTA, pMemory, 0, 0x8000)
            closeProcess(hGTA)
        }
        dwGTAPID := 0, hGTA := 0x0, dwSAMP := 0x0, pMemory := 0x0
        return false
    }
    if (!hGTA || dwGTAPID != newPID) {
        if (!(hGTA := openProcess(newPID))) {
            dwGTAPID := 0, hGTA := 0x0, dwSAMP := 0x0, pMemory := 0x0
            return false
        }
        dwGTAPID := newPID, dwSAMP := 0x0, pMemory := 0x0
    }
    return true
}

refreshSAMP() {
    if (dwSAMP)
        return true
    dwSAMP := getModuleBaseAddress("samp.dll", hGTA)
    if (!dwSAMP)
        return false
    if (__READMEM(hGTA, dwSAMP, [0x1036], "UChar") != 0xD8) {
        msgbox, 64, % "SA:MP Version not compatible", % "use 0.3.7"
        ExitApp
    }
    return true
}

refreshMemory() {
    if (!pMemory) {
        pMemory := virtualAllocEx(hGTA, 6384, 0x1000 | 0x2000, 0x40)
        if (ErrorLevel) {
            pMemory := 0x0
            return false
        }
        pInjectFunc := pMemory + 5120
        pDetours    := pInjectFunc + 1024
    }
    return true
}

__DWORD(hProcess, dwAddress, offsets) {
    if (!hProcess || !dwAddress)
        return ""
    VarSetCapacity(dwRead, 4)
    for i, o in offsets {
        dwRet := DllCall("ReadProcessMemory", "UInt", hProcess, "UInt", dwAddress + o, "Str", dwRead, "UInt", 4, "UInt*", 0)
        if (!dwRet)
            return ""
        dwAddress := NumGet(dwRead, 0, "UInt")
    }
    return dwAddress
}
 

Scraatch

Active member
Joined
Jan 14, 2017
Messages
76
Reaction score
2
Location
Germany
Everythink what you need .. , already is in this thread, wait 1min i make code.
Might be like that, but i have no experience with converting code and i really dont understand how the _DWORD function in autohotkey works
 

Parazitas

God
Joined
Jan 2, 2017
Messages
3,113
Solutions
5
Reaction score
878
Location
Lithuania
This is all what you need.
Just get samp memory and read it step by step...
PHP:
:IsLocalPlayerSpawned
{
    SAMP 0.3.7 - R1
}
0AA2: 0@ = loadLib "samp.dll" //samp base offset          
0@ += 0x21A0F8
0A8D: 0@ = readmem 0@ sz 4 vp 0 //stInfo  
0@ += 0x3CD
0A8D: 0@ = readmem 0@ sz 4 vp 0 //stPools
0@ += 0x18
0A8D: 0@ = readmem 0@ sz 4 vp 0 //stPlayerPools
0@ += 0x22
0A8D: 0@ = readmem 0@ sz 4 vp 0 //LocalPlayer 
0@ += 0x136
0A8D: 0@ = readmem 0@ sz 4 vp 0
if
0@ == 1
then
    0485:  return_true
else
    059A:  return_false
end
0AB2: ret 0
 
Last edited:

Scraatch

Active member
Joined
Jan 14, 2017
Messages
76
Reaction score
2
Location
Germany
This is all what you need.
Just get samp memory and read it step by step...
PHP:
:IsLocalPlayerSpawned
0AA2: 0@ = loadLib "samp.dll" //samp base offset        
0@ += 0x21A0F8
0A8D: 0@ = readmem 0@ sz 4 vp 0 //stInfo
0@ += 0x3CD
0A8D: 0@ = readmem 0@ sz 4 vp 0 //stPools
0@ += 0x18
0A8D: 0@ = readmem 0@ sz 4 vp 0 //stPlayerPools
0@ += 0x22
0A8D: 0@ = readmem 0@ sz 4 vp 0 //LocalPlayer
0@ += 0x136
0A8D: 0@ = readmem 0@ sz 4 vp 0
if
0@ == 1
then
    0485:  return_true
else
    059A:  return_false
end
0AB2: ret 0
bro i think thats cleo? i want it in c++
 

Scraatch

Active member
Joined
Jan 14, 2017
Messages
76
Reaction score
2
Location
Germany
yeah i have the address for the samp.dll and i do internal code for a .dll
 

Parazitas

God
Joined
Jan 2, 2017
Messages
3,113
Solutions
5
Reaction score
878
Location
Lithuania
Fine , i make code , but i haven't windows programs , so i use online compiler, wait little.
 
Last edited:

Parazitas

God
Joined
Jan 2, 2017
Messages
3,113
Solutions
5
Reaction score
878
Location
Lithuania
I haven't a lot experience with c++ , but this should work.

C++:
#include <iostream>
#include <windows.h>
#include <TlHelp32.h>

using namespace std;

uintptr_t GetModuleBaseAddress(DWORD dwProcID, const char* szModuleName)
{
    uintptr_t ModuleBaseAddress = 0;
    HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE | TH32CS_SNAPMODULE32, dwProcID);
    if (hSnapshot != INVALID_HANDLE_VALUE)
    {
        MODULEENTRY32 ModuleEntry32;
        ModuleEntry32.dwSize = sizeof(MODULEENTRY32);
        if (Module32First(hSnapshot, &ModuleEntry32))
        {
            do
            {
                if (strcmp(ModuleEntry32.szModule, szModuleName) == 0)
                {
                    ModuleBaseAddress = (uintptr_t)ModuleEntry32.modBaseAddr;
                    break;
                }
            } while (Module32Next(hSnapshot, &ModuleEntry32));
        }
        CloseHandle(hSnapshot);
    }
    return ModuleBaseAddress;
}


int main()
{
    DWORD dwProcId;

    HWND hWnd = FindWindowA(NULL, ("GTA:SA:MP"));

    GetWindowThreadProcessId(hWnd, &dwProcId);
    HANDLE pHandle = OpenProcess(PROCESS_VM_READ, FALSE, dwProcId);

    uintptr_t SampBaseaddr = (uintptr_t)GetModuleBaseAddress(dwProcId, "samp.dll");
    uintptr_t SAMP_INFO_PTR = SampBaseaddr + 0x21A0F8;
    
    uintptr_t SAMP_INFO_PTR_RTN;  // STORE RETURN MEMORY
    ReadProcessMemory(pHandle, (LPVOID)SAMP_INFO_PTR, &SAMP_INFO_PTR_RTN, sizeof(SAMP_INFO_PTR_RTN), NULL;

    uintptr_t SAMP_POOLS_PTR = SAMP_INFO_PTR_RTN + 0x3CD;
    
    uintptr_t SAMP_POOLS_RTN; // STORE RETURN MEMORY
    ReadProcessMemory(pHandle, (LPVOID)SAMP_POOLS_PTR, &SAMP_POOLS_RTN, sizeof(SAMP_POOLS_RTN), NULL;

    uintptr_t SAMP_POOL_PLAYER_PTR = SAMP_POOLS_RTN + 0x18;
    
    uintptr_t SAMP_POOL_PLAYER_RTN; // STORE RETURN MEMORY
    ReadProcessMemory(pHandle, (LPVOID)SAMP_POOL_PLAYER_PTR, &SAMP_POOL_PLAYER_RTN, sizeof(SAMP_POOL_PLAYER_RTN), NULL;

    uintptr_t SAMP_LOCALPLAYER_PTR = SAMP_POOL_PLAYER_RTN + 0x22;
    
    uintptr_t SAMP_LOCALPLAYER_RTN; // STORE RETURN MEMORY
    ReadProcessMemory(pHandle, (LPVOID)SAMP_LOCALPLAYER_PTR, &SAMP_LOCALPLAYER_RTN, sizeof(SAMP_LOCALPLAYER_RTN), NULL;

    uintptr_t SAMP_LOCALPLAYER_SPAWNED_PTR = SAMP_LOCALPLAYER_RTN + 0x136;
    
    uintptr_t SAMP_LOCALPLAYER_SPAWNED_RTN; // STORE RETURN MEMORY
    ReadProcessMemory(pHandle, (LPVOID)SAMP_LOCALPLAYER_SPAWNED_PTR, &SAMP_LOCALPLAYER_SPAWNED_RTN, sizeof(SAMP_LOCALPLAYER_SPAWNED_RTN), NULL;

    /// SAMP_LOCALPLAYER_SPAWNED_RTN - is your value , 1 = spawned / 0 = not spawned 

    return 0;
}
 
Top