windows driver 枚举串口

来源:互联网 发布:淘宝两钻买家号多少钱 编辑:程序博客网 时间:2024/06/06 08:59
//枚举串口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;

0 0