0

I have a C# program executing Lua scripts using the LuaInterface. So far, it is working well, unless my Lua script requires a specific package, like LuaXML.

I want to send an XML string from C# to a Lua function.

This is the XML, saved in C:\temp:

<?xml version="1.0" encoding="utf-16" ?>
<library id="101">
    <book id="10" author="Balzac" title="Le Père Goriot"></book>
    <book id="20" quantity="Stendhal" price="Le Rouge et le noir"></book>
</library>

This is the C# code:

        Lua lua = new Lua();

        XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.Load(@"C:\temp\library.xml");

        lua.DoFile(@"C:\temp\myScript.lua");

        LuaFunction luaFunction = lua.GetFunction("transformXML");
        Object o = luaFunction.Call(xmlDocument.OuterXml); 

This is the Lua script, saved on C\temp:

require("LuaXML")

function transformXML(input)

x = xml.eval(input)

output = nil

local library = x:find("library")

return library[1].id

end


local s = '<?xml version="1.0" encoding="utf-16" ?><library id="101"><book id="10" author="Balzac" title="Le Père Goriot"></book><book id="20" quantity="Stendhal" price="Le Rouge et le noir"></book></library>'

print(transformXML(s))

This is the error I got:

C:\temp\myScript.lua:1: module 'LuaXML' not found:
no field package.preload['LuaXML']
no file '.\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\lua\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\lua\LuaXML\init.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML.lua'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML\init.lua'
no file 'C:\Program Files (x86)\Lua\5.1\lua\LuaXML.luac'
no file '.\LuaXML.dll'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\LuaXML.dll'
no file 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\loadall.dll'

Do I have to put all these files (.lua, .dll) manually to all these locations?

The Lua script works when I execute it from the Lua console. It returns 10, the id of the first book.

Also, as mentionned, I was able to call Lua functions from C# as long as there is no 'require' in the script. It is not only LuaXML that throws that type of exception. Any package would.

I've played with the environment variables, but was not successful. However, I am not very good at that.

Thank you in advance for the help.

1 Answers1

1

You have to put the LuaXML shared library into one of those locations (see the output, especially the last lines which look for a DLL), so that Lua require can find it. Alternatively, you can change package.cpath to point where your LuaXML DLL is saved. Example:

package.cpath = package.cpath .. ";./libs/?.dll"
Michal Kottman
  • 16,375
  • 3
  • 47
  • 62
  • There is neither a LuaXMl.luac file (with a c in the suffix), nor a LuaXML.dll (only a LuaXML_lib.dll). However, I have to put require("LuaXML") for the script to work directly (not from C#). I do not have access to the Microsoft Shared folder. It tells me that I have to be an administrator (I am one, btw). I have try the package.cpath at many place: from the console or in the script. It does not work either. I am sure it is quite simple and that I am probably missing something. Thanks in advance if you have any other instructions that can help. – Auguste Derlangue Feb 20 '12 at 03:34