2

I am trying to load a .net assembly using LuaInterface. If I place the assembly in the same folder as my executable (and my LuaInterface.dll and LuaNet.dll) then everything works great. I would like to move the assembly into a different folder, but when I try that I get "A .NET exception occured in user-code". I have tried:

package.path = package.path .. "C:\\path\\to\\my\\assembly\\?.dll"
luanet.load_assembly("MyAssembly")

and

luanet.load_assembly("C:\\path\\to\\my\\assembly\\MyAssembly")

and

luanet.load_assembly("C:\\path\\to\\my\\assembly\\MyAssembly.dll")

All of these return the .NET exception error. Is there a way to define the path that LuaInterface uses?

justderb
  • 2,835
  • 1
  • 25
  • 38
TobyD
  • 21
  • 4

2 Answers2

0

Your assembly is loaded by your "hosting" executable, and not really loaded by the Lua environment itself. luanet.load_assembly("MyAssembly") simply makes the assembly accessible to the Lua environment. For example (C#):

using MyAssembly; //you can't compile unless MyAssembly is available

namespace LuaRunner
{
    class LuaRunner
    {        
        void DoLua()
        {
            using (LuaInterface.Lua lua = new LuaInterface.Lua())
            {
                lua.DoString("luanet.load_assembly('MyAssembly')");
                //... do what you want within Lua with MyAssembly
            }
        }
    }
}

Your running program is the "host" for Lua to run within, so it's your running program that actually loads MyAssembly. Your executable needs a reference to MyAssembly.dll, (and needs to be able to find it at runtime in the usual locations).

justderb
  • 2,835
  • 1
  • 25
  • 38
tyriker
  • 2,290
  • 22
  • 31
0

To search other assemblies, set the package.cpath variable. For example:

package.cpath = DATA_DIR .. "\\clibs\\?.dll;" .. package.cpath

From the Lua 5.1 documentation:

require (modname)

First require queries package.preload[modname]. If it has a value, this value (which should be a function) is the loader. Otherwise require searches for a Lua loader using the path stored in package.path. If that also fails, it searches for a C loader using the path stored in package.cpath.

package.cpath

The path used by require to search for a C loader.

Lua initializes the C path package.cpath in the same way it initializes the Lua path package.path, using the environment variable LUA_CPATH or a default path defined in luaconf.h.

Community
  • 1
  • 1
SwDevMan81
  • 48,814
  • 22
  • 151
  • 184