高分请大家看一句注册表RegQueryInfoKey()哪里错了?

calm_zw 2002-08-04 04:05:27
我想读出某一目录下面的第一级子目录的个数
如Software\\ODBC\\ODBC.ini下面所有数据源的个数,现在有11个
我采用下面的语句:
DWORD subkeynum;
RegQueryInfoKey(keyid,NULL,NULL,NULL,&subkeynum,NULL,NULL,NULL,NULL,NULL,NULL,NULL);
可是读出subkeynum=72
各位是不是读出了不仅是第一级子目录的个数,而是所有的
并请大虾给出正确的解决方法,谢谢!
...全文
273 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
calm_zw 2002-08-04
  • 打赏
  • 举报
回复
大虾给我解释一下,重点部分
你写的好像和我问的不一样呀
^_^
wuxuan 2002-08-04
  • 打赏
  • 举报
回复

/*****************************************************************
* *
* Display the indexes and/or names for all performance objects, *
* instances, and counters. *
* *
*****************************************************************/

void main()
{
PPERF_DATA_BLOCK PerfData = NULL;
PPERF_OBJECT_TYPE PerfObj;
PPERF_INSTANCE_DEFINITION PerfInst;
PPERF_COUNTER_DEFINITION PerfCntr, CurCntr;
PPERF_COUNTER_BLOCK PtrToCntr;
DWORD BufferSize = TOTALBYTES;
DWORD i, j, k;

// Get the name strings through the registry.

GetNameStrings( );

// Allocate the buffer for the performance data.

PerfData = (PPERF_DATA_BLOCK) malloc( BufferSize );

while( RegQueryValueEx( HKEY_PERFORMANCE_DATA,
"Global",
NULL,
NULL,
(LPBYTE) PerfData,
&BufferSize ) == ERROR_MORE_DATA )
{
// Get a buffer that is big enough.

BufferSize += BYTEINCREMENT;
PerfData = (PPERF_DATA_BLOCK) realloc( PerfData, BufferSize );
}

// Get the first object type.

PerfObj = FirstObject( PerfData );

// Process all objects.

for( i=0; i < PerfData->NumObjectTypes; i++ )
{
// Display the object by index and name.

printf( "\nObject %ld: %s\n", PerfObj->ObjectNameTitleIndex,
lpNamesArray[PerfObj->ObjectNameTitleIndex] );

// Get the first counter.

PerfCntr = FirstCounter( PerfObj );

if( PerfObj->NumInstances > 0 )
{
// Get the first instance.

PerfInst = FirstInstance( PerfObj );

// Retrieve all instances.

for( k=0; k < PerfObj->NumInstances; k++ )
{
// Display the instance by name.

printf( "\n\tInstance %S: \n",
(char *)((PBYTE)PerfInst + PerfInst->NameOffset));
CurCntr = PerfCntr;

// Retrieve all counters.

for( j=0; j < PerfObj->NumCounters; j++ )
{
// Display the counter by index and name.

printf("\t\tCounter %ld: %s\n",
CurCntr->CounterNameTitleIndex,
lpNamesArray[CurCntr->CounterNameTitleIndex]);

// Get the next counter.

CurCntr = NextCounter( CurCntr );
}

// Get the next instance.

PerfInst = NextInstance( PerfInst );
}
}
else
{
// Get the counter block.

PtrToCntr = (PPERF_COUNTER_BLOCK) ((PBYTE)PerfObj +
PerfObj->DefinitionLength );

// Retrieve all counters.

for( j=0; j < PerfObj->NumCounters; j++ )
{
// Display the counter by index and name.

printf( "\tCounter %ld: %s\n", PerfCntr->CounterNameTitleIndex,
lpNamesArray[PerfCntr->CounterNameTitleIndex] );

// Get the next counter.

PerfCntr = NextCounter( PerfCntr );
}
}

// Get the next object type.

PerfObj = NextObject( PerfObj );
}
}
wuxuan 2002-08-04
  • 打赏
  • 举报
回复
Displaying Object, Instance, and Counter Names
The performance data contains information for a variable number of object types, instances per object, and counters per object type. Therefore, the number and size of blocks in the performance data varies. To ensure that your application correctly receives the performance data, you must use the offsets included in the performance structures to navigate through the data. Every offset is a count of bytes relative to the structure containing it.

Note The reason the system uses offsets instead of pointers is that pointers are not valid across process boundaries. The addresses that the process that installs the counters would store would not be valid for the process that reads the counters.

The following example displays the index and name of each object, along with the indexes and names of its counters.

The object and counter names are stored in the registry, by index. This example creates a function, GetNameStrings, to load the indexes and names of each object and counter from the registry into an array, so that they can be easily accessed. GetNameStrings uses the following standard registry functions to access the data: RegOpenKey, RegCloseKey, RegQueryInfoKey, and RegQueryValueEx.

This example creates the following functions for navigating the performance data: FirstObject, FirstInstance, FirstCounter, NextCounter, NextInstance, and NextCounter. These functions navigate the performance data by using the offsets stored in the performance structures.

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

#define TOTALBYTES 8192
#define BYTEINCREMENT 1024

LPSTR lpNameStrings;
LPSTR *lpNamesArray;

/*****************************************************************
* *
* Functions used to navigate through the performance data. *
* *
*****************************************************************/

PPERF_OBJECT_TYPE FirstObject( PPERF_DATA_BLOCK PerfData )
{
return( (PPERF_OBJECT_TYPE)((PBYTE)PerfData +
PerfData->HeaderLength) );
}

PPERF_OBJECT_TYPE NextObject( PPERF_OBJECT_TYPE PerfObj )
{
return( (PPERF_OBJECT_TYPE)((PBYTE)PerfObj +
PerfObj->TotalByteLength) );
}

PPERF_INSTANCE_DEFINITION FirstInstance( PPERF_OBJECT_TYPE PerfObj )
{
return( (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj +
PerfObj->DefinitionLength) );
}

PPERF_INSTANCE_DEFINITION NextInstance(
PPERF_INSTANCE_DEFINITION PerfInst )
{
PPERF_COUNTER_BLOCK PerfCntrBlk;

PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst +
PerfInst->ByteLength);

return( (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk +
PerfCntrBlk->ByteLength) );
}

PPERF_COUNTER_DEFINITION FirstCounter( PPERF_OBJECT_TYPE PerfObj )
{
return( (PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj +
PerfObj->HeaderLength) );
}

PPERF_COUNTER_DEFINITION NextCounter(
PPERF_COUNTER_DEFINITION PerfCntr )
{
return( (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr +
PerfCntr->ByteLength) );
}

/*****************************************************************
* *
* Load the counter and object names from the registry to the *
* global variable lpNamesArray. *
* *
*****************************************************************/

void GetNameStrings( )
{
HKEY hKeyPerflib; // handle to registry key
HKEY hKeyPerflib009; // handle to registry key
DWORD dwMaxValueLen; // maximum size of key values
DWORD dwBuffer; // bytes to allocate for buffers
DWORD dwBufferSize; // size of dwBuffer
LPSTR lpCurrentString; // pointer for enumerating data strings
DWORD dwCounter; // current counter index

// Get the number of Counter items.

RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
0,
KEY_READ,
&hKeyPerflib);

dwBufferSize = sizeof(dwBuffer);

RegQueryValueEx( hKeyPerflib,
"Last Counter",
NULL,
NULL,
(LPBYTE) &dwBuffer,
&dwBufferSize );

RegCloseKey( hKeyPerflib );

// Allocate memory for the names array.

lpNamesArray = malloc( (dwBuffer+1) * sizeof(LPSTR) );

// Open the key containing the counter and object names.

RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
0,
KEY_READ,
&hKeyPerflib009);

// Get the size of the largest value in the key (Counter or Help).

RegQueryInfoKey( hKeyPerflib009,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&dwMaxValueLen,
NULL,
NULL);

// Allocate memory for the counter and object names.

dwBuffer = dwMaxValueLen + 1;

lpNameStrings = malloc( dwBuffer * sizeof(CHAR) );

// Read the Counter value.

RegQueryValueEx( hKeyPerflib009,
"Counter",
NULL,
NULL,
lpNameStrings, &dwBuffer );

// Load names into an array, by index.

for( lpCurrentString = lpNameStrings; *lpCurrentString;
lpCurrentString += (lstrlen(lpCurrentString)+1) )
{
dwCounter = atol( lpCurrentString );

lpCurrentString += (lstrlen(lpCurrentString)+1);

lpNamesArray[dwCounter] = (LPSTR) lpCurrentString;
}
}

wuxuan 2002-08-04
  • 打赏
  • 举报
回复
This example creates the following functions for navigating the performance data: FirstObject, FirstInstance, FirstCounter, NextCounter, NextInstance, and NextCounter. These functions navigate the performance data by using the offsets stored in the performance structures.

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

#define TOTALBYTES 8192
#define BYTEINCREMENT 1024

LPSTR lpNameStrings;
LPSTR *lpNamesArray;

/*****************************************************************
* *
* Functions used to navigate through the performance data. *
* *
*****************************************************************/

PPERF_OBJECT_TYPE FirstObject( PPERF_DATA_BLOCK PerfData )
{
return( (PPERF_OBJECT_TYPE)((PBYTE)PerfData +
PerfData->HeaderLength) );
}

PPERF_OBJECT_TYPE NextObject( PPERF_OBJECT_TYPE PerfObj )
{
return( (PPERF_OBJECT_TYPE)((PBYTE)PerfObj +
PerfObj->TotalByteLength) );
}

PPERF_INSTANCE_DEFINITION FirstInstance( PPERF_OBJECT_TYPE PerfObj )
{
return( (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfObj +
PerfObj->DefinitionLength) );
}

PPERF_INSTANCE_DEFINITION NextInstance(
PPERF_INSTANCE_DEFINITION PerfInst )
{
PPERF_COUNTER_BLOCK PerfCntrBlk;

PerfCntrBlk = (PPERF_COUNTER_BLOCK)((PBYTE)PerfInst +
PerfInst->ByteLength);

return( (PPERF_INSTANCE_DEFINITION)((PBYTE)PerfCntrBlk +
PerfCntrBlk->ByteLength) );
}

PPERF_COUNTER_DEFINITION FirstCounter( PPERF_OBJECT_TYPE PerfObj )
{
return( (PPERF_COUNTER_DEFINITION) ((PBYTE)PerfObj +
PerfObj->HeaderLength) );
}

PPERF_COUNTER_DEFINITION NextCounter(
PPERF_COUNTER_DEFINITION PerfCntr )
{
return( (PPERF_COUNTER_DEFINITION)((PBYTE)PerfCntr +
PerfCntr->ByteLength) );
}

/*****************************************************************
* *
* Load the counter and object names from the registry to the *
* global variable lpNamesArray. *
* *
*****************************************************************/

void GetNameStrings( )
{
HKEY hKeyPerflib; // handle to registry key
HKEY hKeyPerflib009; // handle to registry key
DWORD dwMaxValueLen; // maximum size of key values
DWORD dwBuffer; // bytes to allocate for buffers
DWORD dwBufferSize; // size of dwBuffer
LPSTR lpCurrentString; // pointer for enumerating data strings
DWORD dwCounter; // current counter index

// Get the number of Counter items.

RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib",
0,
KEY_READ,
&hKeyPerflib);

dwBufferSize = sizeof(dwBuffer);

RegQueryValueEx( hKeyPerflib,
"Last Counter",
NULL,
NULL,
(LPBYTE) &dwBuffer,
&dwBufferSize );

RegCloseKey( hKeyPerflib );

// Allocate memory for the names array.

lpNamesArray = malloc( (dwBuffer+1) * sizeof(LPSTR) );

// Open the key containing the counter and object names.

RegOpenKeyEx( HKEY_LOCAL_MACHINE,
"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Perflib\\009",
0,
KEY_READ,
&hKeyPerflib009);

// Get the size of the largest value in the key (Counter or Help).

RegQueryInfoKey( hKeyPerflib009,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
&dwMaxValueLen,
NULL,
NULL);

// Allocate memory for the counter and object names.

dwBuffer = dwMaxValueLen + 1;

lpNameStrings = malloc( dwBuffer * sizeof(CHAR) );

// Read the Counter value.

RegQueryValueEx( hKeyPerflib009,
"Counter",
NULL,
NULL,
lpNameStrings, &dwBuffer );

// Load names into an array, by index.

for( lpCurrentString = lpNameStrings; *lpCurrentString;
lpCurrentString += (lstrlen(lpCurrentString)+1) )
{
dwCounter = atol( lpCurrentString );

lpCurrentString += (lstrlen(lpCurrentString)+1);

lpNamesArray[dwCounter] = (LPSTR) lpCurrentString;
}
}

16,467

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧