Lua Alien - Pointer Arithmetic and Dereferencing -


my goal call windows' getmoduleinformation function moduleinfo struct back. working fine. problem comes result of me wanting pointer arithmetic , dereferences on lpvoid lpbaseofdll part of moduleinfo.

here code call function in lua:

require "luarocks.require" require "alien"  sizeofmoduleinfo = 12   --gotten sizeof(moduleinfo) visual studio  moduleinfo = alien.defstruct{     {"lpbaseofdll", "pointer"};    --does need 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  --if there error, exit     return 0 end  local dataptr = mod.lpbaseofdll  --now how do pointer arithmetic and/or dereference "dataptr"? 

at point, mod.sizeofimage seems giving me correct values expecting, know functions being called , struct being populated. however, cannot seem pointer arithmetic on mod.lpbaseofdll because userdata.

the information in alien documentation may address i'm trying these:

  1. pointer unpacking

alien provides 3 convenience functions let dereference pointer , convert value lua type:

alien.tostring takes userdata (usually returned function has pointer return value), casts char*, , returns lua string. can supply optional size argument (if don’t alien calls strlen on buffer first).

alien.toint takes userdata, casts int*, dereferences , returns number. if pass number assumes userdata array number of elements.

alien.toshort, alien.tolong, alien.tofloat, , alien.todouble alien.toint, works with respective typecasts. unsigned versions available.

my issue those, need go byte-by-byte, , there no alien.tochar function. also, , more importantly, still doesn't solve problem of me being able elements outside of base address.

  1. buffers

after making buffer can pass in place of argument of string or pointer type.

...

you can pass buffer or other userdata new method of struct type, , in case backing store of struct instance creating. useful unpacking foreign struct c function returned.

these seem suggest can use alien.buffer argument of moduleinfo's lpvoid lpbaseofdll. , buffers described byte arrays, can indexed using notation: buf[1], buf[2], etc. additionally, buffers go bytes, ideally solve problems. (if understanding correctly).

unfortunately, can not find examples of anywhere (not in docs, stackoverflow, google, etc), have no idea how this. i've tried few variations of syntax, every 1 gives runtime error (others not work expected).

any insight on how might able go byte-by-byte (c char-by-char) across mod.lpbaseofdll through dereferences , pointer arithmetic?

i need go byte-by-byte, , there no alien.tochar function.

sounds alien.tostring has covered:

alien.tostring takes userdata (usually returned function has pointer return value), casts char*, , returns lua string. can supply optional size argument (if don’t alien calls strlen on buffer first).

lua strings can contain arbitrary byte values, including 0 (i.e. aren't null-terminated c strings), long pass size argument alien.tostring can data byte buffer, aka lua string, , whatever please bytes.

it sounds can't tell start @ arbitrary offset given pointer address. easiest way tell sure, if documentation doesn't tell you, @ source. trivial add offset parameter.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -