Lua Alien - pointer arithmetic and dereference

My goal is to call the Windows GetModuleInformation function to get MODULEINFO back. It all works great. The problem arises as a result of the fact that I want to do arithmetic of pointers and markup on LPVOID lpBaseOfDllwhich is part MODULEINFO.

Here is my code for calling a function in Lua:

require "luarocks.require"
require "alien"

sizeofMODULEINFO = 12   --Gotten from sizeof(MODULEINFO) from Visual Studio

MODULEINFO = alien.defstruct{
    {"lpBaseOfDll", "pointer"};    --Does this need to be a buffer? If so, how?
    {"SizeOfImage", "ulong"};
    {"EntryPoint", "pointer"};
}

local GetModuleInformation = alien.Kernel32.K32GetModuleInformation
GetModuleInformation:types{ret = "int", abi = "stdcall", "long", "pointer", "pointer", "ulong"}

local GetModuleHandle = alien.Kernel32.GetModuleHandleA
GetModuleHandle:types{ret = "pointer", abi = "stdcall", "pointer"}

local GetCurrentProcess = alien.Kernel32.GetCurrentProcess
GetCurrentProcess:types{ret = "long", abi = "stdcall"}

local mod = MODULEINFO:new() --Create struct (needs buffer?)

local currentProcess = GetCurrentProcess()
local moduleHandle = GetModuleHandle("myModule.dll")
local success = GetModuleInformation(currentProcess, moduleHandle, mod(), sizeofMODULEINFO)

if success == 0 then  --If there is an error, exit
    return 0
end

local dataPtr = mod.lpBaseOfDll

--Now how do I do pointer arithmetic and/or dereference "dataPtr"?

At this point mod.SizeOfImage, it seems to give me the correct values ​​that I expect, so I know that the functions are being called and the structure is populating. However, I cannot do arithmetic of pointers to mod.lpBaseOfDll, because it is UserData.

The only information in the Alien Documentation that can address what I'm trying to do is this:

  • Unpacking Pointers

Alien , Lua:

alien.tostring userdata ( ), char * Lua . ( strlen ).

alien.toint userdata, int *, . , userdata - .

alien.toshort, alien.tolong, alien.tofloat alien.todouble alien.toint, . .

, , alien.tochar. , , , .

.

...

struct type, . external struct, C.

, alien.buffer MODULEINFO LPVOID lpBaseOfDll. , : buf[1], buf[2] .. , , . ( ).

, ( , stackoverflow, Google ..), , . , ( , ).

, (C char -by- char) mod.lpBaseOfDll ?

+4
1

, alien.tochar.

, alien.tostring :

alien.tostring userdata ( ), char * Lua. ( Alien strlen ).

Lua , 0 (.. , C), , alien.tostring, , aka Lua string, , , .

, , . , , - . , .

+1

Source: https://habr.com/ru/post/1584636/


All Articles