Using hex in Lua scripts

Started by
10 comments, last by Maega 21 years, 3 months ago
Hey, I wrote a function in C that takes a DWORD parameter that is hexadecimal (read prefixed with 0x) Well, Lua dies when I include that function in the script. My other C functions work fine, but anything that uses hex just dies out. Any suggestions? The code is fine (I typecast the double returned by tonumber to a DWORD (perhaps that could be it..) ) If that is the problem, could someone explain to me or show me an example on how to add a user defined type to fix this problem.
Advertisement
Well, a DWORD is a 32-bit int. So can you just convert your hex numbers to ints? Then you can easily type it with lua''s doubles ( i think they are doubles... ) Post some code if i didnt quite understand

Brian
Brian J
Oops.. just noticed I didn''t include why the numbers are hex

They are memory addresses. I am unsure that you could convert them to unsigned longs and type cast them correctly.
So you''re exposing the function to Lua? Post the C function here, I think I can help.

Don''t listen to me. I''ve had too much coffee.
Ok..

Small C function.. =P

void WriteByte(DWORD address, BYTE value)
{
WriteProcessMemory(processhandle, (void *)address, (void *)&value, 1, NULL);
}


glue function looks like this

int l_WriteByte(lua_State* luaVM)
{
DWORD address = 0;
BYTE value = 0;


address = (DWORD)lua_tonumber(luaVM, 1);
value = (BYTE)lua_tonumber(luaVM, 2);

theScriptWindow->WriteByte(address, value);

return 1;
}
hexidecimal is just another way to represent a number, just like binary or the "human-readable" way decimal. "0xD431" is exactly the same number as "54321" in decimal or "1101010000110001" in binary, when you type it in your code the computer sees it as binary anyway.
i don''t know lua but you can convert the numbers into decimal or whatever lua understands, and it will mean the same thing to the computer.
--- krez ([email="krez_AT_optonline_DOT_net"]krez_AT_optonline_DOT_net[/email])
The internal representation of numbers in Lua is as doubles. A double does not have enough precision to store an unsigned 32-bit integer. And anyway, that''s not what it''s for.

Pointers in Lua should be handled with userdatas, not numbers. If you''re using Lua 4, you can create userdatas with arbitrary pointers, so just pass in the pointer to lua_pushuserdata. In Lua 5, you could use light-userdatas to accomplish this, but a more robust solution (light userdatas are never supposed to be exposed to scripts) would be to put the pointer in a memory location pointed to by a userdata. Look at the macros lua_boxpointer and lua_unboxpointer for details.

Don''t listen to me. I''ve had too much coffee.
Pointers in lua I never figured out..

doesn''t really get explained enough for me to understand in the manual :/
Basically, a "userdata" is a piece of memory that your program can use, but that is maintained by Lua. On the C side, you can obtain a pointer to this memory, if you need to read/write it. What boxpointer does is create a userdata that refers to a 4-byte area of memory, and then stores the pointer in that memory. So once you have the pointer to the userdata, you dereference it (using unboxpointer) to get the original pointer you stored.

Don''t listen to me. I''ve had too much coffee.
I''ll look into tomorrow.. my brain is popping and fizzling too much to grasp what you mean haha

This topic is closed to new replies.

Advertisement