3F / DllExport

.NET DllExport with .NET Core support (aka 3F/DllExport aka DllExport.bat)
MIT License
940 stars 131 forks source link

An error was thrown when use System.Reflection.Assembly.GetExecutingAssembly() #132

Open wuyu8512 opened 4 years ago

wuyu8512 commented 4 years ago

Hello

I'm trying dllexport for .NET Core When I use System.Reflection.Assembly.GetExecutingAssembly (), it throws an exception

Unhandled exception. System.Runtime.InteropServices.SEHException (0x80004005): External component has thrown an exception.

Unhandled exception: System.IO.FileNotFoundException: Failed to load file or assembly "System.Runtime, Version = 4.2.2.0, Culture = neutral, PublicKeyToken = b03f5f7f11d50a3a" or one of its dependencies. The system can not find the file specified.

v1.7beta3: Manager: Tried both x86, x64 and Direct-Mod/Cecil and Rebase System Object in all possible variants. Project type: .NET Core 3.1.100 Class Library

Code:

using System.Runtime.InteropServices;

namespace Test
{
    public static class Class1
    {
        [DllExport("AppInfo", CallingConvention = CallingConvention.StdCall)]
        public static string AppInfo()
        {
            string ApiVer = "9";             
            string AppID = System.Reflection.Assembly.GetExecutingAssembly().GetName().Name;  
            //string AppID = "Test";
            return string.Concat(ApiVer, ",", AppID);
        }
    }
}

Calling code

using System;
using System.Runtime.InteropServices;

namespace Console_Core
{
    class Program
    {
        [DllImport("Test.dll")]
        public static extern string AppInfo();

        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.WriteLine(AppInfo());
        }
    }
}

Thank you for your contribution to this project

3F commented 4 years ago

By design. Your case requires .NET Standard 2.0 (where export).

Although, more probably I will also consider later our "rebasing feature" for other system-based types in higher versions of .NET Core. See details: https://github.com/3F/DllExport/pull/123

Today, only System Object. Follow the news.

esso23 commented 4 years ago

I reworked my VS solution from https://github.com/3F/DllExport/issues/134 to use .NET Standard 2.0 (x64) library and now I get this exception:

This exception is only displayed when run in console.

Unhandled exception. System.DllNotFoundException: Unable to load DLL 'ExportLib.dll' or one of its dependencies: A dynamic link library (DLL) initialization routine failed. (0x8007045A)
   at ImportApp.ImportClass.ExportMethod()
   at ImportApp.Program.Main(String[] args) in D:\Sync\Projects\DllExport_SEH\ImportApp\Program.cs:line 9

Application exits with code: This is displayed when run in debug mode.

The program '[8740] ImportApp.exe' has exited with code -1073741819 (0xc0000005) 'Access violation'.

Modified VS Solution: DllExport_SEH_2.zip

Anyway, is there a way how to make this work on .NET Standard 2.1? Or are you planning to support it anytime soon? We just migrated our entire codebase to .NET Standard 2.1/.NET Core 3.1 so this would save us a ton of work. I tried reading the links you posted but to be completely honest, I do not understand much of it (I'm not an ILAsm expert).

3F commented 4 years ago

@esso23

That's how MS implements CLR support when pe-module starts to be processed on host machine.

For this repo I can implement some additional behavior in my custom assembler for additional rebasing of the types in final modules. Like it was already for system object (1.7-beta3).

For my other way please follow the news: https://github.com/3F/DllExport/issues/90#issuecomment-546637702

Or are you planning to support it anytime soon?

It can be considered at least only after 1.7 release. Not earlier. Time will tell.

I'm not an ILAsm expert

IL Assembler is mainly frontend. Well, at least this applies to the coreclr implementation which is different from the netfx implementation (no open source). Legit support requires more such processing for System.Private.Corlib / mscorlib.

System.DllNotFoundException: Unable to load DLL 'ExportLib.dll'

I only unpacked your DllExport_SEH_2.zip and I don't see problems in VS 16.4.2. Please open new issue for how to reproduce this error. Thanks.