Can a C++ dll file be loaded in Lua?
c++, dll, lua
Solution
Yes, it can be done. Expose a C-function loader `luaopen_MyAPI`, where you can call a function that uses any kind of C++ Lua Wrapper, such as LuaBridge, LuaBind or others. If your calls in C++ don't conform to the rules of the bindings, such as lifetime management, passing objects by value, etc, you might need to wrap the classes into bindable classes.
For an example see pugilua
- pugilua_lib.h - module loader API
- pugilua_lib.cpp - wrapper classes and a LuaBridge binding
- pugilua.cpp - calling the bindings from the module loader
Problem
I need to load a DLL file in Lua to connect different APIs. I know that C type dlls can be loaded, but what I have is a dll file produced in C++. The code (in C++) that produced this library was of the form: ``` // MyAPI.h namespace MyAPI { public class MyFirstClass { public: MyFirstClass(); void performSomeMethod(int arg); } } ``` which then produced the dll file `MyAPI.dll`. When I now try to import this in Lua, using: ``` require "MyAPI" ``` it immediately gives the error: `error loading module 'MyAPI' from file '.\MyAPI.dll': The specified procedure could not be found`. I do not understand what this means, or how to get rid of it. Can C++ libraries in general not be included by Lua (i.e. should I write another C wrapper?) or is there a way to do this?