如何获取串口名称

lqiang5182 2016-05-15 09:33:53
硬盘上文件句柄,可通过ZwQueryInformationFile获取到文件路径及名称,串口句柄为什么获取不到串口名称(如,“COM1”),该用什么方法呢,求指教。。。。
...全文
2032 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
汪宁宇 2016-05-18
  • 打赏
  • 举报
回复
重新补一下,乱码了
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
using namespace std;

typedef struct _OBJECT_NAME_INFORMATION {
	WORD Length;
	WORD MaximumLength;
	LPWSTR Buffer;
} OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;

typedef long(__stdcall*PNtQueryObject)(HANDLE ObjectHandle,ULONG ObjectInformationClass,PVOID ObjectInformation,ULONG ObjectInformationLength,PULONG ReturnLength);

void wcharTochar(const wchar_t *wchar, char *chr, int length)  
{  
	WideCharToMultiByte( CP_ACP, 0, wchar, -1, chr, length, NULL, NULL );  
}

void charTowchar(const char *chr, wchar_t *wchar, int size)  
{     
	MultiByteToWideChar( CP_ACP, 0, chr, strlen(chr)+1, wchar, size/sizeof(wchar[0]) );  
}

int main()
{
	PNtQueryObject			NtQueryObject;
	OBJECT_NAME_INFORMATION name,*pname;ULONG len;
	HANDLE					hFile = INVALID_HANDLE_VALUE;
	char					szPath[MAX_PATH] = {0x00};

	NtQueryObject = reinterpret_cast<PNtQueryObject>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"),"NtQueryObject"));
	if(NtQueryObject == NULL)
		return 0;

	hFile = CreateFile("C:\\Windows\\System32\\ntdll.dll",FILE_READ_DATA,1,0,OPEN_EXISTING,0,0);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		NtQueryObject(hFile,1,&name,sizeof name,&len);
		pname=reinterpret_cast<POBJECT_NAME_INFORMATION>(new char[len]);
		NtQueryObject(hFile,1,pname,len,&len);
		wcharTochar(pname->Buffer,szPath,MAX_PATH);
		printf("Path: %s\n",szPath);
		CloseHandle(hFile);
		hFile = INVALID_HANDLE_VALUE;
	}

	hFile=CreateFile("\\\\.\\COM4", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		NtQueryObject(hFile,1,&name,sizeof name,&len);
		pname=reinterpret_cast<POBJECT_NAME_INFORMATION>(new char[len]);
		NtQueryObject(hFile,1,pname,len,&len);
		wcharTochar(pname->Buffer,szPath,MAX_PATH);
		printf("Path: %s\n",szPath);

		//
		// 如果是USB串口,注意要查注册表
		// HKEY_LOCAL_MACHINE\\Hardware\\DeviceMap\\SerialComm
		// \Device\Serial2
		//
		CloseHandle(hFile);
		hFile = INVALID_HANDLE_VALUE;
	}

	return 0;
}
汪宁宇 2016-05-18
  • 打赏
  • 举报
回复
引用 楼主 lqiang5182 的回复:
硬盘上文件句柄,可通过ZwQueryInformationFile获取到文件路径及名称,串口句柄为什么获取不到串口名称(如,“COM1”),该用什么方法呢,求指教。。。。
以前用到过的函数,你参考一下,USB串口有点特殊,需要查注册表
#include "stdafx.h"
#include <windows.h>
#include <tchar.h>
#include <stdio.h>
#include <iostream>
using namespace std;

typedef struct _OBJECT_NAME_INFORMATION {
	WORD Length;
	WORD MaximumLength;
	LPWSTR Buffer;
} OBJECT_NAME_INFORMATION, *POBJECT_NAME_INFORMATION;

typedef long(__stdcall*PNtQueryObject)(HANDLE ObjectHandle,ULONG ObjectInformationClass,PVOID ObjectInformation,ULONG ObjectInformationLength,PULONG ReturnLength);

void wcharTochar(const wchar_t *wchar, char *chr, int length)  
{  
	WideCharToMultiByte( CP_ACP, 0, wchar, -1, chr, length, NULL, NULL );  
}

void charTowchar(const char *chr, wchar_t *wchar, int size)  
{     
	MultiByteToWideChar( CP_ACP, 0, chr, strlen(chr)+1, wchar, size/sizeof(wchar[0]) );  
}

int main()
{
	PNtQueryObject			NtQueryObject;
	OBJECT_NAME_INFORMATION name,*pname;ULONG len;
	HANDLE					hFile = INVALID_HANDLE_VALUE;
	char					szPath[MAX_PATH] = {0x00};

	NtQueryObject = reinterpret_cast<PNtQueryObject>(GetProcAddress(GetModuleHandleW(L"ntdll.dll"),"NtQueryObject"));
	if(NtQueryObject == NULL)
		return 0;

	hFile = CreateFile("C:\\Windows\\System32\\ntdll.dll",FILE_READ_DATA,1,0,OPEN_EXISTING,0,0);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		NtQueryObject(hFile,1,&name,sizeof name,&len);
		pname=reinterpret_cast<POBJECT_NAME_INFORMATION>(new char[len]);
		NtQueryObject(hFile,1,pname,len,&len);
		wcharTochar(pname->Buffer,szPath,MAX_PATH);
		printf("Path: %s\n",szPath);
		CloseHandle(hFile);
		hFile = INVALID_HANDLE_VALUE;
	}

	hFile=CreateFile("\\\\.\\COM4", GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
	if(hFile != INVALID_HANDLE_VALUE)
	{
		NtQueryObject(hFile,1,&name,sizeof name,&len);
		pname=reinterpret_cast<POBJECT_NAME_INFORMATION>(new char[len]);
		NtQueryObject(hFile,1,pname,len,&len);
		wcharTochar(pname->Buffer,szPath,MAX_PATH);
		printf("Path: %s\n",szPath);

		//
		// Èç¹ûÊÇUSB´®¿Ú£¬×¢ÒâÒª²é×¢²á±í
		// HKEY_LOCAL_MACHINE\\Hardware\\DeviceMap\\SerialComm
		// \Device\Serial2
		//
		CloseHandle(hFile);
		hFile = INVALID_HANDLE_VALUE;
	}

	return 0;
}
笨笨仔 2016-05-17
  • 打赏
  • 举报
回复
引用 4 楼 lqiang5182 的回复:
wxhxj0268 ,请问该如何根据串口句柄获得数值顺序值,查了很多资料,仍一无所获,还望赐教

我的状态图供参考:
阿先森 2016-05-17
  • 打赏
  • 举报
回复
可通过CxDeviceFind或CxDeviceMapFind获取 http://www.cnblogs.com/EdmundDwyane/p/5503578.html
lqiang5182 2016-05-17
  • 打赏
  • 举报
回复
wxhxj0268 ,请问该如何根据串口句柄获得数值顺序值,查了很多资料,仍一无所获,还望赐教
笨笨仔 2016-05-17
  • 打赏
  • 举报
回复
引用 2 楼 lqiang5182 的回复:
我不是要枚举所有串口,而是要根据串口句柄获得串口名称,如 HANDLE hCom=CreateFile("COM1",........),我想在DLL中,根据hCom而获得"COM1"
串口返回的是一个数值顺序,至于“COM”几需要自己完成字符串的组合。 串口返回的是一个数值,需要自己转换成字符串,例如:

// 显示连接状态type: 0-未连接,1-连接中,2-已连接
	CString com=L"";
	if(type==2)
	{
		com.Format(L"COM%d ",m_comCode);
	}
lqiang5182 2016-05-17
  • 打赏
  • 举报
回复
wxhxj0268,虽然看不太懂,但还是谢谢你 其实我是想知道有没有直接点的办法,比如说通过某个API或某个算法可以获取到的
lqiang5182 2016-05-16
  • 打赏
  • 举报
回复
我不是要枚举所有串口,而是要根据串口句柄获得串口名称,如 HANDLE hCom=CreateFile("COM1",........),我想在DLL中,根据hCom而获得"COM1"
用户 昵称 2016-05-16
  • 打赏
  • 举报
回复

//***********************
// 枚举所有的串口,以及名称
//***********************
int
EnumAllComm( TCHAR *buf )
{
    HKEY hkey;  
    int result;
	int i = 0;

	*buf = 0;

    result = RegOpenKeyEx( HKEY_LOCAL_MACHINE,
							_T( "Hardware\\DeviceMap\\SerialComm" ),
							NULL,
							KEY_READ,
							&hkey );

	if( ERROR_SUCCESS == result )   //   打开串口注册表   
	{   
		TCHAR portName[ 0x100 ], commName[ 0x100 ];
		DWORD dwLong, dwSize;

		do
		{   
			dwSize = sizeof( portName ) / sizeof( TCHAR );
			dwLong = dwSize;
			result = RegEnumValue( hkey, i, portName, &dwLong, NULL, NULL, ( LPBYTE )commName, &dwSize );
			if( ERROR_NO_MORE_ITEMS == result )
			{
				//   枚举串口
				break;   //   commName就是串口名字
			}
			_tcscpy( buf, commName );
			buf += ( _tcslen( buf ) + 1 );
			i++;   
		} while ( 1 );

		RegCloseKey( hkey );   
	}  

	*buf = 0;

	return i;
}

2,643

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 硬件/系统
社区管理员
  • 硬件/系统社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧