看看API帮助。
LONG RegQueryValue(......)
hKey
Identifies a currently open key or any of the following predefined reserved
handle values:
HKEY_CLASSES_ROOT
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
预置键值列表没有HKEY_DYN_DATA,所以看来只能先用RegOpenKey打开它,再读取。
------------------------------------------------------------------------
干脆我把全部代码都贴给你吧。
bool __fastcall GetStat()
{
DWORD Ret;
HKEY TempKey=0;
if(FOSVersion==OS_W9X) // Win95/Win98
{
Ret=RegOpenKeyEx(HKEY_DYN_DATA, Reg_PerfStatData.c_str(),0, KEY_READ, &TempKey);
if(Ret!=ERROR_SUCCESS)
{
RegCloseKey(TempKey);
FLastStatus=SysErrorMessage(Ret);
return false;
}
bool Result=GetData(TempKey,FKeyDUNXmitStr, &FBytesXmitTot) ;
if(Result) Result=GetData(TempKey,FKeyDUNRecvStr, &FBytesRecvTot) ;
if(Result)
{
FBytesXmitCur=FBytesXmitTot - FBytesXmitCon ;
FBytesRecvCur=FBytesRecvTot - FBytesRecvCon ;
}
else return false;
}
else if(FOSVersion==OS_NT4) // Windows NT4.0
{
// Get Xmit and Recv bytes by reading registry.
PERF_DATA_BLOCK* DataBlock=NULL;
PERF_OBJECT_TYPE* ObjType;
PERF_COUNTER_DEFINITION* CounterDef,CurCounterDef;
PERF_INSTANCE_DEFINITION* InstDef;
PERF_COUNTER_BLOCK* CounterBlock;
PERF_COUNTER_DEFINITION **CounterDefArray=NULL;
PBYTE ObjOffset,CounterDefOffset,CounterBlockOffset;
unsigned long* DataValue;
DWORD BufSize=TOTALBYTES;
DataBlock=(PERF_DATA_BLOCK*)malloc(BufSize);
while(RegQueryValueEx(HKEY_PERFORMANCE_DATA,PData_Ras_Total.c_str(),
NULL,NULL,(PBYTE)DataBlock,&BufSize)==ERROR_MORE_DATA)
{
BufSize+=BYTEINCREMENT;
free(DataBlock);
DataBlock=(PERF_DATA_BLOCK*)malloc(BufSize);
}
if(DataBlock->NumObjectTypes==0) return false;
// Process all objects
ObjOffset=(PBYTE)DataBlock + DataBlock->HeaderLength;
for(DWORD NumObj=0; NumObj < DataBlock->NumObjectTypes ; NumObj++)
{
Application->ProcessMessages();
ObjType=(PERF_OBJECT_TYPE*)ObjOffset;
CounterDefOffset=ObjOffset+ObjType->HeaderLength;
if(ObjType->NumCounters > 0)
{
CounterDefArray=new PERF_COUNTER_DEFINITION*[ObjType->NumCounters];
for(DWORD NumCounter=0; NumCounter < ObjType->NumCounters; NumCounter++)
{
Application->ProcessMessages();
CounterDefArray[NumCounter]=(PERF_COUNTER_DEFINITION*)CounterDefOffset;
CounterDefOffset+=CounterDefArray[NumCounter]->ByteLength;
}
bool LoopFlag=true;
int NumInst=1;
while(LoopFlag)
{
if(ObjType->NumInstances > 0)
{
InstDef=(PERF_INSTANCE_DEFINITION*)CounterDefOffset;
CounterDefOffset+=InstDef->ByteLength;
}
CounterBlockOffset=CounterDefOffset;
CounterBlock=(PERF_COUNTER_BLOCK*)CounterBlockOffset;
for(DWORD NumCounter=0; NumCounter < ObjType->NumCounters; NumCounter++)
{
if(CounterDefArray[NumCounter]->CounterNameTitleIndex==PData_Bytes_Xmit)
{
DataValue=(unsigned long*)(CounterBlockOffset+CounterDefArray[NumCounter]->CounterOffset);
if(*DataValue>FBytesXmitCur) FBytesXmitCur=*DataValue;
}
if(CounterDefArray[NumCounter]->CounterNameTitleIndex==PData_Bytes_Recv)
{
DataValue=(unsigned long*)(CounterBlockOffset+CounterDefArray[NumCounter]->CounterOffset);
if(*DataValue>FBytesRecvCur) FBytesRecvCur=*DataValue;
}
}
CounterDefOffset+=CounterBlock->ByteLength;
// Check for more instances of these counters
if(ObjType->NumInstances>0)
{
NumInst++;
if(NumInst>ObjType->NumInstances) LoopFlag=false;
}
else LoopFlag=false;
}
delete[] CounterDefArray;
}
// Process next object type
ObjOffset=ObjOffset+ObjType->TotalByteLength;
}
free(DataBlock);
}
else if(FOSVersion==OS_W2K) // Windows 2000( NT5.0 )
{
if(FCurRasConn==NULL) goto ERROR_RES;
if(RasGetConnectionStatisticsProc==NULL) goto ERROR_RES;
// Get current connections statistics data
DWORD Ret;
RAS_STATS RasStats;
RasStats.dwSize=sizeof(RAS_STATS);
Ret=RasGetConnectionStatisticsProc(FCurRasConn,&RasStats);
if(Ret!=0)
{
char ErrBuf[256];
RasGetErrorString(Ret,ErrBuf,256);
FLastStatus=AnsiString(ErrBuf);
goto ERROR_RES;
}
FBytesXmitCur=RasStats.dwBytesXmited;
FBytesRecvCur=RasStats.dwBytesRcved;
//FConnectSpeed=RasStats.dwBps*1000; // Old unit is Kbps, convert it to bps.
return true;
ERROR_RES:
FBytesXmitCur=MAXINT;
FBytesRecvCur=MAXINT;
return false;
}
return true;
}
//---------------------------------------------------------------------------