67 lines
2.8 KiB
C++
67 lines
2.8 KiB
C++
//main.cpp
|
||
#include "UserVisibleLog.h"
|
||
#include <Windows.h>
|
||
#include <QApplication>
|
||
#include <QCoreApplication>
|
||
#include <QTextCodec>
|
||
#include "MainWindow.h"
|
||
#ifdef VISIONGUIDE_QMAKE
|
||
#include "PortableRuntime.h"
|
||
#include "CameraManager.h"
|
||
#include <QJsonArray>
|
||
#include <QJsonDocument>
|
||
#include <QMessageBox>
|
||
#include <cstdio>
|
||
#include <stdexcept>
|
||
#endif
|
||
|
||
int main(int argc, char *argv[])
|
||
{
|
||
installVisionGuideMessageFilter();
|
||
// ── 必须在任何 ORT 相关代码加载前执行 ──────────────────────────
|
||
// 只允许从"应用程序目录"和"System32"加载 DLL,
|
||
// 彻底屏蔽掉 C:\Windows\System32\onnxruntime.dll 的误命中。
|
||
SetDefaultDllDirectories(
|
||
LOAD_LIBRARY_SEARCH_APPLICATION_DIR | // exe 同目录
|
||
LOAD_LIBRARY_SEARCH_SYSTEM32 // 真正的系统 DLL
|
||
);
|
||
|
||
QApplication a(argc, argv);
|
||
QCoreApplication::setApplicationName("VisionGuide");
|
||
|
||
// ========== Qt5 专用 UTF-8 编码设置(100%兼容,无报错) ==========
|
||
QTextCodec* codec = QTextCodec::codecForName("UTF-8");
|
||
QTextCodec::setCodecForLocale(codec);
|
||
|
||
#ifdef VISIONGUIDE_QMAKE
|
||
try {
|
||
const bool checkOnly = a.arguments().contains("--portable-check");
|
||
const bool cameraCheck = a.arguments().contains("--camera-check");
|
||
auto state = preparePortableRuntime(checkOnly || cameraCheck);
|
||
if (cameraCheck) {
|
||
const bool initialized = CameraManager::initSDK();
|
||
state["camera_api_initialized"] = initialized;
|
||
QJsonArray devices;
|
||
if (initialized) { for (const auto& device : CameraManager::enumerateDevices()) devices.append(device); CameraManager::uninitSDK(); }
|
||
state["camera_devices"] = devices;
|
||
const auto json = QJsonDocument(state).toJson(); std::fwrite(json.constData(), 1, json.size(), stdout);
|
||
return initialized ? 0 : 3;
|
||
}
|
||
if (checkOnly) { const auto json = QJsonDocument(state).toJson(); std::fwrite(json.constData(), 1, json.size(), stdout); return 0; }
|
||
} catch (const std::exception& error) {
|
||
if (a.arguments().contains("--portable-check") || a.arguments().contains("--camera-check") || a.arguments().contains("--background")) { std::fprintf(stderr, "%s\n", error.what()); return 2; }
|
||
QMessageBox::critical(nullptr, QString::fromUtf8("启动失败"), sanitizeUserVisibleLog(QString::fromUtf8(error.what()))); return 2;
|
||
}
|
||
#endif
|
||
|
||
MainWindow w;
|
||
const auto arguments=a.arguments();const int ipcIndex=arguments.indexOf("--ipc-name");
|
||
if(ipcIndex>=0){
|
||
if(ipcIndex+1>=arguments.size()||arguments[ipcIndex+1].isEmpty()){std::fprintf(stderr,"--ipc-name requires a name\n");return 2;}
|
||
w.startIpc(arguments[ipcIndex+1]);
|
||
}
|
||
if(!arguments.contains("--background"))w.show();
|
||
else a.setQuitOnLastWindowClosed(false);
|
||
return a.exec();
|
||
}
|