reading the cpu frequncy fromregistry in kernel

daniel02x 26 Reputation points
2023-05-08T03:25:27.36+00:00

hello im trying to read the cpu freq from the registry am i doing correct ?



__int64 getCPUFrequencyFromRegistryA()
{
	NTSTATUS status;
	OBJECT_ATTRIBUTES objAttributes;
	UNICODE_STRING keyPath = RTL_CONSTANT_STRING(L"\\Registry\\Machine\\HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
	HANDLE hKey;

	InitializeObjectAttributes(&objAttributes, &keyPath, OBJ_CASE_INSENSITIVE, NULL, NULL);

	status = ZwOpenKey(&hKey, KEY_QUERY_VALUE | KEY_WOW64_64KEY, &objAttributes);
	if (NT_SUCCESS(status))
	{
		UNICODE_STRING valueName = RTL_CONSTANT_STRING(L"~MHz");

		// Allocate a larger buffer for the KEY_VALUE_PARTIAL_INFORMATION structure
		UCHAR buffer[sizeof(KEY_VALUE_PARTIAL_INFORMATION) + sizeof(DWORD)];
		PKEY_VALUE_PARTIAL_INFORMATION kvpi = (PKEY_VALUE_PARTIAL_INFORMATION)buffer;
		ULONG resultLength;

		status = ZwQueryValueKey(hKey, &valueName, KeyValuePartialInformation, kvpi, sizeof(buffer), &resultLength);
		if (NT_SUCCESS(status))
		{
			DWORD frequency = *(PDWORD)(kvpi->Data);

			// Check the status of ZwClose(hKey) and handle potential errors
			status = ZwClose(hKey);
			if (!NT_SUCCESS(status)) {
				// Handle error
			}

			return (__int64)frequency * 1000000; // Multiply by 1,000,000 to get the frequency in Hz.
		}
	}

	status = ZwClose(hKey);
	if (!NT_SUCCESS(status)) {
		// Handle error
	}

	return 0;
}

Windows for business | Windows Client for IT Pros | Devices and deployment | Other
Windows for business | Windows Client for IT Pros | User experience | Other
Developer technologies | C++
0 comments No comments
{count} votes

Accepted answer
  1. Castorix31 90,696 Reputation points
    2023-05-08T06:28:01.4666667+00:00
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.