[GH-ISSUE #76] Can't get it to work with .NET Framework/Mono #49

Open
opened 2026-05-05 11:01:29 -06:00 by gitea-mirror · 7 comments
Owner

Originally created by @akbcode on GitHub (Oct 10, 2018).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/76

I created a small program to test

static int Main(string[] args)
{
    using (var app = new QGuiApplication(args))
    {
        using (var engine = new QQmlApplicationEngine())
        {
            engine.Load("main.qml");
            return app.Exec();
        }
    }
}

But it throws an exception on the line var app = new QGuiApplication(args)

System.TypeInitializationException: 'The type initializer for 'Qml.Net.Internal.Interop' threw an exception.'
FileNotFoundException: The specified library ("QmlNet") was not found in any of the loader search paths.

I'm using version 0.6.2 targeting .NET 4.6.1. Tested on both Windows and Mac.

Originally created by @akbcode on GitHub (Oct 10, 2018). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/76 I created a small program to test static int Main(string[] args) { using (var app = new QGuiApplication(args)) { using (var engine = new QQmlApplicationEngine()) { engine.Load("main.qml"); return app.Exec(); } } } But it throws an exception on the line `var app = new QGuiApplication(args)` System.TypeInitializationException: 'The type initializer for 'Qml.Net.Internal.Interop' threw an exception.' FileNotFoundException: The specified library ("QmlNet") was not found in any of the loader search paths. I'm using version 0.6.2 targeting .NET 4.6.1. Tested on both Windows and Mac.
Author
Owner

@pauldotknopf commented on GitHub (Oct 10, 2018):

Can you try this project?

https://github.com/qmlnet/qmlnet-examples

Also, did you add a reference to the native packages?


<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.1</TargetFramework>
    <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory>
  </PropertyGroup>
  <ItemGroup>
    <None Remove="**\*.qmlc" />
  </ItemGroup>
  <ItemGroup>
    <PackageReference Include="Qml.Net" Version="0.6.2" />
    <PackageReference Include="Qml.Net.LinuxBinaries" Version="0.6.2" />
    <PackageReference Include="Qml.Net.OSXBinaries" Version="0.6.2" />
    <PackageReference Include="Qml.Net.WindowsBinaries" Version="0.6.2" />
  </ItemGroup>
</Project>
<!-- gh-comment-id:428553615 --> @pauldotknopf commented on GitHub (Oct 10, 2018): Can you try this project? https://github.com/qmlnet/qmlnet-examples Also, did you add a reference to the native packages? ```xml <Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp2.1</TargetFramework> <RunWorkingDirectory>$(MSBuildProjectDirectory)</RunWorkingDirectory> </PropertyGroup> <ItemGroup> <None Remove="**\*.qmlc" /> </ItemGroup> <ItemGroup> <PackageReference Include="Qml.Net" Version="0.6.2" /> <PackageReference Include="Qml.Net.LinuxBinaries" Version="0.6.2" /> <PackageReference Include="Qml.Net.OSXBinaries" Version="0.6.2" /> <PackageReference Include="Qml.Net.WindowsBinaries" Version="0.6.2" /> </ItemGroup> </Project> ```
Author
Owner

@akbcode commented on GitHub (Oct 10, 2018):

Yes, I did add the native packages.
The qmlnet-examples project works fine. But only when targeting .NET Core.

<!-- gh-comment-id:428566136 --> @akbcode commented on GitHub (Oct 10, 2018): Yes, I did add the native packages. The qmlnet-examples project works fine. But only when targeting .NET Core.
Author
Owner

@pauldotknopf commented on GitHub (Oct 10, 2018):

I see, I will check it this evening.

<!-- gh-comment-id:428582195 --> @pauldotknopf commented on GitHub (Oct 10, 2018): I see, I will check it this evening.
Author
Owner

@TripleWhy commented on GitHub (Apr 7, 2019):

@pauldotknopf I would like to see support for .NET Framework as well. Any hints on where to start if I were to add this myself?

<!-- gh-comment-id:480595693 --> @TripleWhy commented on GitHub (Apr 7, 2019): @pauldotknopf I would like to see support for .NET Framework as well. Any hints on where to start if I were to add this myself?
Author
Owner

@pauldotknopf commented on GitHub (Apr 14, 2019):

So, it seems that DllImport isn't finding the native binaries on .NET Fullframework.

Add a reference to Qml.Net.WindowsBinaries.

[DllImport("QmlNet")]
internal static extern long qml_net_getVersion();

static int Main(string[] args)
{
    RuntimeManager.DiscoverOrDownloadSuitableQtRuntime();
    Console.WriteLine(qml_net_getVersion());
}

This will throw a "dll not found error". It works on Core.

If anybody has any ideas, I'd love to hear them.

<!-- gh-comment-id:482905974 --> @pauldotknopf commented on GitHub (Apr 14, 2019): So, it seems that ```DllImport``` isn't finding the native binaries on .NET Fullframework. Add a reference to ```Qml.Net.WindowsBinaries```. ```c# [DllImport("QmlNet")] internal static extern long qml_net_getVersion(); static int Main(string[] args) { RuntimeManager.DiscoverOrDownloadSuitableQtRuntime(); Console.WriteLine(qml_net_getVersion()); } ``` This will throw a "dll not found error". It works on Core. If anybody has any ideas, I'd love to hear them.
Author
Owner

@pauldotknopf commented on GitHub (Apr 14, 2019):

I've posed the question on SO here. I will let you guys know if I hear anything back.

<!-- gh-comment-id:483043664 --> @pauldotknopf commented on GitHub (Apr 14, 2019): I've posed the question on SO [here](https://stackoverflow.com/questions/55678984/dllimport-native-dependency-from-nuget-package-not-working-on-net472-but-works). I will let you guys know if I hear anything back.
Author
Owner

@kshanafelt commented on GitHub (Dec 9, 2019):

I found a bit of a workaround for this issue.

It seems to have some to do with how .net framework pulls in native dlls, I noticed that when publishing a publish profile for net472 set to win-x64 (see below), that the native QmlNet.dll gets pulled in.

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <PublishProtocol>FileSystem</PublishProtocol>
    <Configuration>Release</Configuration>
    <Platform>Any CPU</Platform>
    <TargetFramework>net472</TargetFramework>
    <PublishDir>bin\Release\publish\net472-w64</PublishDir>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
    <SelfContained>true</SelfContained>
  </PropertyGroup>
</Project>

So I tried creating a x64 platform configuration (since only native x64 is supported anyway), and the build for that pulled the native QmlNet.dll as well. I guess in .net framework AnyCpu it can't figure out what bitness of native dlls to pull in, until you tell it with a publish.

While this got me past the error noted on this issue, it brought a new one:

System.TypeAccessException: Attempt by method 'DynamicClass.method(Qml.Net.Internal.Types.NetReference, Qml.Net.Internal.Qml.NetVariantList, Qml.Net.Internal.Qml.NetVariant, System.Threading.Tasks.Task ByRef)' to access type 'Qml.Net.Internal.Types.NetReference' failed.
   at method(NetReference , NetVariantList , NetVariant , Task& )
   at Qml.Net.Internal.DefaultCallbacks.ReadProperty(IntPtr p, IntPtr t, IntPtr ip, IntPtr r)
   at MetaGalactic.QML.Program.Main(String[] args) in C:\src\MetaGalactic\MetaGalactic.QML\Program.cs:line 28

Though I cannot articulate how, I had a eureka while reading this issue: Native assets are not copied to bin for net472 app if any referenced package uses netstandard2.0, but since their workaround didn't work in my case, you should probably regard as a red herring.

<!-- gh-comment-id:563379317 --> @kshanafelt commented on GitHub (Dec 9, 2019): I found a bit of a workaround for this issue. It seems to have some to do with how .net framework pulls in native dlls, I noticed that when publishing a publish profile for net472 set to win-x64 (see below), that the native `QmlNet.dll` gets pulled in. ```xml <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup> <PublishProtocol>FileSystem</PublishProtocol> <Configuration>Release</Configuration> <Platform>Any CPU</Platform> <TargetFramework>net472</TargetFramework> <PublishDir>bin\Release\publish\net472-w64</PublishDir> <RuntimeIdentifier>win-x64</RuntimeIdentifier> <SelfContained>true</SelfContained> </PropertyGroup> </Project> ``` So I tried creating a x64 platform configuration (since only native x64 is supported anyway), and the **build** for that pulled the native `QmlNet.dll` as well. I guess in .net framework `AnyCpu` it can't figure out what bitness of native dlls to pull in, until you tell it with a publish. While this got me past the error noted on this issue, it brought a new one: ``` System.TypeAccessException: Attempt by method 'DynamicClass.method(Qml.Net.Internal.Types.NetReference, Qml.Net.Internal.Qml.NetVariantList, Qml.Net.Internal.Qml.NetVariant, System.Threading.Tasks.Task ByRef)' to access type 'Qml.Net.Internal.Types.NetReference' failed. at method(NetReference , NetVariantList , NetVariant , Task& ) at Qml.Net.Internal.DefaultCallbacks.ReadProperty(IntPtr p, IntPtr t, IntPtr ip, IntPtr r) at MetaGalactic.QML.Program.Main(String[] args) in C:\src\MetaGalactic\MetaGalactic.QML\Program.cs:line 28 ``` Though I cannot articulate how, I had a eureka while reading this issue: [Native assets are not copied to bin for net472 app if any referenced package uses netstandard2.0](https://github.com/dotnet/sdk/issues/3031), but since their workaround didn't work in my case, you should probably regard as a red herring.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference: github-starred/qmlnet#49
No description provided.