I found some references on this but I was not able to make them work. I have a Debian box with mysql and mysql-proxy. I am intercepting the SQL queries with the LUA script.
function read_query(packet)
if packet:byte() ~= proxy.COM_QUERY then
print("error read (COM_QUERY)")
end
local query = packet:sub(2)
print ("query : " .. query )
//Transformation here
return proxy.PROXY_SEND_QUERY
end
I want to parse and process the query so I can rewrite it with some c functions I already have developed. I am trying to find the way to call this fucntions but the only way I have found asumes that the c MAIN function starts the LUA registering process.
Is there any way to make the LUA script call the function in a compiled C file?
Any example of how should I make (LUA) and receive (C) the call?
Extract from lua.org
When we say that Lua can call C functions, this does not mean that Lua can call any C function.(There are packages that allow Lua to call any C function, but they are neither portable nor robust.) ...
... for a C function to be called from Lua, we must register it, that is, we must give its address to Lua in an appropriate way.
You should have a look here
SWIG is a good option to generate bindings for you: www.swig.org. You create a .i file in which load your C headers, and SWIG generates all the binding code for you. You then compile the generated code, link it to your C library and Lua library, and in your script you put require 'yourCLibrary', you can do what you want. Very practical, and your wrapper can be used to access yourCLibrary from other languages like Python and C#.
Related
Most of the examples online of C-to-Lua implementations show the C program messing around with Lua States and Compilers and even compiling both files in a special way to work properly.
But is there a way to call a C function from Lua without the C program knowing it will be used in a Lua program?
I mean something like loading a Lua library: to do that, I just call
module = require("/path/library")
module.doSomething()
from a standard Lua interpreter, while the examples of C-to-Lua I found online say not only you have to modify your C program to fit whit Lua, but you also need to compile them in a special way and things like that.
So, again, is it possible to call a C function from a standard Lua interpreter whitout the C module knowing it will be used in Lua?
I don't know if it can be of any help, but I'm using LuaJIT.
Use the FFI library that is integrated into LuaJIT.
I am working on a program in C, and I am having trouble with libconfig.h. Because of this, I think if I could have my program download an external function from the Internet (using libcurl.h) and have my program treat it as a compiled and linked shared object, that would be perfect. It would need to work on all desktop platforms (Windows, Mac, and Linux), so no .dll's, and would have to be downloaded by the program, treated as a function, and then get deleted by the program. So, my question is: is that possible in C?
The reason that I need to download it separately is because the function would need to be updated regularly, and requiring the user to download a new version of the program regularly would defeat the purpose of the program.
Well the closest to what you ask for would be this
Download .so/.dll using curl
Dynamically load .so/.dll into your process
set up function pointer in your process to point to a function in .so/.dll
On Windows:
HMODULE handle = LoadLibrary("mylib.dll");
if (handle)
myfunc = GetProcAddress(handle, "myfunc");
To unload call
FreeLibrary(handle)
It decreases ref count, and the DLL is actually unloaded when ref count hits 0.
On Linux, check this post:
How do I load a shared object in C++?
You can't just treat it as compiled; you would have to do one of two things:
Actually compile it on the fly, then load it as a dynamic library, which requires ensuring that there is a compiler on the system and will probably cause an unholy mess of errors on the user end.
Build your own C parser to interpret the external function, which is no small feat.
Far simpler solution: just write a function that works and compile platform-specific versions of it into your binary (or a library, if you prefer) before shipping the product.
You could link a Python interpreter into your program and have it execute a Python version of your function.
This approach would actually work with different languages, such as Java, Ruby, etc.
I have a file called example.i that allows the calling of a number of C functions.
Is it possible to call these functions from my TCL code? I have seen some ways to call C functions from TCL but I found them very hard to understand or incomplete, at least for a TCL rookie such as myself. How can this be done?
The .i suffix suggests you are generating an interface using swig. If so, swig has a -tcl option that should generate code for an extension you can load into Tcl - see Swig page on the Tcl wiki
It's pretty extensively documented already.
See here hello_world and here SWIG for example.
I would like to have a detailed explanation.
How would I use the Lua source code to create a Lua interpreter that will execute given blocks of Lua code? The blocks of Lua code would be sent as a char.
you need a call to lua_load to compile the block of code, and then a call to lua_call to run it. For a really good example of how this is done, take a look at the example provided here:.
The first argument to any Lua api function is always an interpreter state, which is the return value of lua_open()
The example actually uses luaL_loadbuffer which wraps the call to lua_load to make compiling a c string a bit easier. you can read how to use it in the chapter of the reference manual that covers the The Auxiliary Library. This leaves a lua chunk at the top of the lua stack, which can then be invoked with lua_call, but the example uses lua_pcall, which provides a bit of error trapping. since the chunk you just compiled doesn't take any arguments (it's a chunk not a function) and doesn't have any return value you'd be interested in, and you want to see the error exactly as it was produced, all of the arguments besides the first (which is always the lua interpreter state) can be zeros.
http://www.lua.org/manual/2.1/subsection3_7_6.html
http://lua-users.org/lists/lua-l/2006-10/msg00405.html an example
http://www.debian-administration.org/articles/264 c++, same type thing
This will tell you how to call Lua from C.
Some time ago, I had created a DLL to be used in another C program. Basically I exposed specific functions by using the following within my dll:
void __declspec(dllexport) MyFunc(myFirstArg, mySecondArg);
Then I added an external file (MyExposedDll.h) with all exposed functions and structures to the new C program and included it:
#include MyExposedDll.h
Now how can I use this dll (or mainly a dll) to a Cobol function? I need to expose a function that has two char* arguments and returns a boolean.
Thanks,
Sun
This should not be difficult in an IBM Z/OS environment with LE support.
Capture the boolean result using the
COBOL CALL RETURNING
form of the CALL statement. The string arguments are passed just as any other arguments in a COBOL CALL statement.
The only thing to be wary of is that C uses Null terminated strings whereas COBOL generally does not. You should review
how to handle null terminated strings in
COBOL.
Have a look at: Using COBOL DLLs with C/C++ programs this gives a really simple example showing a call to a C++ function returning a function pointer.
EDIT
I may have missed part of your question... When your COBOL program is linked-edited, you need to provide the your DLL IMPORT file so it can be bound. See linking DLL's.
EDIT 2
Based on your comments, I take it you are running your application on a Z/OS box. Visual Studio is a PC based product so I am guessing that you develop your code there but deploy it under Z/OS? In order to get the COBOL program to recognize your DLL you need to create a "side file" from your C program when it is compiled. This "side file" contains the DLL structures needed by the linker when the COBOL program is linked. You should be able to get the process worked out from the links provided above.