Ascer Posted November 14, 2018 Posted November 14, 2018 Hello, I've read a article about lua functions here: https://debian-administration.org/article/264/Embedding_a_scripting_language_inside_your_C/C_code There are two ways of include lua scripts to your AutoIt code. 1. Run lua functions by Autoit. 2. Register Autoit function and run as lua. It's even possible to apply second way to AutoIt? As we can see on example there a few functions to make it possible. It's a lua file save as average.lua avg, sum = average(10, 20, 30, 40, 50) print("The average is ", avg) print("The sum is ", sum) This is a C functions to run lua. Can be convert to AutoIt ofc. There is a problem with func lua_register(L, "average", average) this function is not even detected in Lua51.dll We can use lua_pushcclosure instead. #include <stdio.h> #include "lua.h" #include "lualib.h" #include "lauxlib.h" /* the Lua interpreter */ lua_State* L; /* The function we'll call from the lua script */ static int average(lua_State *L) { /* get number of arguments */ int n = lua_gettop(L); double sum = 0; int i; /* loop through each argument */ for (i = 1; i <= n; i++) { if (!lua_isnumber(L, i)) { lua_pushstring(L, "Incorrect argument to 'average'"); lua_error(L); } /* total the arguments */ sum += lua_tonumber(L, i); } /* push the average */ lua_pushnumber(L, sum / n); /* push the sum */ lua_pushnumber(L, sum); /* return the number of results */ return 2; } int main ( int argc, char *argv[] ) { /* initialize Lua */ L = lua_open(); /* load Lua base libraries */ lua_baselibopen(L); /* register our function */ lua_register(L, "average", average); /* run the script */ lua_dofile(L, "average.lua"); /* cleanup Lua */ lua_close(L); return 0; }
Earthshine Posted November 14, 2018 Posted November 14, 2018 Is there a question? This is the gelp forum My resources are limited. You must ask the right questions
Developers Jos Posted November 14, 2018 Developers Posted November 14, 2018 (edited) 2 minutes ago, Earthshine said: Is there a question? This is the gelp forum Yes... Gelp? 14 minutes ago, Ascer said: It's even possible to apply second way to AutoIt? Jos Edited November 14, 2018 by Jos Earthshine 1 SciTE4AutoIt3 Full installer Download page - Beta files Read before posting How to post scriptsource Forum etiquette Forum Rules Live for the present, Dream of the future, Learn from the past.
Ascer Posted November 14, 2018 Author Posted November 14, 2018 (edited) Yes. Is there possible to call function lua_pushcfunction via dllcall? Added Zip code Lua.rar Edited November 14, 2018 by Ascer
Ascer Posted November 14, 2018 Author Posted November 14, 2018 (edited) This is an another example from autoit site. Spoiler As an example, the following function receives a variable number of numerical arguments and returns their average and sum: static int foo (lua_State *L) { int n = lua_gettop(L); /* number of arguments */ lua_Number sum = 0; int i; for (i = 1; i <= n; i++) { if (!lua_isnumber(L, i)) { lua_pushstring(L, "incorrect argument to function `average'"); lua_error(L); } sum += lua_tonumber(L, i); } lua_pushnumber(L, sum/n); /* first result */ lua_pushnumber(L, sum); /* second result */ return 2; /* number of results */ } To register a C function to Lua, there is the following convenience macro: #define lua_register(L,n,f) \ (lua_pushstring(L, n), \ lua_pushcfunction(L, f), \ lua_settable(L, LUA_GLOBALSINDEX)) /* lua_State *L; */ /* const char *n; */ /* lua_CFunction f; */ which receives the name the function will have in Lua and a pointer to the function. Thus, the C function foo above may be registered in Lua as average by calling lua_register(L, "average", foo); Im guess that problem is in pushing parameter $lua_CFunction into DLLCALL func see below. Func lua_pushcclosure($lua_CFunction, $index = 0, $p_state = 0) If Not _Lua_Initiate() Then Return SetError(-1, 0, 0) If $p_state = 0 Then $p_state = $__g_pluanewstate Local $aRet = DllCall($__g_hluadll, "none:cdecl", "lua_pushcclosure", "ptr", $p_state, "str", $lua_CFunction, "int", Int($index)) If @error Then Return @error Return 1 EndFunc How to push param such as function ? We have a function: Func Jos() lua_pushstring("Jos") Return 1 EndFunc When i push like this no error but also nothing happen. lua_pushcclosure(Jos) I've check var for Jos. ConsoleWrite("jos: " & VarGetType(Jos) & @CRLF) ;==> return UserFunction So.. how to pas UserFunction in dll "str" ? UserFunction Edited November 14, 2018 by Ascer
Earthshine Posted November 14, 2018 Posted November 14, 2018 (edited) I think you want to put your functions in lua tables, right? and call them that way? I don't think you can do what you are trying to do. I did find this http://www.nongnu.org/cinvoke/lua.html Edited November 14, 2018 by Earthshine My resources are limited. You must ask the right questions
Ascer Posted November 14, 2018 Author Posted November 14, 2018 (edited) @Earthshine Do you know how to register autoit function to later call in via lua? Edited November 14, 2018 by Ascer
Earthshine Posted November 14, 2018 Posted November 14, 2018 not at this time, I come from a C world of long ago. My resources are limited. You must ask the right questions
Earthshine Posted November 14, 2018 Posted November 14, 2018 (edited) Quote lua_pushcfunction [-0, +1, m] void lua_pushcfunction (lua_State *L, lua_CFunction f); Pushes a C function onto the stack. This function receives a pointer to a C function and pushes onto the stack a Lua value of type function that, when called, invokes the corresponding C function. Any function to be registered in Lua must follow the correct protocol to receive its parameters and return its results (see lua_CFunction). lua_pushcfunction is defined as a macro: #define lua_pushcfunction(L,f) lua_pushcclosure(L,f,0) http://www.lua.org/manual/5.1/manual.html#lua_pushcclosure not sure why you are pushing string onto stack with pushstring. Edited November 14, 2018 by Earthshine My resources are limited. You must ask the right questions
Ascer Posted November 14, 2018 Author Posted November 14, 2018 @Earthshine Thanks for answer. As autoit in lua documentation: https://www.autoitscript.com/autoit3/scite/docs/SciTE4AutoIt3/Lua/manual.html #define lua_register(L,n,f) \ (lua_pushstring(L, n), \ lua_pushcfunction(L, f), \ lua_settable(L, LUA_GLOBALSINDEX)) /* lua_State *L; */ /* const char *n; */ /* lua_CFunction f; */ I replaced it to autoIt code: Tried many times.. This examples looks good but not working lua_pushstring("Jos") ; push string to stack lua_pushcclosure(Jos, 0) ; push function index default index 0 lua_setglobal("Jos") ; set global function lua_pushstring("Jos") ; push string to stack lua_pushcclosure(Jos, 0) ; push function index default index 0 lua_settable($LUA_GLOBALSINDEX) ; set global table index Func Jos() lua_pushstring("sdfsdf") Return 0 EndFunc
Ascer Posted November 16, 2018 Author Posted November 16, 2018 nvm solved it. Using powerful LUA to call AutoIt so sweet mmmmmmmmmm..
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now