获得注册表某一主键下的所有串值 HD 用API函数: RegOpenKey RegEnumKey RegEnumValue RegQueryValue RegCloseKey 以下是我以前写的(VB用DLL)读键名和数据名函数: EXTERN_C LONG WINAPI GetRegKeyNames( HKEY hRootKey, LPCTSTR lpSubKey, LPLPSAFEARRAY KeyNames ) { LONG lRtn = -1; HKEY hKey; DWORD dwSubKeys, dwMaxSubKeyLen; DWORD dwLoop, dwSize; LPSTR lpstrTmp; BSTR bstrKeyName; SAFEARRAYBOUND arrayBound; if ( RegOpenKey(hRootKey, lpSubKey, &hKey) == ERROR_SUCCESS ) { if ( RegQueryInfoKey(hKey, NULL, NULL, NULL, &dwSubKeys, &dwMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ) { if ( dwSubKeys > 0 && dwMaxSubKeyLen > 0 ) { lRtn = dwSubKeys; lpstrTmp = AllocMem(dwMaxSubKeyLen + 2); if ( lpstrTmp ) { arrayBound.lLbound = 0; arrayBound.cElements = dwSubKeys; if ( RedimArray( KeyNames, 1, sizeof(BSTR), &arrayBound) == ERROR_SUCCESS ) { (*KeyNames)->fFeatures = FADF_BSTR; for ( dwLoop = 0; dwLoop < dwSubKeys; dwLoop++) { dwSize = dwMaxSubKeyLen+1; if ( RegEnumKeyEx(hKey, dwLoop, lpstrTmp, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ) { bstrKeyName = SysAllocStringByteLen(lpstrTmp, dwSize); (*KeyNames)->fFeatures ^= FADF_BSTR; SafeArrayPutElement(*KeyNames, &dwLoop, &bstrKeyName); (*KeyNames)->fFeatures |= FADF_BSTR; } else { break; } } if ( dwLoop < dwSubKeys ) { SafeArrayDestroy(*KeyNames); *KeyNames = NULL; lRtn = -1; } } FreeMem(lpstrTmp); } } else lRtn = 0; } RegCloseKey(hKey); } return lRtn; } EXTERN_C LONG WINAPI GetRegValueNames( HKEY hRootKey, LPCTSTR lpSubKey, LPLPSAFEARRAY ValueNames ) { LONG lRtn = -1; HKEY hKey; DWORD dwValues, dwMaxValueLen; DWORD dwLoop, dwSize; LPSTR lpstrTmp; BSTR bstrValueName; SAFEARRAYBOUND arrayBound; if ( RegOpenKey(hRootKey, lpSubKey, &hKey) == ERROR_SUCCESS ) { if ( RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &dwValues, &dwMaxValueLen, NULL, NULL, NULL) == ERROR_SUCCESS ) { if ( dwValues > 0 && dwMaxValueLen > 0 ) { lRtn = dwValues; lpstrTmp = AllocMem(dwMaxValueLen + 2); if ( lpstrTmp ) { arrayBound.lLbound = 0; arrayBound.cElements = dwValues; if ( RedimArray( ValueNames, 1, sizeof(BSTR), &arrayBound) == ERROR_SUCCESS ) { (*ValueNames)->fFeatures = FADF_BSTR; for ( dwLoop = 0; dwLoop < dwValues; dwLoop++) { dwSize = dwMaxValueLen + 1; if ( RegEnumValue(hKey, dwLoop, lpstrTmp, &dwSize, NULL, NULL, NULL, NULL) == ERROR_SUCCESS ) { bstrValueName = SysAllocStringByteLen(lpstrTmp, dwSize); (*ValueNames)->fFeatures ^= FADF_BSTR; SafeArrayPutElement(*ValueNames, &dwLoop, &bstrValueName); (*ValueNames)->fFeatures |= FADF_BSTR; } else { break; } } if ( dwLoop < dwValues ) { SafeArrayDestroy(*ValueNames); *ValueNames = NULL; lRtn = -1; } } FreeMem(lpstrTmp); } } else lRtn = 0; } RegCloseKey(hKey); } return lRtn; } 忘了以下函数: // define AllocMem, FreeMem #ifndef AllocMem #define AllocMem(cb) LocalAlloc (LPTR, cb) #define FreeMem(pMem) if ( pMem ) {LocalFree((HANDLE)(pMem)); pMem = NULL;} #endif // #ifndef AllocMem EXTERN_C DWORD WINAPI RedimArray( LPLPSAFEARRAY lppArray, USHORT cDims, ULONG cbElemSize, LPSAFEARRAYBOUND lpBound ) { DWORD dwRet = ERROR_SUCCESS; LPSAFEARRAY lpTempArray; DWORD dwLop; if ( *lppArray ) { lpTempArray = *lppArray; if ( cDims == lpTempArray->cDims && cbElemSize == lpTempArray->cbElements ) { if ( SafeArrayRedim( lpTempArray, lpBound ) != S_OK ) dwRet = ERROR_NOT_ENOUGH_MEMORY; } else dwRet = ERROR_CALL_NOT_IMPLEMENTED ; } else { if ( SafeArrayAllocDescriptor(cDims, lppArray) == S_OK ) { lpTempArray = *lppArray; lpTempArray->fFeatures = 0; lpTempArray->cbElements = cbElemSize; for ( dwLop = 0; dwLop < cDims; dwLop++) { lpTempArray->rgsabound[dwLop] = lpBound[dwLop]; } if ( SafeArrayAllocData( lpTempArray ) != S_OK ) { SafeArrayDestroyDescriptor( *lppArray ); dwRet = ERROR_NOT_ENOUGH_MEMORY; } } else dwRet = ERROR_NOT_ENOUGH_MEMORY; } return dwRet; }