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 10
Windows 10
A Microsoft operating system that runs on personal computers and tablets.
11,195 questions
C++
C++
A high-level, general-purpose programming language, created as an extension of the C programming language, that has object-oriented, generic, and functional features in addition to facilities for low-level memory manipulation.
3,637 questions
Windows Hardware Performance
Windows Hardware Performance
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.Hardware Performance: Delivering / providing hardware or hardware systems or adjusting / adapting hardware or hardware systems.
1,579 questions
0 comments No comments
{count} votes

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

0 additional answers

Sort by: Most helpful