2,643
社区成员
发帖
与我相关
我的任务
分享#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;
}#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;
}
// 显示连接状态type: 0-未连接,1-连接中,2-已连接
CString com=L"";
if(type==2)
{
com.Format(L"COM%d ",m_comCode);
}
//***********************
// 枚举所有的串口,以及名称
//***********************
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;
}