gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 18:41:23 26 4
gpt4 key购买 nike

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

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

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

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

第二次尝试:该项目是从 Excel 使用的类库 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- 您需要从 获取 LuaInterface .Net 4.0 http://www.mdome.org/2011/05/16/luainterface-for-csharp-net-4-custom-build/

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
}
}

崩溃的函数是从 Excel 调用时的测试函数

我愿意为此提供任何帮助谢谢

最佳答案

因为好像是signed,所以尝试把Lua51放到GAC里看看能不能用。也许您甚至可以尝试将 Lua15.dll 放在与 excel.exe 相同的路径中。

关于C#外部库(Lua)调用问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6856826/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com