Giter Club home page Giter Club logo

Comments (9)

viniciusjarina avatar viniciusjarina commented on July 22, 2024

Please be more specific

from keralua.

rdscfh avatar rdscfh commented on July 22, 2024

an anycpu classlib default depens on a lua54.dll(x86)
when build with a classlib It build output with a x64 lua54.dll
when run with the classlib it show
System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)

from keralua.

rdscfh avatar rdscfh commented on July 22, 2024

dotnet framework 4.5

from keralua.

viniciusjarina avatar viniciusjarina commented on July 22, 2024

What are you trying to do? What are the repro steps?

from keralua.

rdscfh avatar rdscfh commented on July 22, 2024

.net framework4.5 classlib

using NLua;

namespace luali
{
    public class Class1
    {
        public static void Run()
        {
            using(var lua=new Lua())
            {

            }
        }
    }
}

.net framework4.5 Console app

using luali;
using System;

namespace luan2
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Class1.Run();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
            Console.Read();
        }
    }
 }

build and run
System.BadImageFormatException: 试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)
在 KeraLua.NativeMethods.luaL_newstate()
在 NLua.Lua..ctor()
在 luali.Class1.Run() 位置 C:\Users\heyz\Desktop\luali\Class1.cs:行号 9
在 luan2.Program.Main(String[] args) 位置 C:\Users\heyz\Desktop\luan\luan2\Program.cs:行号 12

from keralua.

viniciusjarina avatar viniciusjarina commented on July 22, 2024

The problem is because the NuGet target copies the x86 since the main program has the flag Preffer32 bit.
Try to uncheck that flag
ConsoleApp2.zip

from keralua.

rdscfh avatar rdscfh commented on July 22, 2024

i found that in the KeraLua.targets setting

<PropertyGroup>
        <!-- handle x86/x64 specifically -->
        <PreferredNativeLua Condition=" '$(PreferredNativeLua)' == '' and ( '$(PlatformTarget)' == 'x64' or '$(PlatformTarget)' == 'x86' ) ">$(PlatformTarget)</PreferredNativeLua> 
        <!-- handle Any CPU, considering Prefer32Bit -->
        <PreferredNativeLua Condition=" '$(PreferredNativeLua)' == '' and '$(Prefer32Bit)' == 'False' ">x64</PreferredNativeLua>  
        <PreferredNativeLua Condition=" '$(PreferredNativeLua)' == '' and '$(Prefer32Bit)' == 'True' ">x86</PreferredNativeLua> 
        <!-- fall back to x64 on 64-bit machines -->
        <PreferredNativeLua Condition=" '$(PreferredNativeLua)' == '' and $([System.Environment]::Is64BitOperatingSystem) ">x64</PreferredNativeLua> 
        <!-- fall back to x86 -->
        <PreferredNativeLua Condition=" '$(PreferredNativeLua)' == '' ">x86</PreferredNativeLua>
    </PropertyGroup>

but a classlib can never set the flag Preffer32 bit.
because my main app and my classlib was built separate ,so that will be a problom

from keralua.

viniciusjarina avatar viniciusjarina commented on July 22, 2024

Change the Prefer32Bit flag from your final .exe 😉

from keralua.

viniciusjarina avatar viniciusjarina commented on July 22, 2024

Let me try to explain what is happening:

When you run a AnyCPU assemply that contains the flag Prefer32 bit the CLR may or may not run your application as x64.
In your case when you reference a class library (AnyCPU) CLR is loading your application as x64.

The way that the .targets works, will copy the x64 version if the flag Prefer32 is not set.

There a couple of options you can workaround this:

  • You can move your assemblies to a specific target e.g x64
  • You can remove the flag Prefer32bits and the .targets will copy the x64 bit
  • You can p/Invoke SetDllDirectory and specify the directory you want to load the DLL depending if you are running x86 or x64 (Some binding libraries do this, but I don't think is a good idea since that afect the whole process I guess is up to the user if they want to change the DLL directory search.

I hope this help you thank you.

from keralua.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.