简体   繁体   中英

Editing memory with a custom function in Lua C API

I'm trying to edit memory with my custom function in the luaC api but for example when i do like 3 lua_tonumber(LS, -1) it just gets mixed up? Please try to review my code and tell me how to fix this..

lua_State *L;

using namespace std;




DWORD MyGetProcessId(LPCTSTR ProcessName) 
{
    PROCESSENTRY32 pt;
    HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
    pt.dwSize = sizeof(PROCESSENTRY32);
    if (Process32First(hsnap, &pt)) {
        do {
            if (!lstrcmpi(pt.szExeFile, ProcessName)) {
                CloseHandle(hsnap);
                return pt.th32ProcessID;
            }
        } while (Process32Next(hsnap, &pt));
    }
    CloseHandle(hsnap);
    return 0;
}

int CustomGetProcessByName(lua_State* Ls) {
    DWORD dieman = MyGetProcessId(lua_tostring(Ls, -1));


    lua_pushvalue(Ls, dieman);

    return 1;

}
int CustomWriteMemInt(lua_State* Ls) {



    HANDLE ProcHand = OpenProcess(PROCESS_ALL_ACCESS, FALSE, lua_tonumber(Ls, -1));
    int Value = lua_tonumber(Ls, -3);
    WriteProcessMemory(ProcHand, (LPVOID)lua_topointer(Ls, -2), &Value, sizeof(Value), 0);

    return 1;

}

void Load() {
    L = luaL_newstate();
    lua_register(L, "GetProcByName", CustomGetProcessByName);
    lua_register(L, "WriteMemInt", CustomWriteMemInt);



}
int main() {
    Load();
    luaL_dostring(L, "a = GetProcByName('ac_client.exe')");
    luaL_dostring(L, "WriteMemInt(a, 0x0293AA60, 9999)");
    system("Pause");
}

I know the writing memory function works because I did it without this..

lua_topointer returns a pointer to a lua object. You just want to use lua_tonumber instead.

Note that a number is normally a double so will not be able to hold 64-bit addresses, in your example it should work though as the address appears to be 32-bit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM