Archived

This topic is now archived and is closed to further replies.

Maega

Using hex in Lua scripts

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
One of the best ways to understand how to interface lua and C is to look at the lua libraries (all the files in the src/lib directory). They provide good examples of how to interface efficiently and show how various lua tools are "supposed" to be used.


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

Share this post


Link to post
Share on other sites
Well lagged double post =P

Anyway.. I haven''t tried it yet, but what I did try was pulling DWORD * from lua_touserdata, and then dereferencing them to get the value ( *variable), but it still died..

so I guess I''ll HAVE to understand how to do what Sneftel said

Share this post


Link to post
Share on other sites