在VC中折腾注册表
碰到的问题:用遍历注册表函数RegEnumValue所得出的字符串老是被截断。这是刚开始所用的代码:
DWORD retCode;
HKEY regHKey;
CHAR achKey[MAX_PATH];
DWORD ccValue=MAX_PATH;
if(::RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\OKI\\CCons33\\UNITTYPE",0,KEY_READ,®HKey)==ERROR_SUCCESS)
{
for(i=0,retCode=ERROR_SUCCESS;retCode!=ERROR_NO_MORE_ITEMS;i++)
{
retCode = ::RegEnumValue(regHKey, i,achKey,&ccValue, NULL,NULL,NULL, NULL);
for(int j=0;j<strlen(achKey);j++)
cout<<achKey[j];
cout<<endl;
}
}
HKEY regHKey;
CHAR achKey[MAX_PATH];
DWORD ccValue=MAX_PATH;
if(::RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\OKI\\CCons33\\UNITTYPE",0,KEY_READ,®HKey)==ERROR_SUCCESS)
{
for(i=0,retCode=ERROR_SUCCESS;retCode!=ERROR_NO_MORE_ITEMS;i++)
{
retCode = ::RegEnumValue(regHKey, i,achKey,&ccValue, NULL,NULL,NULL, NULL);
for(int j=0;j<strlen(achKey);j++)
cout<<achKey[j];
cout<<endl;
}
}
例如“MediaPathUnexpanded”的项名,它只返回其中前面部分,就好像“MediaPathUn”,后面的就不见了,我百思不知其所,
解决方法:
正如http://topic.csdn.net/t/20040701/00/3136042.html这篇文章所说:
通过RegQueryInfoKey可以获得value name的最大长度的(参数lpcMaxValueNameLen),RegEnumValue中lpValueName开辟的空间比lpcMaxValueNameLen大就行了
以下是解决的代码:
1LPCSTR lp="Software\\OKI\\CCons33\\UNITTYPE";
2 HKEY regHKey;
3 if(::RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\OKI\\CCons33\\UNITTYPE",0,KEY_READ,®HKey)==ERROR_SUCCESS)
4 {
5 /* ::RegQueryValueEx(regHKey,"GateKeeper#1",NULL,&nType,lpData,&size);
6 CString str;
7 str.Format("%s",(LPCSTR)lpData);
8 for(int i=0;i<str.GetLength();i++)
9 cout<<str[i];
10 */
11
12 //int i;
13 DWORD dwIndex = 0;
14 TCHAR *tcKeyName = NULL;
15 DWORD dwKeyType;
16 DWORD dwKeyDataLength,dwKeyNameLen;
17 LPBYTE pbbinKeyData = NULL;
18 LONG lResult;
19 //LPDWORD max_Path;
20 DWORD ccValue=MAX_PATH;
21 DWORD lNoOfValues = 0;
22 DWORD lLongestKeyNameLen = 1;
23 DWORD lLongestDataLen = 1;
24 lResult = RegQueryInfoKey(regHKey,NULL,NULL,NULL,NULL,NULL,NULL,&lNoOfValues,
25 &lLongestKeyNameLen,&lLongestDataLen,NULL,NULL);
26
27 lLongestKeyNameLen++;
28 lLongestDataLen++;
29 tcKeyName = new TCHAR[lLongestKeyNameLen];
30 pbbinKeyData = new BYTE[lLongestDataLen];
31 tcKeyName = new TCHAR[lLongestKeyNameLen];
32 pbbinKeyData =new BYTE[lLongestDataLen];
33 CString cstrFinalData,cstrTemp;
34 while(TRUE)
35 {
36 memset(pbbinKeyData,0,lLongestDataLen);
37 memset(tcKeyName,0,lLongestKeyNameLen);
38 dwKeyType = dwKeyDataLength = dwKeyNameLen=0;
39 dwKeyNameLen = lLongestKeyNameLen;
40 dwKeyDataLength=lLongestDataLen;
41 lResult=RegEnumValue(regHKey,dwIndex,tcKeyName,&dwKeyNameLen,NULL,&dwKeyType,pbbinKeyData,&dwKeyDataLength);
42 if(lResult == ERROR_NO_MORE_ITEMS)
43 break;
44 cout<<tcKeyName<<endl;
45 //dRegistryItem(tcKeyName,dwKeyType,pbbinKeyData,dwKeyDataLength,dwIndex);//?帵悢悩槹ListView
46 dwIndex++;
47 }
48 RegCloseKey(regHKey);
49 delete tcKeyName;
50 delete pbbinKeyData;
2 HKEY regHKey;
3 if(::RegOpenKeyEx(HKEY_CURRENT_USER,"Software\\OKI\\CCons33\\UNITTYPE",0,KEY_READ,®HKey)==ERROR_SUCCESS)
4 {
5 /* ::RegQueryValueEx(regHKey,"GateKeeper#1",NULL,&nType,lpData,&size);
6 CString str;
7 str.Format("%s",(LPCSTR)lpData);
8 for(int i=0;i<str.GetLength();i++)
9 cout<<str[i];
10 */
11
12 //int i;
13 DWORD dwIndex = 0;
14 TCHAR *tcKeyName = NULL;
15 DWORD dwKeyType;
16 DWORD dwKeyDataLength,dwKeyNameLen;
17 LPBYTE pbbinKeyData = NULL;
18 LONG lResult;
19 //LPDWORD max_Path;
20 DWORD ccValue=MAX_PATH;
21 DWORD lNoOfValues = 0;
22 DWORD lLongestKeyNameLen = 1;
23 DWORD lLongestDataLen = 1;
24 lResult = RegQueryInfoKey(regHKey,NULL,NULL,NULL,NULL,NULL,NULL,&lNoOfValues,
25 &lLongestKeyNameLen,&lLongestDataLen,NULL,NULL);
26
27 lLongestKeyNameLen++;
28 lLongestDataLen++;
29 tcKeyName = new TCHAR[lLongestKeyNameLen];
30 pbbinKeyData = new BYTE[lLongestDataLen];
31 tcKeyName = new TCHAR[lLongestKeyNameLen];
32 pbbinKeyData =new BYTE[lLongestDataLen];
33 CString cstrFinalData,cstrTemp;
34 while(TRUE)
35 {
36 memset(pbbinKeyData,0,lLongestDataLen);
37 memset(tcKeyName,0,lLongestKeyNameLen);
38 dwKeyType = dwKeyDataLength = dwKeyNameLen=0;
39 dwKeyNameLen = lLongestKeyNameLen;
40 dwKeyDataLength=lLongestDataLen;
41 lResult=RegEnumValue(regHKey,dwIndex,tcKeyName,&dwKeyNameLen,NULL,&dwKeyType,pbbinKeyData,&dwKeyDataLength);
42 if(lResult == ERROR_NO_MORE_ITEMS)
43 break;
44 cout<<tcKeyName<<endl;
45 //dRegistryItem(tcKeyName,dwKeyType,pbbinKeyData,dwKeyDataLength,dwIndex);//?帵悢悩槹ListView
46 dwIndex++;
47 }
48 RegCloseKey(regHKey);
49 delete tcKeyName;
50 delete pbbinKeyData;