#include #include #include #include #include #include // for strstr #include #include #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); } 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"; } } } } #pragma once