// seriallist.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <windows.h>
#include <setupapi.h>
#include <locale.h>
BOOL WcharIsDigit(WCHAR ch)
{
if (ch >= L'0' && ch <= L'9')
{
return TRUE;
}
return FALSE;
}
// The following define is from ntddser.h in the DDK. It is also
// needed for serial port enumeration.
#ifndef GUID_CLASS_COMPORT
DEFINE_GUID(GUID_CLASS_COMPORT, 0x86e0d1e0L, 0x8089, 0x11d0, 0x9c, 0xe4, \
0x08, 0x00, 0x3e, 0x30, 0x1f, 0x73);
#endif
BOOL EnumPortsWdm()
{
// Create a device information set that will be the container for
// the device interfaces.
GUID *guidDev = (GUID*)&GUID_CLASS_COMPORT;
HDEVINFO hDevInfo = INVALID_HANDLE_VALUE;
SP_DEVICE_INTERFACE_DETAIL_DATA *pDetData = NULL;
hDevInfo = SetupDiGetClassDevs(guidDev,
NULL,
NULL,
DIGCF_PRESENT | DIGCF_DEVICEINTERFACE
);
if (INVALID_HANDLE_VALUE == hDevInfo)
{
return FALSE;
}
// Enumerate the serial ports
BOOL bOk = TRUE;
SP_DEVICE_INTERFACE_DATA ifcData;
DWORD dwDetDataSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA) + 256+256;
pDetData = (SP_DEVICE_INTERFACE_DETAIL_DATA*)new char[dwDetDataSize];
if (!pDetData)
{
return FALSE;
}
// This is required, according to the documentation. Yes,
// it's weird.
ifcData.cbSize = sizeof(SP_DEVICE_INTERFACE_DATA);
pDetData->cbSize = sizeof(SP_DEVICE_INTERFACE_DETAIL_DATA);
for (DWORD ii = 0; bOk; ii++)
{
bOk = SetupDiEnumDeviceInterfaces(hDevInfo,
NULL, guidDev, ii, &ifcData);
if (bOk)
{
// Got a device. Get the details.
SP_DEVINFO_DATA devdata = { sizeof(SP_DEVINFO_DATA) };
bOk = SetupDiGetDeviceInterfaceDetail(hDevInfo,
&ifcData, pDetData, dwDetDataSize, NULL, &devdata);
if (bOk) {
// Got a path to the device. Try to get some more info.
WCHAR fname[256] = { 0 };
WCHAR desc[256] = { 0 };
BOOL bSuccess = SetupDiGetDeviceRegistryProperty(
hDevInfo, &devdata, SPDRP_FRIENDLYNAME, NULL,
(PBYTE)fname, sizeof(fname), NULL);
bSuccess = bSuccess && SetupDiGetDeviceRegistryProperty(
hDevInfo, &devdata, SPDRP_DEVICEDESC, NULL,
(PBYTE)desc, sizeof(desc), NULL);
BOOL bUsbDevice = FALSE;
WCHAR locinfo[256] = { 0 };
if (SetupDiGetDeviceRegistryProperty(
hDevInfo, &devdata, SPDRP_LOCATION_INFORMATION, NULL,
(PBYTE)locinfo, sizeof(locinfo), NULL))
{
bUsbDevice = (wcsncmp(locinfo, L"USB", 3) == 0);
}
if (bSuccess)
{
printf("FriendlyName = %S\r\n", fname);
printf("Port Desc = %S\r\n", desc);
}
}
else
{
if (pDetData != NULL)
{
delete[](char*)pDetData;
}
if (hDevInfo != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
}
return FALSE;
}
}
else
{
DWORD err = GetLastError();
if (err != ERROR_NO_MORE_ITEMS)
{
if (pDetData != NULL)
{
delete[](char*)pDetData;
}
if (hDevInfo != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
}
return FALSE;
}
}
}
if (pDetData != NULL)
{
delete[](char*)pDetData;
}
if (hDevInfo != INVALID_HANDLE_VALUE)
{
SetupDiDestroyDeviceInfoList(hDevInfo);
}
return TRUE;
}
int main(int argc, char* argv[])
{
setlocale(LC_ALL, "chs");
EnumPortsWdm();
getchar();
return 0;
}
链接器-->输入 ->库加入 setupapi.lib