3F / DllExport

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

Unable to get working using .NET 6 #197

Closed DiskCrasher closed 2 years ago

DiskCrasher commented 2 years ago

. . .

The question is related to:


Goal: To call a C# library DLL from Access VBA.

I got the below code working using .NET Framework 4.8:

using System.Runtime.InteropServices;

namespace COMLibrary;

public interface IComClass1Interface
{
    string HelloWorld { get; }
    string Delme();
}

[ComVisible(true)]
public class ComClass1 : IComClass1Interface
{
    public string HelloWorld { get { return "From DLL: Hello, World!"; } }

    public string Delme() => "From DLL: Hello, World!";
}

static class UnmanagedExports
{
    [DllExport]
    [return: MarshalAs(UnmanagedType.IDispatch)]
    static object CreateDotNetObject()
    {
        return new ComClass1();
    }
}

And calling from VBA:

Public Declare Function CreateDotNetObject Lib "C:\Users\user\Documents\Visual Studio Projects\COMLibrary\COMLibrary\bin\Debug\COMLibrary.dll" () As Object

Public Sub test()

    Dim o
    Set o = CreateDotNetObject()
    MsgBox o.HelloWorld
    Set o = Nothing

End Sub

However, when I use the same code in a .NET 6 project MS Access crashes when the VBA call is made. I've tried changing several things with no success. Dependencies tool shows the same CreateDotNetObject entry point in the DLL file, so it's not that.

What do I need to do to get this working with .NET 6?

3F commented 2 years ago

@DiskCrasher, Thanks for the report!

Please follow the instructions from https://github.com/3F/DllExport/issues/193#issuecomment-899030110

DiskCrasher commented 2 years ago

@DiskCrasher, Thanks for the report!

Please follow the instructions from #193 (comment)

In other words, .NET 5/6 aren't yet supported.

3F commented 2 years ago

In other words, .NET 5/6 aren't yet supported.

Only through targeting via supported .NET Core TFMs at this day (most recent version is 1.7.4)

CodingMadness commented 2 years ago

Mate, sry if im stupid, but im kinda new to .NET and especially to their massive architectual changes of the entire environment^^

Would you mind, stating in short sentence: How you mean by targeting via .NET core?

3F commented 2 years ago

How you mean by targeting via .NET core?

@Shpendicus,

targeting via supported .NET Core TFMs

For example, .NET Core 2.2, or .NET Standard 2

<TargetFrameworks>netstandard2.0</TargetFrameworks>

Also note, lower version can be evaluated in higher Runtime. This is for the case when the module as an intermediary.

CodingMadness commented 2 years ago

@3F the thing is, i watched your 3 min tutorial, on how to use your DLLExport-attribute, and i did it step by step, it even builds! BUT: the moment you passed, in the video, the newly created .dll into the "CFF" viewer, u could see the address of the static function you defined in your class, but when I did it, the address window in the "Exported Object" folder, in the CFF-viewer, was empty :(

the thing is, I use features of .NET 6 and this wont work then, right?

CodingMadness commented 2 years ago

Btw, I have donated you a monthly-fee for your work as a thanks to such big work, since every time I wanted something like COM or hard low level it's a pain in the ass, if you are not yet that good of a programmer, so yea pls keep up the dev of "DllExport" and also "Corini" to be usable fully with .NET 6 , in that sense, that you can export a .NET object/function even when u make use of all what is in .NET 6 and C# 10 :)

3F commented 2 years ago

the address window in the "Exported Object" folder, in the CFF-viewer, was empty :(

It shouldn't be empty. Make sure you're looking at correct assembly (x64 or x86 folder if you're using both arch)

the thing is, I use features of .NET 6 and this wont work then, right?

Right.

  1. For the case when TFM where exported methods is .NET 6 and CLR inits Runtime assembly v6, See #132
  2. For the case when TFM is lowever, for example .NET Core 2.1
    1. ...and CLR inits Runtime assembly v2, it must be failed because it can't process types from higher v6 at least without loading it in domain, but domain already contain loaded v2 at the moment. So you can't for several reasons.
    2. Only higher Runtime version can process types from lower such v2 but this implies related initialization at a first CLR "activation".

I just answered the same on youtube and if it's not you (not sure because I had a 4 several absolutely the same requests :) to me yesterday from different place),

What about modern unmanaged delegates? In referenced issue above we're talking also something about it and I think this is a main reason to me set automatically much less priority for this project in my timeline today (etc. https://www.reddit.com/r/x3F/comments/ovq368/highspeed_multiplication_of_large_numbers_fast/ )

"Corini"

*Conari

Btw, I have donated you a monthly-fee for your work as a thanks to such big work

Thank you! it is really appreciated!!