Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua strings won't concatenate

I am trying to concatenate a variable to make my output "prettier", however it seems to output as an empty string when concatenated with other string values.

The output code excerpt looks like this:

local name = ""

local SendMessage = alien.User32.SendMessageA
SendMessage:types{ret = "int", abi = "stdcall", "pointer", "int", "int", "string"}

SendMessage(handle, LB_GETTEXT, index, name)

print(type(name)) --To verify that it is a string type
print(name) --Prints the name "Sample 1" perfectly fine
print("Title: " .. name .. "\n") --Doesn't print the name variable "Sample 1"

Output:

Output

The name variable is set via Alien for Lua call to the WINAPI SendMessage function from User32.dll. I think this might be part of the issue, however as you can see above, the variable is set to type (Lua) "string" and prints fine when called by itself. However whenever you concatenate it with anything, it acts like the empty string (or something similar).

EDIT: I have also tried tostring(name) and alien.tostring(name). Neither of which fix the issue.

like image 553
MrHappyAsthma Avatar asked May 18 '26 07:05

MrHappyAsthma


1 Answers

After a lot of trial and error as well as reading the Lua Alien documentation, I discovered that using an Alien Buffer solves the issue of converting between the hybrid C-Lua string and a native Lua string:

local name = alien.buffer()
print("Title: " .. name:tostring() .. "\n")

You can then use the tostring() method on the variable at any time to get the native Lua string.

NOTE: The buffer variable will be of type userdata. If you try to print it out by itself, it will show its contents, but if you try to concatenate it like a string (without the :tostring()) Lua will throw an error because you cannot concatenate a string with userdata.

like image 112
MrHappyAsthma Avatar answered May 21 '26 07:05

MrHappyAsthma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!