Created
October 28, 2018 05:02
-
-
Save sergev/b55656ebcdfa1cd8a40f0d9ecdeac1bf to your computer and use it in GitHub Desktop.
Enumerating serial ports on Windows, from https://www.codeproject.com/Articles/4187/Serial-ports-Enumeration-and-FIFO-control
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 <setupapi.h> | |
#define MAX_NAME_PORTS 7 | |
#define RegDisposition_OpenExisting (0x00000001) // open key only if exists | |
#define CM_REGISTRY_HARDWARE (0x00000000) | |
typedef DWORD WINAPI | |
(* CM_Open_DevNode_Key)(DWORD, DWORD, DWORD, DWORD, ::PHKEY, DWORD); | |
HANDLE BeginEnumeratePorts(VOID) | |
{ | |
BOOL guidTest=FALSE; | |
DWORD RequiredSize=0; | |
int j; | |
HDEVINFO DeviceInfoSet; | |
char* buf; | |
guidTest=SetupDiClassGuidsFromNameA( | |
"Ports",0,0,&RequiredSize); | |
if(RequiredSize < 1)return -1; | |
buf=malloc(RequiredSize*sizeof(GUID)); | |
guidTest=SetupDiClassGuidsFromNameA( | |
"Ports",buf,RequiredSize*sizeof(GUID),&RequiredSize); | |
if(!guidTest)return -1; | |
DeviceInfoSet=SetupDiGetClassDevs( | |
buf,NULL,NULL,DIGCF_PRESENT); | |
if(DeviceInfoSet == INVALID_HANDLE_VALUE)return -1; | |
free(buf); | |
return DeviceInfoSet; | |
} | |
BOOL EnumeratePortsNext(HANDLE DeviceInfoSet, LPTSTR lpBuffer) | |
{ | |
static CM_Open_DevNode_Key OpenDevNodeKey=NULL; | |
static HINSTANCE CfgMan; | |
int res1; | |
char DevName[MAX_NAME_PORTS]={0}; | |
static int numDev=0; | |
int numport; | |
SP_DEVINFO_DATA DeviceInfoData={0}; | |
DeviceInfoData.cbSize=sizeof(SP_DEVINFO_DATA); | |
if(!DeviceInfoSet || !lpBuffer)return -1; | |
if(!OpenDevNodeKey) | |
{ | |
CfgMan=LoadLibrary("cfgmgr32"); | |
if(!CfgMan)return FALSE; | |
OpenDevNodeKey= | |
(CM_Open_DevNode_Key)GetProcAddress(CfgMan,"CM_Open_DevNode_Key"); | |
if(!OpenDevNodeKey) | |
{ | |
FreeLibrary(CfgMan); | |
return FALSE; | |
} | |
} | |
while(TRUE) | |
{ | |
HKEY KeyDevice; | |
DWORD len; | |
res1=SetupDiEnumDeviceInfo( | |
DeviceInfoSet,numDev,&DeviceInfoData); | |
if(!res1) | |
{ | |
SetupDiDestroyDeviceInfoList(DeviceInfoSet); | |
FreeLibrary(CfgMan); | |
OpenDevNodeKey=NULL; | |
return FALSE; | |
} | |
res1=OpenDevNodeKey(DeviceInfoData.DevInst,KEY_QUERY_VALUE,0, | |
RegDisposition_OpenExisting,&KeyDevice,CM_REGISTRY_HARDWARE); | |
if(res1 != ERROR_SUCCESS)return NULL; | |
len=MAX_NAME_PORTS; | |
res1=RegQueryValueEx( | |
KeyDevice, // handle of key to query | |
"portname", // address of name of value to query | |
NULL, // reserved | |
NULL, // address of buffer for value type | |
DevName, // address of data buffer | |
&len // address of data buffer size | |
); | |
RegCloseKey(KeyDevice); | |
if(res1 != ERROR_SUCCESS)return NULL; | |
numDev++; | |
if(memicmp(DevName, "com", 3))continue; | |
numport=atoi(DevName+3); | |
if(numport > 0 && numport <= 256) | |
{ | |
strcpy(lpBuffer,DevName); | |
return TRUE; | |
} | |
FreeLibrary(CfgMan); | |
OpenDevNodeKey=NULL; | |
return FALSE; | |
} | |
} | |
BOOL EndEnumeratePorts(HANDLE DeviceInfoSet) | |
{ | |
if(SetupDiDestroyDeviceInfoList(DeviceInfoSet))return TRUE; | |
else return FALSE; | |
} |
Author
sergev
commented
Oct 28, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment