windows driver 枚举串口

//枚举串口
	NTSTATUS status;
	HANDLE hKey = NULL;
	OBJECT_ATTRIBUTES oa;
	UNICODE_STRING strPath = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\HARDWARE\\DEVICEMAP\\SERIALCOMM");//
	ULONG ResultLength = 0;

	PKEY_FULL_INFORMATION PKFI;
	PKEY_VALUE_PARTIAL_INFORMATION pkvpi;
	UNICODE_STRING strOutPut;
	wchar_t strTemp[ArrayLength] = {0};

	ULONG i = 0;
	ULONG nSubKeys = 0;

	RtlInitEmptyUnicodeString(&strOutPut, strTemp, ArrayLength);

	InitializeObjectAttributes(&oa, &strPath, OBJ_CASE_INSENSITIVE | OBJ_KERNEL_HANDLE, NULL, NULL);
	status = ZwOpenKey(&hKey, KEY_ALL_ACCESS, &oa);
	if (!NT_SUCCESS(status)){
		KdPrint(("ZwOpenKey failed"));
		return status;
	}
	
	ZwQueryKey(hKey, KeyFullInformation, NULL, 0, &ResultLength);
	PKFI = (PKEY_FULL_INFORMATION)ExAllocatePool(PagedPool, ResultLength);
	if (PKFI == NULL){
		KdPrint(("ExAllocatePool failed"));
		ZwClose(hKey);
		ExFreePool(PKFI);
		return STATUS_INSUFFICIENT_RESOURCES;
	}
	status = ZwQueryKey(hKey, KeyFullInformation, PKFI, ResultLength, &ResultLength);
	if (!NT_SUCCESS(status)){
		KdPrint(("ZwEnumerateKey failed with error %I32X", status));
		ZwClose(hKey);
		ExFreePool(PKFI);
		return status;
	}

	nSubKeys = PKFI->Values;
	KdPrint(("子键个数: %I32d", PKFI->Values));
	ExFreePool(PKFI);
	
	for (; i < nSubKeys; i++){
		ZwEnumerateValueKey(hKey, i,  KeyValuePartialInformation, NULL, 0, &ResultLength);
		pkvpi = (PKEY_VALUE_PARTIAL_INFORMATION)ExAllocatePool(PagedPool, ResultLength);
		if (pkvpi == NULL){
			KdPrint(("ExAllocatePoolWithTag failed"));
			ZwClose(hKey);
			ExFreePool(pkvpi);
			return STATUS_INSUFFICIENT_RESOURCES ;
		}
		
		status = ZwEnumerateValueKey(hKey, i, KeyValuePartialInformation, pkvpi, ResultLength, &ResultLength);
		if (!NT_SUCCESS(status)){
			KdPrint(("ZwEnumerateValueKey failed with error %I32X", status));
			ZwClose(hKey);
			ExFreePool(pkvpi);
			return status;
		}

		RtlStringCbPrintfW(strOutPut.Buffer, ArrayLength, L"%s", pkvpi->Data);
		KdPrint(("%ws", strOutPut.Buffer));
		ExFreePool(pkvpi);
	}

	ZwClose(hKey);
	return STATUS_SUCCESS;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值