convert ahk code into c++

Scraatch

Well-Known Member
Joined
Jan 14, 2017
Messages
76
Likes
2
Points
23
Location
Germany
#1
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
}
 
OP
OP
Scraatch

Scraatch

Well-Known Member
Joined
Jan 14, 2017
Messages
76
Likes
2
Points
23
Location
Germany
#3
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

Well-Known Member
Joined
Jan 2, 2017
Messages
1,598
Likes
202
Points
78
Location
Lithuania
Website
ugbase.eu
15
#4
This is all what you need.
Just get samp memory and read it step by step...
PHP:
:IsLocalPlayerSpawned
{
    SAMP 0.3.7 - R1
}
0AA2: [email protected] = loadLib "samp.dll" //samp base offset          
[email protected] += 0x21A0F8
0A8D: [email protected] = readmem [email protected] sz 4 vp 0 //stInfo  
[email protected] += 0x3CD
0A8D: [email protected] = readmem [email protected] sz 4 vp 0 //stPools
[email protected] += 0x18
0A8D: [email protected] = readmem [email protected] sz 4 vp 0 //stPlayerPools
[email protected] += 0x22
0A8D: [email protected] = readmem [email protected] sz 4 vp 0 //LocalPlayer 
[email protected] += 0x136
0A8D: [email protected] = readmem [email protected] sz 4 vp 0
if
[email protected] == 1
then
    0485:  return_true
else
    059A:  return_false
end
0AB2: ret 0
 
Last edited:
OP
OP
Scraatch

Scraatch

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

Parazitas

Well-Known Member
Joined
Jan 2, 2017
Messages
1,598
Likes
202
Points
78
Location
Lithuania
Website
ugbase.eu
15
#13
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