#include "mainwindow.h" #include <QApplication> #include <windows.h> #include <wtsapi32.h> #pragma comment(lib, "Wtsapi32.lib") int main(int argc, char *argv[]) { QApplication a(argc, argv); HANDLE hToken = NULL; DWORD dwSessionId = WTSGetActiveConsoleSessionId(); if (!WTSQueryUserToken(dwSessionId, &hToken)) { QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("获取用户令牌失败!")); return -1; } HANDLE mutex = ::CreateMutex(Q_NULLPTR, true, (LPCWSTR)qApp->applicationName().toStdWString().c_str()); if (GetLastError() == ERROR_ALREADY_EXISTS) { QMessageBox warningBox(QMessageBox::Warning, QStringLiteral("警告"), QStringLiteral("程序[文本数据同步服务端]只能运行一个!")); warningBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定")); warningBox.setStandardButtons(QMessageBox::Ok); warningBox.exec(); ::CloseHandle(mutex); return 0; } else { ::ReleaseMutex(mutex); } STARTUPINFO si; ZeroMemory(&si, sizeof(si)); si.cb = sizeof(si); si.lpDesktop = (LPWSTR)L"winsta0\\default"; PROCESS_INFORMATION pi; ZeroMemory(&pi, sizeof(pi)); if (!CreateProcessAsUser(hToken, NULL, (LPWSTR)qApp->applicationFilePath().toStdWString().data(), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) { QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("创建进程失败!")); CloseHandle(hToken); return -1; } CloseHandle(hToken); MainWindow w; w.show(); return a.exec(); } 以上QT C++代码会报出以下错误: D:\QtProject\DataServer\main.cpp:-1: error: undefined reference to `WTSQueryUserToken@8' collect2.exe:-1: error: error: ld returned 1 exit status 请修复后给我完整代码
时间: 2024-01-18 21:02:35 浏览: 147
moc.exe 解决moc过程中不支持处理enum包含#include头文件的语法。
5星 · 资源好评率100%
#include "mainwindow.h"
#include <QApplication>
#include <windows.h>
#include <wtsapi32.h>
#pragma comment(lib, "Wtsapi32.lib")
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
HANDLE hToken = NULL;
DWORD dwSessionId = WTSGetActiveConsoleSessionId();
if (!WTSQueryUserToken(dwSessionId, &hToken)) {
QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("获取用户令牌失败!"));
return -1;
}
HANDLE mutex = ::CreateMutex(Q_NULLPTR, true, (LPCWSTR)qApp->applicationName().toStdWString().c_str());
if (GetLastError() == ERROR_ALREADY_EXISTS) {
QMessageBox warningBox(QMessageBox::Warning, QStringLiteral("警告"), QStringLiteral("程序[文本数据同步服务端]只能运行一个!"));
warningBox.setButtonText(QMessageBox::Ok, QStringLiteral("确定"));
warningBox.setStandardButtons(QMessageBox::Ok);
warningBox.exec();
::CloseHandle(mutex);
return 0;
}
else {
::ReleaseMutex(mutex);
}
STARTUPINFO si;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
si.lpDesktop = (LPWSTR)L"winsta0\\default";
PROCESS_INFORMATION pi;
ZeroMemory(&pi, sizeof(pi));
if (!CreateProcessAsUser(hToken, NULL, (LPWSTR)qApp->applicationFilePath().toStdWString().data(), NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi)) {
QMessageBox::warning(nullptr, QStringLiteral("错误"), QStringLiteral("创建进程失败!"));
CloseHandle(hToken);
return -1;
}
CloseHandle(hToken);
MainWindow w;
w.show();
return a.exec();
}
阅读全文