Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wuyu8512 opened this issue Dec 18, 2019 · 3 comments
Labels
by design Not a bug. Just a feature. netcore

Comments

@wuyu8512
Copy link

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

@wuyu8512 wuyu8512 added the bug label Dec 18, 2019
@3F
Copy link
Owner

3F commented Dec 18, 2019

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: #123

Today, only System Object. Follow the news.

@3F 3F added netcore by design Not a bug. Just a feature. and removed bug labels Dec 18, 2019
@esso23
Copy link

esso23 commented Jan 14, 2020

I reworked my VS solution from #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
Copy link
Owner

3F commented Jan 14, 2020

@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: #90 (comment)

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
by design Not a bug. Just a feature. netcore
Projects
None yet
Development

No branches or pull requests

3 participants