3

I'm new to programming in C# (VS2010) .Net (4.0) and I'm encountering I couldn't solve by myself since some days already.

I'm using an external scripting language (Lua) in my C# code.

To do so I use LuaInterpreter built for .Net 4.0

First try: The project is a console application -> the program works fine when I try to call a Lua class.

Second try: The project is a class Librrary COM used from Excel -> The class library compile fine and my user defined functions work fine within Excel. But when I try to call a Lua class it crashed saying that the Lua assembly is missing.

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)

To reproduce the problem :

1- You need to get LuaInterface .Net 4.0 from http://www.mdome.org/2011/05/16/luainterface-for-csharp-net-4-custom-build/

2- Add LuaInterface as a reference in your project

3- Copy the Lua51 DLL in the building directory (I put my Excel sheet there too)

4- Copy the code for the Class Library

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

The function that crashed is the test function when called from Excel

I would take any help on that Thanks

2 Answers2

0

SInce it appears to be signed, try to put Lua51 into the GAC and see if it works. Probably you can try even by putting Lua15.dll in the same path of excel.exe.

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
  • Thanks for the advices. I couldn't put the DLL in the GAC as gacutil failed saying "Unknown Error". Whereas putting the dll into Excel.exe directory works fine. This proves that the Dll and the program work fine but it is not a long term solution... Do you know how to track the folder where the dll is searched when I call my classes? To visualize: Excel -> User DLL (my assembly) -> LuaInterface.dll -> Lua51.dll The one that needs to be in Excel directory is Lua51.dll – Riadh Abdelhedi Jul 28 '11 at 18:09
0

I've had lots of issues with .NET, LuaInterface, and Lua5.1 interracting on 64-bit machines. Lua5.1 only compiles 32-bit and this requires you to (I believe) build the LuaInterface project as 32-bit as well. Try changing "Project -> Properties -> Build -> Platform Target" to "x86" in your .NET projects.

kyork
  • 713
  • 5
  • 14