aboutsummaryrefslogtreecommitdiffstats
path: root/examples/UserView/UserViewQml/qtdotnet/generated_files/main.cpp
blob: a17163880bfcb0c88acb56947cd22828ce4383df (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/***************************************************************************************************
 Copyright (C) 2024 The Qt Company Ltd.
 SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
***************************************************************************************************/

#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlContext>
#include <QThread>
#include <QFile>
#include <QDotNetStatic>

#define EMBED_HASH_HI_PART_UTF8 "c3ab8ff13720e8ad9047dd39466b3c89" // SHA-256 of "foobar" in UTF-8
#define EMBED_HASH_LO_PART_UTF8 "74e592c2fa383d4a3960714caef0c4f2"
#define EMBED_HASH_FULL_UTF8    (EMBED_HASH_HI_PART_UTF8 EMBED_HASH_LO_PART_UTF8) // NUL terminated

constexpr int EMBED_SZ = sizeof(EMBED_HASH_FULL_UTF8) / sizeof(EMBED_HASH_FULL_UTF8[0]);
constexpr int EMBED_MAX = (EMBED_SZ > 1025 ? EMBED_SZ : 1025); // 1024 DLL name length, 1 NUL

// Contains the EMBED_HASH_FULL_UTF8 value at compile time or the managed DLL name replaced by "dotnet build".
// Must not be 'const' because std::string(&embed[0]) below would bind to a const string ctor plus length
// where length is determined at compile time (=64) instead of the actual length of the string at runtime.
static char appName[EMBED_MAX] = EMBED_HASH_FULL_UTF8;     // series of NULs followed by embed hash string

static const char hi_part[] = EMBED_HASH_HI_PART_UTF8;
static const char lo_part[] = EMBED_HASH_LO_PART_UTF8;

int main(int argc, char *argv[])
{
    qInfo() << "App name" << appName;

    QGuiApplication app(argc, argv);
    auto assemblyPath = QDir(QCoreApplication::applicationDirPath()).filePath(appName);
    if (!QFile::exists(assemblyPath)) {
        qInfo() << "App assembly not found: " << assemblyPath;
        return -1;
    }

    QDotNetHost dotNetHost;
    auto *dotnetThread = QThread::create(
        [argc, argv, &app, &dotNetHost, &assemblyPath]()
        {
            dotNetHost.loadApp(assemblyPath);
            int result = dotNetHost.runApp();
            app.exit(result);
        });
    dotnetThread->start();

    while (!dotNetHost.isReady())
        QThread::sleep(1);
    QDotNetAdapter::instance().init(
        QDir(QCoreApplication::applicationDirPath()).filePath("Qt.DotNet.Adapter.dll"),
        "Qt.DotNet.Adapter", "Qt.DotNet.Adapter", &dotNetHost);

    QQmlApplicationEngine engine;
    engine.loadFromModule("qmlapp", "Main");

    return app.exec();
}