如何创建一个自定义的.NET的DLL加载为lua(forWindows)?自定义、创建一个、加载、DLL

2023-09-04 09:51:47 作者:盖世垃圾

我们正在做的.NET框架的一个项目,并希望使大部分功能可后来的Lua脚本。我以为我可以编写一个DLL并加载到Lua脚本LuaInterface的帮助。但不知何故,没有工作。

We are doing a project in .NET framework and want to make most of its functionalities available later for Lua scripts. I thought I could compile a dll and load it to Lua script with the help of LuaInterface. But somehow it did not work.

什么工作如下:

require 'luanet' 
luanet.load_assembly("System.Windows.Forms")
Form = luanet.import_type("System.Windows.Forms.Form")
Button = luanet.import_type("System.Windows.Forms.Button")
form1 = Form()
button1 = Button()

正如你所看到的,在这里我加载标准装配和类型,并没有造成太大的问题。 不过,如果我有我自己的DLLLuaTest.NET 4.0下编译并尝试加载它LUA。它不起作用。我写的东西一样,

As you can see, here I'm loading standard assembly and types, which didn't cause much problem. However, if I have my own dll 'LuaTest' compiled under .NET 4.0 and try to load it in LUA. It did not work. I wrote something like,

require 'luanet'
luanet.load_assembly("LuaTest")
PlanetarySystem = luanet.import_type("LuaTest.PlanetarySystem")
solarSystem = PlanetarySystem()

其中PlanetarySystem是一类在LuaTest。如果我跑这片code,跨preTER会说:试图调用全球PlanetarySystem(一个零值)。

where 'PlanetarySystem' is a class in LuaTest. If I run this piece of code, the interpreter would say: attempt to call global 'PlanetarySystem' (a nil value).

我也试过另一种方式来加载DLL:

I also tried another way to load the dll:

package.path = package.path .. ";" .. "/?.dll"
require 'luanet'
require 'LuaTest'

运行后,除preTER抛出:LUA:错误加载模块LuaTest从文件'\ LuaTest.dll。':指定的程序无法找到

After run, the interpreter throws: lua: error loading module 'LuaTest' from file '.\LuaTest.dll': The specified procedure could not be found.

我是相当新手到.NET框架和LuaInterface。也许我做了完全错误的。请帮我在这。非常感谢!

I'm quite a newbie to .NET framework and LuaInterface. Perhaps I did something utterly wrong. Please help me on this. Many thanks!

编辑:也许我应该有一个入口点为lua在我的DLL,表明这个dll是LUA加载???

Perhaps I should have an 'Entry Point' for Lua in my dll to indicate that this dll is LUA loadable???

编辑:Lua的不LUA。没有进攻,以讲葡萄牙语的人。我使用的是必须与.NET 4.0兼容的Lunanet,否则第一块code是行不通的。

Lua not LUA. No offense to Portuguese speaking people. The Lunanet I'm using must be compatible with .NET 4.0, otherwise the first piece of code would not work.

推荐答案

我相信你是混乱的程序集名是您试图导入的类型的完全限定名的必需部分。该错误表示PlanetarySystem类是一个零值,这意味着它可能无法找到一个类由完全合格的名称。我将肯定在您的类是在命名空间。

I believe you are confusing the assembly name with being a required part of the fully qualified name of the type that you are trying to import. The error indicates that the PlanetarySystem class is a "a nil value", meaning that it likely couldn't find a class by that fully qualified name. I would be certain on the namespace that your class was in.

二,如果我的第一个建议是不行的,你可能需要让你的类标记有ComVisible特性,使得Lua的引擎能看到你的类。

Second, if my first recommendation doesn't work, you may need to make your classes ComVisible so that the Lua engine can see your classes.

http://msdn.microsoft.com/en-us/library/ ms182157.aspx

 
精彩推荐