How to call a function in C DLL from Lua?

I have a DLL written in C. It is legacy code and the source code cannot be modified. I want to be able to call some functions inside the DLL from Lua.

< p>For example, I want to do something like this:

-- My Lua File
include(myCppDll.dll)

function callCppFunctionFromDll ()
local result = myCppFunctionFromDll(arg1, arg2)
--Do something with result here
end

Is such a thing possible?

If Alien cannot meet your needs, and if the DLL has a powerful object-oriented interface, you You need to get the members and methods of the object and only call the exported functions, then it may not be easy to use, then you should look at generating a wrapper DLL, which connects the legacy API from the DLL to Lua.

This can be done using a wrapper generator (such as Swig), which will write wrappers for Lua and many other scripting languages ​​based on class and function declarations, usually only an existing .h file is required as input.

Lua is also simple enough code, because it may be easier to write your own wrapper manually in C. For this reason, create a Lua callable module in C starting from the standard recipe, and implement the function entry of transferring parameters from the Lua stack The form suitable for each API call, call the DLL, and push any results back to the Lua stack. This is also the use of Lua to be able to return more for those functions in the DLL that must use the output pointer to process the second (or more) return value There is a place for the ability of the results. The discussion of the issues and some sample codes are provided on the Lua user’s Wiki.

There is also a page dedicated to binding Lua to other languages ​​on the Lua user’s Wiki.< /p>

I have a DLL written in C. It is a legacy code, and the source code cannot be modified. I want to be able to call some functions inside the DLL from Lua.

For example, I want to do something like this:

-- My Lua File
include(myCppDll.dll)

function callCppFunctionFromDll()
local result = myCppFunctionFromDll(arg1, arg2)
--Do something with result here
end

Is such a thing possible?

If Alien cannot meet your needs, and if the DLL has a powerful object-oriented interface, you need to get the members and methods of the object and only call the exported Function, then it may not be easy to use, then you should look at generating a wrapper DLL, which connects the legacy API from the DLL to Lua.

This can use a wrapper generator (such as Swig) To complete it, it will write wrappers for Lua and many other scripting languages ​​based on class and function declarations, usually only the existing .h file is required as input.

Lua is also simple enough code, because in It may be easier to write your own wrapper manually in C. For this reason, create a Lua callable module in C starting from the standard recipe, and implement the function of transferring parameters from the Lua stack into a form suitable for each API call, call the DLL, and Push any result back to the Lua stack. This is also the place to take advantage of Lua’s ability to return multiple results for those functions in the DLL that must use the output pointer to process the second (or more) return value. On the Lua user’s Wiki A discussion of the issues and some sample codes are provided.

There is also a page dedicated to binding Lua to other languages ​​on the Lua user’s Wiki.

p>

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3266 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.