我是C#(VS2010).Net(4.0)編程的新手,我遇到了自己幾天以來無法解決的問題。C#外部庫(Lua)調用問題
我在我的C#代碼中使用外部腳本語言(Lua)。
要做到這一點我用LuaInterpreter專爲.NET 4.0
首先嚐試: 該項目是一個控制檯應用程序 - >程序工作正常,當我嘗試調用一個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/
2-獲得LuaInterface .NET 4.0添加LuaInterface作爲您的項目
3-副本的引用建築目錄中的Lua51 DLL(我把我的Excel工作表也存在)
4-複製Cl屁股圖書館
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
}
}
功能墜毀時從Excel
稱爲測試功能我會採取任何幫助,對 感謝
感謝您的建議。 我無法將該DLL放入GAC中,因爲gacutil失敗,說「未知錯誤」。 雖然將DLL放入Excel.exe目錄中工作正常。這證明了Dll和程序工作正常,但它不是一個長期的解決方案... 你知道如何跟蹤當我打電話給我的課程的dll被搜索的文件夾? 可視化:Excel - >用戶DLL(我的程序集) - > LuaInterface.dll - > Lua51.dll 需要在Excel目錄中的那個是Lua51.dll –