C#外部库(Lua)调用问题

我是C#(VS2010).Net(4.0)编程的新手,我遇到了自从几天以来我自己无法解决的问题。

我在我的C#代码中使用外部脚本语言(Lua)。

为此,我使用为.Net 4.0构建的LuaInterpreter

首先尝试:该项目是一个控制台应用程序 – >当我尝试调用Lua类时,程序运行正常。

第二次尝试:该项目是一个从Excel中使用的类Librrary COM – >类库编译正常,我的用户定义函数在Excel中正常工作。 但当我试图打电话给Lua课时,它崩溃说Lua集会失踪了。

Could not load file or assembly 'lua51, Version=0.0.0.0, Culture=neutral, PublicKeyToken=1e1fb15b02227b8a' or one of its dependencies. Strong name validation failed. (Exception from HRESULT: 0x8013141A) 

重现问题:

1-您需要从http://www.mdome.org/2011/05/16/luainterface-for-csharp-net-4-custom-build/获取LuaInterface .Net 4.0

2-添加LuaInterface作为项目中的参考

3-将Lua51 DLL复制到建筑目录中(我也将Excel表格放在那里)

4-复制类库的代码

 using System; using System.Collections.Generic; using System.Runtime.InteropServices; using Microsoft.Win32; using Excel = Microsoft.Office.Interop.Excel; using LuaInterface; namespace POC { [ClassInterface(ClassInterfaceType.AutoDual)] [ComVisible(true)] public class Functions { public int test() { Lua lua = new Lua(); return 0; } #region Class in Excel [ComRegisterFunctionAttribute] public static void RegisterFunction(Type type) { Registry.ClassesRoot.CreateSubKey( GetSubKeyName(type, "Programmable")); RegistryKey key = Registry.ClassesRoot.OpenSubKey( GetSubKeyName(type, "InprocServer32"), true); key.SetValue("", System.Environment.SystemDirectory + @"\mscoree.dll", RegistryValueKind.String); } [ComUnregisterFunctionAttribute] public static void UnregisterFunction(Type type) { Registry.ClassesRoot.DeleteSubKey( GetSubKeyName(type, "Programmable"), false); } private static string GetSubKeyName(Type type, string subKeyName) { System.Text.StringBuilder s = new System.Text.StringBuilder(); s.Append(@"CLSID\{"); s.Append(type.GUID.ToString().ToUpper()); s.Append(@"}\"); s.Append(subKeyName); return s.ToString(); } #endregion } } 

崩溃的function是从Excel调用时的测试function

我会对此表示感谢

它似乎已签名,尝试将Lua51放入GAC并查看它是否有效。 也许您可以尝试将Lua15.dll放在excel.exe的相同路径中。

我在.NET,LuaInterface和Lua5.1上在64位计算机上进行交互时遇到了很多问题。 Lua5.1只编译32位,这要求你(我相信)将LuaInterface项目构建为32位。 尝试在.NET项目中将“项目 – >属性 – >构建 – >平台目标”更改为“x86”。