Created
October 6, 2014 14:07
-
-
Save daisukekobayashi/083b70b83fefd5281a4f to your computer and use it in GitHub Desktop.
Listing devices and get com port of specified devices.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <string> | |
#include <vector> | |
#include <windows.h> | |
#include <setupapi.h> | |
#pragma comment(lib,"setupapi.lib") | |
typedef std::basic_string<TCHAR> tstring; | |
bool ListDevice(const GUID& guid, std::vector<tstring>& devices) | |
{ | |
SP_DEVINFO_DATA devInfoData; | |
ZeroMemory(&devInfoData, sizeof(devInfoData)); | |
devInfoData.cbSize = sizeof(devInfoData); | |
int nDevice = 0; | |
std::vector<tstring> tmp; | |
HDEVINFO hDeviceInfo = SetupDiGetClassDevs(&guid, 0, 0, DIGCF_PRESENT | | |
DIGCF_DEVICEINTERFACE); | |
while (SetupDiEnumDeviceInfo(hDeviceInfo, nDevice++, &devInfoData)) { | |
BYTE friendly_name[300]; | |
SetupDiGetDeviceRegistryProperty(hDeviceInfo, &devInfoData, | |
SPDRP_FRIENDLYNAME, NULL, friendly_name, | |
sizeof(friendly_name), NULL); | |
tmp.push_back(tstring((TCHAR*)friendly_name)); | |
} | |
SetupDiDestroyDeviceInfoList(hDeviceInfo); | |
devices.swap(tmp); | |
return true; | |
} | |
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, | |
PSTR szCmdLine, int iCmdShow) | |
{ | |
std::vector<tstring> dev; | |
ListDevice(GUID_DEVINTERFACE_COMPORT, dev); | |
for (int i = 0; i < dev.size(); i++) { | |
MessageBox(NULL, dev[i].c_str(), NULL, IDOK); | |
if (dev[i].find(TEXT("Matrox")) != tstring::npos) { | |
int start = dev[i].find(TEXT("(")); | |
int end = dev[i].find(TEXT(")")); | |
MessageBox(NULL, dev[i].substr(start + 1, end - (start + 1)).c_str(), NULL, IDOK); | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment