📄 main.cpp
字号:
#include "stdafx.h"
#include "enumser.h"
void main()
{
//Initialize COM (Required by CEnumerateSerial::UsingWMI)
CoInitialize(NULL);
//Initialize COM security (Required by CEnumerateSerial::UsingWMI)
HRESULT hr = CoInitializeSecurity(NULL, -1, NULL, NULL, RPC_C_AUTHN_LEVEL_DEFAULT, RPC_C_IMP_LEVEL_IMPERSONATE, NULL, EOAC_NONE, NULL);
if (FAILED(hr))
{
_tprintf(_T("Failed to initialize COM security, Error:%x\n"), hr);
CoUninitialize();
return;
}
CUIntArray ports;
_tprintf(_T("CreateFile method reports the following serial ports\n"));
if (CEnumerateSerial::UsingCreateFile(ports))
{
for (int i=0; i<ports.GetSize(); i++)
_tprintf(_T("COM%d\n"), ports.ElementAt(i));
}
else
_tprintf(_T("CEnumerateSerial::UsingCreateFile failed, Error:%d\n"), GetLastError());
_tprintf(_T("QueryDosDevice method reports the following serial ports\n"));
if (CEnumerateSerial::UsingQueryDosDevice(ports))
{
for (int i=0; i<ports.GetSize(); i++)
_tprintf(_T("COM%d\n"), ports.ElementAt(i));
}
else
_tprintf(_T("CEnumerateSerial::UsingQueryDosDevice failed, Error:%d\n"), GetLastError());
_tprintf(_T("GetDefaultCommConfig method reports the following serial ports\n"));
if (CEnumerateSerial::UsingGetDefaultCommConfig(ports))
{
for (int i=0; i<ports.GetSize(); i++)
_tprintf(_T("COM%d\n"), ports.ElementAt(i));
}
else
_tprintf(_T("CEnumerateSerial::UsingGetDefaultCommConfig failed, Error:%d\n"), GetLastError());
_tprintf(_T("Device Manager (SetupAPI) method reports the following serial ports\n"));
CStringArray sFriendlyNames;
if (CEnumerateSerial::UsingSetupAPI(ports, sFriendlyNames))
{
for (int i=0; i<ports.GetSize(); i++)
_tprintf(_T("%s (COM%d)\n"), sFriendlyNames.GetAt(i), ports.ElementAt(i));
}
else
_tprintf(_T("CEnumerateSerial::UsingSetupAPI failed, Error:%d\n"), GetLastError());
_tprintf(_T("EnumPorts method reports the following serial ports\n"));
if (CEnumerateSerial::UsingEnumPorts(ports))
{
for (int i=0; i<ports.GetSize(); i++)
_tprintf(_T("COM%d\n"), ports.ElementAt(i));
}
else
_tprintf(_T("CEnumerateSerial::UsingEnumPorts failed, Error:%d\n"), GetLastError());
_tprintf(_T("WMI method reports the following serial ports\n"));
if (CEnumerateSerial::UsingWMI(ports))
{
for (int i=0; i<ports.GetSize(); i++)
_tprintf(_T("COM%d\n"), ports.ElementAt(i));
}
else
_tprintf(_T("CEnumerateSerial::UsingWMI failed, Error:%d\n"), GetLastError());
CoUninitialize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -