83 lines
4.5 KiB
C++
83 lines
4.5 KiB
C++
#pragma once
|
|||
|
|
#ifndef NOMINMAX
|
||
|
|
#define NOMINMAX
|
||
|
|
#endif
|
||
|
|
#include <Windows.h>
|
||
|
|
#include <QCoreApplication>
|
||
|
|
#include <QDir>
|
||
|
|
#include <QFileInfo>
|
||
|
|
#include <QJsonObject>
|
||
|
|
#include <QSet>
|
||
|
|
#include <QStandardPaths>
|
||
|
|
#include <mutex>
|
||
|
|
#include <stdexcept>
|
||
|
|
|
||
|
|
// Configure this process from the bundled SDK on every launch. No machine/user
|
||
|
|
// environment settings or previous PowerShell session are required.
|
||
|
|
inline QJsonObject configureGalaxyRuntime(const QString& directory) {
|
||
|
|
static std::mutex mutex;
|
||
|
|
static QSet<QString> searchDirectories;
|
||
|
|
std::lock_guard<std::mutex> lock(mutex);
|
||
|
|
const auto sdk=QDir(directory).canonicalPath();
|
||
|
|
if(sdk.isEmpty())throw std::runtime_error("Bundled camera SDK directory is missing");
|
||
|
|
const QStringList folders{sdk+"/APIDll/Win64",sdk+"/GenICam/bin/Win64_x64",sdk+"/GenTL/Win64"};
|
||
|
|
const auto root=sdk+"/GenICam";
|
||
|
|
const auto log=root+"/log/config/DefaultLogging.properties";
|
||
|
|
const auto cache=QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation)+"/VisionGuide/Galaxy/xml/cache";
|
||
|
|
for(const auto& folder:folders)if(!QDir(folder).exists())throw std::runtime_error(("Camera runtime directory missing: "+folder).toUtf8().constData());
|
||
|
|
if(!QFileInfo::exists(log))throw std::runtime_error("Camera logging configuration is missing");
|
||
|
|
if(!QDir().mkpath(cache))throw std::runtime_error("Cannot create writable camera XML cache");
|
||
|
|
if(!SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_APPLICATION_DIR|LOAD_LIBRARY_SEARCH_SYSTEM32|LOAD_LIBRARY_SEARCH_USER_DIRS))throw std::runtime_error("Cannot configure camera DLL search paths");
|
||
|
|
for(const auto& folder:folders){
|
||
|
|
const auto key=QDir::toNativeSeparators(folder).toLower();
|
||
|
|
if(!searchDirectories.contains(key)){
|
||
|
|
if(!AddDllDirectory(reinterpret_cast<LPCWSTR>(QDir::toNativeSeparators(folder).utf16())))throw std::runtime_error("Cannot add camera DLL directory");
|
||
|
|
searchDirectories.insert(key);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
auto set=[](const char* key,const QString& value){
|
||
|
|
const auto native=QDir::toNativeSeparators(value);
|
||
|
|
if(!qputenv(key,native.toLocal8Bit()))throw std::runtime_error("Cannot set camera process environment");
|
||
|
|
// Keep Win32 readers in SDKs built with another CRT in agreement.
|
||
|
|
const auto wideKey=QString::fromLatin1(key);
|
||
|
|
if(!SetEnvironmentVariableW(reinterpret_cast<LPCWSTR>(wideKey.utf16()),reinterpret_cast<LPCWSTR>(native.utf16())))throw std::runtime_error("Cannot set camera Win32 environment");
|
||
|
|
// GxIAPI uses VC100 and GenICam uses VC120. Their getenv() caches
|
||
|
|
// were created before main() and are independent of Qt's UCRT.
|
||
|
|
using LegacyPutenv = int (__cdecl*)(const char*,const char*);
|
||
|
|
for(const auto* crtName : {L"msvcr100.dll",L"msvcr120.dll"}){
|
||
|
|
const auto module=GetModuleHandleW(crtName);
|
||
|
|
if(!module)continue;
|
||
|
|
const auto putenv=reinterpret_cast<LegacyPutenv>(GetProcAddress(module,"_putenv_s"));
|
||
|
|
if(!putenv||putenv(key,native.toLocal8Bit().constData())!=0)throw std::runtime_error("Cannot update camera SDK runtime environment");
|
||
|
|
}
|
||
|
|
};
|
||
|
|
set("GENICAM_ROOT_V3_0",root);
|
||
|
|
set("GENICAM_CACHE_V3_0",cache);
|
||
|
|
set("GENICAM_LOG_CONFIG_V3_0",log);
|
||
|
|
set("GENICAM_GENTL64_PATH",folders[2]);
|
||
|
|
// Preserve compatibility with components that use the unversioned aliases.
|
||
|
|
set("GALAXY_GENICAM_ROOT",root);
|
||
|
|
set("GALAXY_GENICAM_CACHE",cache);
|
||
|
|
set("GALAXY_GENICAM_LOG_CONFIG",log);
|
||
|
|
QStringList path;
|
||
|
|
for(const auto& folder:folders)path.append(QDir::toNativeSeparators(folder));
|
||
|
|
for(const auto& item:qEnvironmentVariable("PATH").split(';',QString::SkipEmptyParts)){
|
||
|
|
bool duplicate=false;
|
||
|
|
for(const auto& known:path)if(QDir::cleanPath(QDir::fromNativeSeparators(item)).compare(QDir::cleanPath(QDir::fromNativeSeparators(known)),Qt::CaseInsensitive)==0){duplicate=true;break;}
|
||
|
|
if(!duplicate)path.append(item);
|
||
|
|
}
|
||
|
|
set("PATH",path.join(';'));
|
||
|
|
return {{"sdk_root",sdk},{"GENICAM_ROOT_V3_0",root},{"GENICAM_CACHE_V3_0",cache},{"GENICAM_LOG_CONFIG_V3_0",log},{"GENICAM_GENTL64_PATH",folders[2]}};
|
||
|
|
}
|
||
|
|
|
||
|
|
inline QString bundledGalaxySdk() {
|
||
|
|
QDir app(QCoreApplication::applicationDirPath());
|
||
|
|
for(int i=0;i<10;++i){
|
||
|
|
const auto candidate=app.filePath("runtime/GalaxySDK");
|
||
|
|
if(QFileInfo::exists(candidate+"/APIDll/Win64/GxIAPI.dll"))return candidate;
|
||
|
|
if(!app.cdUp())break;
|
||
|
|
}
|
||
|
|
const auto candidate=QDir::current().filePath("runtime/GalaxySDK");
|
||
|
|
return QFileInfo::exists(candidate+"/APIDll/Win64/GxIAPI.dll")?candidate:QString();
|
||
|
|
}
|