Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can a C++ dll file be loaded in Lua?

Tags:

c++

dll

lua

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?

like image 964
Yellow Avatar asked Dec 02 '25 05:12

Yellow


1 Answers

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
like image 124
Dmitry Ledentsov Avatar answered Dec 03 '25 21:12

Dmitry Ledentsov



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!