100 lines
3.4 KiB
C++
100 lines
3.4 KiB
C++
#include <windows.h>
|
|
#include <setupapi.h>
|
|
#include <cfgmgr32.h>
|
|
#include <tchar.h>
|
|
#include <stdio.h>
|
|
#include <string.h> // for strstr
|
|
#include <devguid.h>
|
|
|
|
#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);
|
|
}
|
|
#pragma once |