本文介绍了为什么RegOpenKeyEx()和RegQueryValueEx()中的相同路径不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我尝试从注册表中读取一个值,请尝试以下代码
注意
:基于Windows7
:使用MSVC ++ 2010作为工具



Hello everyone,

I try to read a value from registry, I try this code
Note
: based on Windows7
: using MSVC++2010 as tool



#include <windows.h>
#include <malloc.h>
#include <stdio.h>

#define TOTALBYTES    8192


void main()
{
    DWORD BufferSize = TOTALBYTES;
    DWORD cbData;
    DWORD dwRet;
	HKEY result;

    PPERF_DATA_BLOCK PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );

	dwRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,
							TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\"),
							0,
							KEY_ALL_ACCESS,
							&result
		);
    if( dwRet == ERROR_SUCCESS )
        printf("\n\RegOpenKeyEx result : %d, function successed\n", &result);
    else printf("\nRegOpenKeyEx failed (%d)\n", dwRet);
	
	dwRet = RegQueryValueEx( HKEY_LOCAL_MACHINE,
                             TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz"),
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData );
	if( dwRet == ERROR_SUCCESS )
        printf("\n\nRegQueryValueEx result(~MHz) %d, function successed\n", &cbData);
    else printf("\nRegQueryValueEx(~MHz) failed (%d)\n", dwRet);

	dwRet = RegQueryValueEx( HKEY_LOCAL_MACHINE,
                             TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0"),
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData);
	if( dwRet == ERROR_SUCCESS )
        printf("\n\nRegQueryValueEx result %d, function successed\n", &cbData);
    else printf("\nRegQueryValueEx failed (%d)\n", dwRet);

	dwRet = RegCloseKey(result);
	if( dwRet == ERROR_SUCCESS )
        printf("\n\nRegCloseKey successed\n");
    else printf("\nRegCloseKey failed (%d)\n", dwRet);


	system("pause");
}



为什么RegQueryValueEx()中的两个路径不起作用,因为它们在RegOpenKeyEx()和RegCloseKey()上起作用?



Why two paths in RegQueryValueEx() is not working, as it works on RegOpenKeyEx() and RegCloseKey()?

推荐答案

windyl3ig写道:
windyl3ig wrote:
dwRet = RegQueryValueEx( HKEY_LOCAL_MACHINE,
                             TEXT("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0\\~MHz"),
                             NULL,
                             NULL,
                             (LPBYTE) PerfData,
                             &cbData );





to

DWORD dwMHz;
cbData = sizeof(dwMHz);
dwRet = RegQueryValueEx( result,
                         TEXT("~MHz"),
                         NULL,
                         NULL,
                         (LPBYTE) &dwMHz,
                         &cbData );


这篇关于为什么RegOpenKeyEx()和RegQueryValueEx()中的相同路径不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 05:09