Files
eUi/include/simpleTools/windowsCtrl.hpp
T

184 lines
6.1 KiB
C++
Raw Normal View History

#include <windows.h>
#include <setupapi.h>
#include <cfgmgr32.h>
#include <tchar.h>
#include <stdio.h>
#include <string.h> // for strstr
#include <devguid.h>
2025-09-18 15:26:38 +08:00
#include <shellapi.h>
2025-10-15 11:02:29 +08:00
#include <ShlObj.h>
#include <QPixmap>
#include <QFileIconProvider>
#include <QFileInfo>
#pragma comment(lib, "setupapi.lib")
static void DisableComPort(const char* portName) {
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Get the device information set for all present devices.
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, NULL, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
printf("SetupDiGetClassDevs 失败: %ld\n", GetLastError());
return;
}
// Enumerate through all devices in the set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
DWORD DataT;
char buffer[4096];
DWORD buffersize = sizeof(buffer);
// Get the friendly name of the device.
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buffer, buffersize, &buffersize)) {
if (strstr(buffer, portName) != NULL) {
printf("Found COM port: %s\n", buffer);
// Disable the device.
DEVINST devInst = DeviceInfoData.DevInst;
CONFIGRET status = CM_Disable_DevNode(devInst, 0);
if (status != CR_SUCCESS) {
printf("CM_Disable_DevNode failed: %d\n", status);
}
else {
printf("Device disabled successfully.\n");
}
break;
}
}
} if (GetLastError() != NO_ERROR && GetLastError() != ERROR_NO_MORE_ITEMS) {
printf("SetupDiEnumDeviceInfo failed: %ld\n", GetLastError());
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
}
static void EnableComPort(const char* portName) {
HDEVINFO hDevInfo;
SP_DEVINFO_DATA DeviceInfoData;
DWORD i;
// Get the device information set for all present devices.
hDevInfo = SetupDiGetClassDevs(&GUID_DEVCLASS_PORTS, NULL, NULL, DIGCF_PRESENT);
if (hDevInfo == INVALID_HANDLE_VALUE) {
printf("SetupDiGetClassDevs 失败: %ld\n", GetLastError());
return;
}
// Enumerate through all devices in the set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
DWORD DataT;
char buffer[4096];
DWORD buffersize = sizeof(buffer);
// Get the friendly name of the device.
if (SetupDiGetDeviceRegistryPropertyA(hDevInfo, &DeviceInfoData, SPDRP_FRIENDLYNAME, &DataT, (PBYTE)buffer, buffersize, &buffersize)) {
if (strstr(buffer, portName) != NULL) {
printf("Found COM port: %s\n", buffer);
// Disable the device.
DEVINST devInst = DeviceInfoData.DevInst;
CONFIGRET status = CM_Enable_DevNode(devInst, 0);
if (status != CR_SUCCESS) {
printf("CM_Enable_DevNode failed: %d\n", status);
}
else {
printf("Device Enable successfully.\n");
}
break;
}
}
}
if (GetLastError() != NO_ERROR && GetLastError() != ERROR_NO_MORE_ITEMS) {
printf("SetupDiEnumDeviceInfo failed: %ld\n", GetLastError());
}
// Cleanup
SetupDiDestroyDeviceInfoList(hDevInfo);
}
2025-09-16 15:22:54 +08:00
static bool isRunAsAdmin() {
BOOL fIsRunAsAdmin = FALSE;
PSID pAdministratorsGroup = NULL;
// Allocate and initialize a SID of the administrators group.
SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
if (!AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, DOMAIN_ALIAS_RID_ADMINS,
0, 0, 0, 0, 0, 0, &pAdministratorsGroup))
{
return false;
}
// Determine whether the SID of administrators group is enabled in the primary access token of the process.
if (!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin))
{
fIsRunAsAdmin = FALSE;
}
// Free the allocated SID.
if (pAdministratorsGroup)
{
FreeSid(pAdministratorsGroup);
}
return fIsRunAsAdmin;
}
static void restartAsAdmin() {
TCHAR szPath[MAX_PATH];
if (GetModuleFileName(NULL, szPath, ARRAYSIZE(szPath))) {
SHELLEXECUTEINFO sei = { sizeof(sei) };
sei.lpVerb = TEXT("runas");
sei.lpFile = szPath;
sei.hwnd = NULL;
sei.nShow = SW_NORMAL;
if (!ShellExecuteEx(&sei)) {
DWORD dwError = GetLastError();
if (dwError == ERROR_CANCELLED) {
//std::cout << "The user refused to allow privileges elevation.\n";
}
}
}
}
2025-10-15 11:02:29 +08:00
// 解析 .lnk 文件,获取目标路径
static QString getShortcutTarget(const QString& shortcutPath) {
HRESULT hres;
CoInitialize(NULL);
IShellLink* psl;
QString targetPath;
hres = CoCreateInstance(CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, IID_IShellLink, (LPVOID*)&psl);
if (SUCCEEDED(hres)) {
IPersistFile* ppf;
hres = psl->QueryInterface(IID_IPersistFile, (LPVOID*)&ppf);
if (SUCCEEDED(hres)) {
WCHAR wsz[MAX_PATH];
shortcutPath.toWCharArray(wsz);
wsz[shortcutPath.length()] = 0;
hres = ppf->Load(wsz, STGM_READ);
if (SUCCEEDED(hres)) {
char szTarget[MAX_PATH];
WIN32_FIND_DATA wfd;
if (SUCCEEDED(psl->GetPath(szTarget, MAX_PATH, &wfd, SLGP_UNCPRIORITY))) {
targetPath = QString::fromLocal8Bit(szTarget);
}
}
ppf->Release();
}
psl->Release();
}
CoUninitialize();
return targetPath;
}
// 获取目标文件图标
static QIcon getFileIcon(const QString& filePath, const QSize& size) {
QFileIconProvider iconProvider;
QFileInfo fileInfo(filePath);
QIcon icon = iconProvider.icon(fileInfo);
QPixmap pixmap = icon.pixmap(size);
return QIcon(pixmap);
}
#pragma once