특정 키가 지정된 모든 하위 키와 값을 다시 제공하는 Windows 응용 프로그램을 작성하려고합니다. 주어진 키 내에 하위 키를 제공하는 것으로 보이는 코드를 작성했지만 값을 올바르게 열거하지는 않습니다. 값없이 하위 키를 성공적으로 열거하고 결과를 탭으로 된 트리 배열의 종류로 반환합니다. 그러나 값을 열거하면 프로그램은 각 값 (매번 동일한 임의의 값)에 대해 임의 값을 반환하고 디버그 오류로 인해 이후에 충돌합니다. 그래서Windows 레지스트리 키의 모든 하위 키와 값 열거
(1) KEY
(1) SUBKEY
(1) SUBKEYWITHINSUBKEY
Code: value1data
Code: value2data
Code: value3data
(2) SUBKEY
(1) SUBKEYWITHINSUBKEY
(3) SUBKEY
... 그리고 :
그것은 의도 출력은 기본적으로.
내가 대신받을 출력은 뭔가 같은 : (다음 ... 그리고 충돌)
(1) KEY
(1) SUBKEY
(1) SUBKEYWITHINSUBKEY
Code: someValue
Code: someValue
Code: someValue
이 다음과 같은 오류와 함께 다음에 :
! "디버그 오류"런타임 확인 실패 # 2 - 변수 'valNameLen'주위의 스택이 손상되었습니다. "
코드가 현재 좀 지저분합니다 (저는 Windows API 초보자입니다).하지만 누군가 내가 잘못하고있는 것을 보여줄 수 있다면, 어쨌든 그들은 내 코딩 스타일을 비판하고 적합하다고 생각하면 좋을 것입니다.
감사!
는-R
/*
Windows Registry Subkey Enumeration Example
Based on example found at code-blue.org
*/
#include <windows.h>
#include <stdio.h>
void EnumerateValues(HKEY hKey, DWORD numValues)
{
DWORD dwIndex = 0;
LPSTR valueName = new CHAR[64];
DWORD valNameLen;
DWORD dataType;
DWORD data;
DWORD dataSize;
for (int i = 0; i < numValues; i++)
{
RegEnumValue(hKey,
dwIndex,
valueName,
&valNameLen,
NULL,
&dataType,
(BYTE*)&data,
&dataSize);
dwIndex++;
printf("Code: 0x%08X\n", data);
}
}
void EnumerateSubKeys(HKEY RootKey, char* subKey, unsigned int tabs = 0)
{
HKEY hKey;
DWORD cSubKeys; //Used to store the number of Subkeys
DWORD maxSubkeyLen; //Longest Subkey name length
DWORD cValues; //Used to store the number of Subkeys
DWORD maxValueLen; //Longest Subkey name length
DWORD retCode; //Return values of calls
RegOpenKeyEx(RootKey, subKey, 0, KEY_ALL_ACCESS, &hKey);
RegQueryInfoKey(hKey, // key handle
NULL, // buffer for class name
NULL, // size of class string
NULL, // reserved
&cSubKeys, // number of subkeys
&maxSubkeyLen, // longest subkey length
NULL, // longest class string
&cValues, // number of values for this key
&maxValueLen, // longest value name
NULL, // longest value data
NULL, // security descriptor
NULL); // last write time
if(cSubKeys>0)
{
char currentSubkey[MAX_PATH];
for(int i=0;i < cSubKeys;i++){
DWORD currentSubLen=MAX_PATH;
retCode=RegEnumKeyEx(hKey, // Handle to an open/predefined key
i, // Index of the subkey to retrieve.
currentSubkey, // buffer to receives the name of the subkey
¤tSubLen, // size of that buffer
NULL, // Reserved
NULL, // buffer for class string
NULL, // size of that buffer
NULL); // last write time
if(retCode==ERROR_SUCCESS)
{
for (int i = 0; i < tabs; i++)
printf("\t");
printf("(%d) %s\n", i+1, currentSubkey);
char* subKeyPath = new char[currentSubLen + strlen(subKey)];
sprintf(subKeyPath, "%s\\%s", subKey, currentSubkey);
EnumerateSubKeys(RootKey, subKeyPath, (tabs + 1));
}
}
}
else
{
EnumerateValues(hKey, cValues);
}
RegCloseKey(hKey);
}
int main()
{
EnumerateSubKeys(HKEY_CURRENT_USER,"SOFTWARE\\MyKeyToSearchIn");
return 0;
}
코드를 처음부터 다시 작성하고 필요한 모든 작업을 수행합니다. 알고리즘은 괜찮을 것입니다. 유틸리티의 전체적인 요점은이 정보와 다른 것들을 얻는 것입니다. 그리고 검색하는 키는 특정하고 많은 서브 키나 값을 포함하지 않습니다. 포인터 주셔서 감사합니다! – 8bitcartridge