[GH-ISSUE #140] Can't get unmanaged hosting to work #85

Closed
opened 2026-05-05 11:03:57 -06:00 by gitea-mirror · 3 comments
Owner

Originally created by @geigertom on GitHub (Jun 11, 2019).
Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/140

Hi, I'm trying to use Qml.Net in a project. I was able to run the examples, but I cannot get the unmanaged hosting approach to work. Unmanaged, because I need to make use of Qt's QVideoFilter and Camera features in order to do some image processing.

I can make the project build, but when I try to run it from Qt Creator I get the following output:

QML debugging is enabled. Only use this in a safe environment.
looking for libhostfxr.so in root /usr/local/share/dotnet
/usr/local/share/dotnet doesn't exist
looking for libhostfxr.so in root /usr/share/dotnet
found potentional file /usr/share/dotnet/host/fxr/2.2.5/libhostfxr.so with version 2.2.5
returning hostfx lib: /usr/share/dotnet/host/fxr/2.2.5/libhostfxr.so

Unhandled Exception: System.TypeInitializationException: The type initializer for 'Qml.Net.Internal.Interop' threw an exception. ---> System.ArgumentNullException: Value cannot be null.Parameter name: ptr
   at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPtr ptr, Type t)
   at Qml.Net.Internal.Interop.LoadDelegates(Object o, IntPtr library, IPlatformLoader loader)
   at Qml.Net.Internal.Interop.LoadInteropType[T](IntPtr library, IPlatformLoader loader)
   at Qml.Net.Internal.Interop..cctor()
   --- End of inner exception stack trace ---
   at Qml.Net.Host.Run(String[] args, Func`5 action)
   at qmlnetmanaged.Program.Main(String[] _) in /home/tom/workspace/qmlnet/QmlNet/Program.cs:line 25

From what I understand, this is not a Qt/Qml.Net-specific error, but rather having to do with .net Interoperability, which I do not know much about at this point.

This is my main function from main.cpp:

int main(int argc, char *argv[])
{
    // Phase 1
    // Initialize Qt/QML like you would normally.
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QGuiApplication app(argc, argv);
    QQmlApplicationEngine engine;

    // Phase 2
    // Get the location to the managed exec
    QString netDll = "/home/tom/workspace/qmlnet/QmlNet/bin/Debug/netcoreapp2.2/QmlNet.dll";
    // Phase 3
    // Find .NET Core and it's libs/paths.
    CoreHost::RunContext runContext;
    runContext.hostFxrContext = CoreHost::findHostFxr();
    runContext.managedExe = netDll;
    // NOTE: You may set entry point to the current executable if
    // the .NET runtime is deployed side-by-side.
    runContext.entryPoint = runContext.hostFxrContext.dotnetRoot;
    runContext.entryPoint.append(CORECLR_DOTNET_EXE_NAME);

    // Phase 4
    // Run the .NET applciation.
    return CoreHost::run(app,
        engine,
        runCallback,
        runContext);
}

And here's my Program.cs:

using Qml.Net;
using Qml.Net.Runtimes;
using System;

namespace qmlnetmanaged
{
    class Program
    {
        public class TestObject
        {
            public void TestMethod()
            {
                Console.WriteLine("test method");
            }
        }

        static int Main(string[] _)
        {
            // The "_" contains some private arguements to help
            // bootstrap things. It is intended to be passed
            // immediately into Host.Run(...).

            // Phase 5
            // Unwrap the magic.
            return Host.Run(_, (args, app, engine, runCallback) =>
            {
                Console.WriteLine("DOT NET STARTING UP!");
                // "args" contains the any user defined arguements passed from
                // CoreHost::run(..) in C++.

                // Phase 6
                // Register any .NET types that will be used.
                // QQmlApplicationEngine.RegisterType<TestObject>("test");

                // Phase 7
                // This callback passes control back to C++ to perform
                // any file registrations and run the event loop.
                return runCallback();
            });
        }
    }
}

I was following the setup guide at https://qmlnet.github.io/setup/unmanaged-hosting/, only adjusting the git repo url to the current location. I tried in Ubuntu 18.04. and Windows7, both 64bit, dotnetcore2.2, same result. Any ideas?

Cheers,
Tom

Originally created by @geigertom on GitHub (Jun 11, 2019). Original GitHub issue: https://github.com/qmlnet/qmlnet/issues/140 Hi, I'm trying to use Qml.Net in a project. I was able to run the examples, but I cannot get the unmanaged hosting approach to work. Unmanaged, because I need to make use of Qt's QVideoFilter and Camera features in order to do some image processing. I can make the project build, but when I try to run it from Qt Creator I get the following output: ``` QML debugging is enabled. Only use this in a safe environment. looking for libhostfxr.so in root /usr/local/share/dotnet /usr/local/share/dotnet doesn't exist looking for libhostfxr.so in root /usr/share/dotnet found potentional file /usr/share/dotnet/host/fxr/2.2.5/libhostfxr.so with version 2.2.5 returning hostfx lib: /usr/share/dotnet/host/fxr/2.2.5/libhostfxr.so Unhandled Exception: System.TypeInitializationException: The type initializer for 'Qml.Net.Internal.Interop' threw an exception. ---> System.ArgumentNullException: Value cannot be null.Parameter name: ptr at System.Runtime.InteropServices.Marshal.GetDelegateForFunctionPointer(IntPtr ptr, Type t) at Qml.Net.Internal.Interop.LoadDelegates(Object o, IntPtr library, IPlatformLoader loader) at Qml.Net.Internal.Interop.LoadInteropType[T](IntPtr library, IPlatformLoader loader) at Qml.Net.Internal.Interop..cctor() --- End of inner exception stack trace --- at Qml.Net.Host.Run(String[] args, Func`5 action) at qmlnetmanaged.Program.Main(String[] _) in /home/tom/workspace/qmlnet/QmlNet/Program.cs:line 25 ``` From what I understand, this is not a Qt/Qml.Net-specific error, but rather having to do with .net Interoperability, which I do not know much about at this point. This is my main function from main.cpp: ``` int main(int argc, char *argv[]) { // Phase 1 // Initialize Qt/QML like you would normally. QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QGuiApplication app(argc, argv); QQmlApplicationEngine engine; // Phase 2 // Get the location to the managed exec QString netDll = "/home/tom/workspace/qmlnet/QmlNet/bin/Debug/netcoreapp2.2/QmlNet.dll"; // Phase 3 // Find .NET Core and it's libs/paths. CoreHost::RunContext runContext; runContext.hostFxrContext = CoreHost::findHostFxr(); runContext.managedExe = netDll; // NOTE: You may set entry point to the current executable if // the .NET runtime is deployed side-by-side. runContext.entryPoint = runContext.hostFxrContext.dotnetRoot; runContext.entryPoint.append(CORECLR_DOTNET_EXE_NAME); // Phase 4 // Run the .NET applciation. return CoreHost::run(app, engine, runCallback, runContext); } ``` And here's my Program.cs: ``` using Qml.Net; using Qml.Net.Runtimes; using System; namespace qmlnetmanaged { class Program { public class TestObject { public void TestMethod() { Console.WriteLine("test method"); } } static int Main(string[] _) { // The "_" contains some private arguements to help // bootstrap things. It is intended to be passed // immediately into Host.Run(...). // Phase 5 // Unwrap the magic. return Host.Run(_, (args, app, engine, runCallback) => { Console.WriteLine("DOT NET STARTING UP!"); // "args" contains the any user defined arguements passed from // CoreHost::run(..) in C++. // Phase 6 // Register any .NET types that will be used. // QQmlApplicationEngine.RegisterType<TestObject>("test"); // Phase 7 // This callback passes control back to C++ to perform // any file registrations and run the event loop. return runCallback(); }); } } } ``` I was following the setup guide at https://qmlnet.github.io/setup/unmanaged-hosting/, only adjusting the git repo url to the current location. I tried in Ubuntu 18.04. and Windows7, both 64bit, dotnetcore2.2, same result. Any ideas? Cheers, Tom
Author
Owner

@geigertom commented on GitHub (Jun 19, 2019):

I eventually found the sample projects (https://github.com/qmlnet/qmlnet/tree/develop/samples/hosting) and could finally make it work. Had to change 'QQmlApplicationEngine.RegisterType("test");' to 'Qml.Net.Qml.RegisterType("test", 1);' to make it run though.

I find it a little hard to get started with qml.net, as there is not a definite entry point for people who are new to the project. The instructions on https://qmlnet.github.io/setup/ are outdated and do not work (see original posting) and the samples still need tweaking to run without errors.

Anyway, I'm glad its working now, qml.net looks really promising, thank you for your time and effort!

Tom

<!-- gh-comment-id:503580902 --> @geigertom commented on GitHub (Jun 19, 2019): I eventually found the sample projects (https://github.com/qmlnet/qmlnet/tree/develop/samples/hosting) and could finally make it work. Had to change 'QQmlApplicationEngine.RegisterType<TestObject>("test");' to 'Qml.Net.Qml.RegisterType<TestObject>("test", 1);' to make it run though. I find it a little hard to get started with qml.net, as there is not a definite entry point for people who are new to the project. The instructions on https://qmlnet.github.io/setup/ are outdated and do not work (see original posting) and the samples still need tweaking to run without errors. Anyway, I'm glad its working now, qml.net looks really promising, thank you for your time and effort! Tom
Author
Owner

@vadi2 commented on GitHub (Jun 19, 2019):

Yeah - the documentation is a bit out of date so it is indeed tough to get started. Would you mind submitting a PR for that particular example? Together we can make it easier :)

(I'm not the dev, just another community member)

<!-- gh-comment-id:503606496 --> @vadi2 commented on GitHub (Jun 19, 2019): Yeah - the documentation is a bit out of date so it is indeed tough to get started. Would you mind submitting a PR for that particular example? Together we can make it easier :) (I'm not the dev, just another community member)
Author
Owner

@geigertom commented on GitHub (Jun 19, 2019):

I created a pull request containing the necessary adjustments.

<!-- gh-comment-id:503653883 --> @geigertom commented on GitHub (Jun 19, 2019): I created a pull request containing the necessary adjustments.
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#85
No description provided.