DllImport native dependency from NuGet package not working on net472, but works netcoreapp2.2


My project currently works flawlessly on .NET Core (Linux/OSX/Windows/netcoreapp2.2). It should work on net472 as well, but for some reason, DllImport isn't finding the native dependency.

A simple repro can be found here, but I will expand further in this question for posterity.

Here is the project that is packed that contains the native dependency.

<Project Sdk="Microsoft.NET.Sdk">
  <Import Project="../../build/common.props" />
    <ProjectReference Include="..\net\Qml.Net\Qml.Net.csproj" />
    <Content Include="output/**/*">

This will produce a NuGet package with the following output.

├── [Content_Types].xml
├── package
│   └── services
│       └── metadata
│           └── core-properties
│               └── e803485f4a674e8d9d0155224fa9cbc2.psmdcp
├── Qml.Net.WindowsBinaries.nuspec
├── _rels
└── runtimes
    └── win-x64
        └── native
            └── QmlNet.dll

8 directories, 4 files

Here is my consuming project.

<Project Sdk="Microsoft.NET.Sdk">
    <None Remove="**\*.qmlc" />
    <PackageReference Include="Qml.Net" Version="0.9.0-alpha.5" />
    <PackageReference Include="Qml.Net.WindowsBinaries" Version="0.9.0-alpha.5" />
    <Content Include="images\**" CopyToPublishDirectory="Always" />
    <Content Include="pages\**" CopyToPublishDirectory="Always" />
    <Content Include="Main.qml" CopyToPublishDirectory="Always" />

And here is the simple code that is trying to do the PInvoke.

using System;
using System.Runtime.InteropServices;
using Qml.Net.Runtimes;

namespace Features
    class Program
        // NOTE: This works if I provide absolute path to QmlNet.dll
        internal static extern long qml_net_getVersion();

        static int Main(string[] args)
            // This is need to prep Qt runtime that QmlNet.dll depends on.
            // It is required, but it is irrelvant to the issue we are experiencing.

            // The following works on netcoreapp2.2, but not on 4.7.2.

            return 0;

When I run this, I get:

System.DllNotFoundException: 'Unable to load DLL 'QmlNet': The specified module could not be found. (Exception from HRESULT: 0x8007007E)'

I tried adding an explicit target to net472 in my NuGet package, didn't work.

I don't get any errors when my consuming project targets netcoreapp2.2.

How do I get my native dependency to be property discovered with DllImport when targeting net472?

asked on Stack Overflow Apr 14, 2019 by Paul Knopf

0 Answers

Nobody has answered this question yet.

User contributions licensed under CC BY-SA 3.0