首页 > 其他分享 >通过注册表枚举串口

通过注册表枚举串口

时间:2022-10-30 12:45:55浏览次数:40  
标签:hKey ERROR lstatus 枚举 串口 注册表

在编写串口代码时,需要枚举当前系统上的串口,通过api RegEnumValueA(W)可以遍历注册表HKEY_LOCAL_MACHINE\HARDWARE\DEVICEMAP\SERIALCOMM下的项,枚举串口,代码如下:

#include <Windows.h>
#pragma comment(lib, "Advapi32.lib")
void EnumSerialCom()
{
  HKEY hKey;
  TCHAR *pSubKey = L"Hardware\\DeviceMap\\SerialComm";
  int ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, pSubKey, 0, KEY_READ, &hKey);
  if (ret != ERROR_SUCCESS)
  {
    printf("打开注册表失败\n");
    return;
  }
  TCHAR szPortName[256] = { 0 }, szComName[256] = { 0 };
  DWORD dwLong, dwSize;
  int nCount = 0;
  while (true)
  {
    dwLong = dwSize = 256;
    LSTATUS lstatus = RegEnumValue(hKey, nCount, szPortName, &dwLong, NULL, NULL, (PUCHAR)szComName, &dwSize);
    if (ERROR_MORE_DATA == lstatus)
    {
      printf("缓冲区太小\n");
      break;
    }
    if (ERROR_SUCCESS != lstatus)
    {
      if (lstatus == ERROR_NO_MORE_ITEMS)//表示枚举完毕
        break;
    }
    printf("%ls %ls\n", szPortName, szComName);
    nCount++;
  }
  RegCloseKey(hKey);
}

标签:hKey,ERROR,lstatus,枚举,串口,注册表
From: https://www.cnblogs.com/ps12345678/p/16841000.html

相关文章

  • c语言学习----枚举
      #include<stdio.h>#include<stdlib.h>#defineFIRST1#defineSECONED2#defineTHIRD3intmain(void){printf("%d,%d,%d\n",FIRST,SECONED,THIR......
  • CF1622D. Shuffle 题解 组合数学/枚举
    题目链接:​​https://codeforces.com/problemset/problem/1622/D​​题目大意:给定一个长度为\(n\)的01字符串\(s\),你可以对这个字符串进行最多一次操作,该次操作需要选择......
  • 驱动开发:内核监控Register注册表回调
    在笔者前一篇文章`《驱动开发:内核枚举Registry注册表回调》`中实现了对注册表的枚举,本章将实现对注册表的监控,不同于32位系统在64位系统中,微软为我们提供......
  • FPGA中串口通信的时钟频率和波特率计数
    目录1.什么是波特率2.串口传输格式3.时钟频率的计数器分频和波特率关系1.什么是波特率波特率bandrate,指的是串口通信的速率,即串口通信时每秒钟可以传输多少个二进制......
  • AcWing 93. 递归实现组合型枚举
    #include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=30;//多开几个,防止边界越界intn,m;intway[N];//表示方案//......
  • c++17 注解标签 attributes & c++ 枚举值
    c++标注c++17后逐渐完善注解标签语法:[[attribute]]types/functions/enums/etc 告诉编译器没有返回值[[noreturn]]常用于系统函数设计,如std::abort()std::exit()......
  • 枚举EnumHelper
    usingSystem.ComponentModel;usingSystem.Reflection;namespaceMEAS.Common{publicclassEnumHelper{//定义一个用于保存静态变量的实例......
  • c语言—自定义类型(结构体,枚举,联合)进阶篇—笔记
    个人觉得结构体相当于类,应该是比较实用的功能。1.结构体结构是一些值的集合,这些值称为成员变量。结构的每个成员可以是不同类型的变量。structtag{member-list;}variable-l......
  • AcWing 94. 递归实现排列型枚举
    #include<iostream>#include<cstring>#include<cstdio>#include<algorithm>usingnamespacestd;constintN=10;intn;intpath[N];//0表示没有放数,1-n表示......
  • AcWing 92. 递归实现指数型枚举
    题解1:#include<iostream>#include<cstring>#include<algorithm>usingnamespacestd;constintN=16;intn;boolst[N];//false不选,true选voiddfs(intu){......