RegEnumKeyExW not return all name sub-keys












-1














I want to enumerate name of key. but. is there something wrong here. it don't return all name. i checks subKeys it return 12 total Keys. when loop in RegEnumKeyExW it only return 3 name not 12 name. it only return 1,2 and 5 name key



in key HKEY_CURRENT_USER. there're 12 Keys.




  1. AppEvents


  2. Console


  3. Control Panel


  4. Environment


  5. EUDC


  6. Identities


  7. Keyboard Layout


  8. Network


  9. Printers


  10. Software


  11. System


  12. Volatile Environment



This the code



HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, NULL, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD subKeys;
FILETIME ftLastWriteTime;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, NULL, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
wchar_t keyName[MAX_KEY_LENGTH];
DWORD cbName;
for (DWORD i = 0; i < subKeys; i++)
{
if (RegEnumKeyExW(hKey, i, keyName, &cbName, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
std::wcout << keyName << std::endl;
}
else
{
std::cout << GetLastError() << std::endl;
}
}
}
else
{
std::cout << GetLastError() << std::endl;
}
}
else
{
std::cout << GetLastError() << std::endl;
}









share|improve this question






















  • Try to open key with fewer rights, RegEnumKeyExW only requires KEY_ENUMERATE_SUB_KEYS. You don't need to call RegQueryInfoKeyW first, just loop until RegEnumKeyExW fails.
    – zett42
    Nov 23 '18 at 7:51






  • 3




    at first Reg api not set GetLastError() but return error code. at second - your error in not initialize cbName in call RegEnumKeyExW. this is in-out parameter and before call must specifies the size of the buffer specified by the lpName parameter, in characters. so must be &(cbName = RTL_NUMBER_OF(keyName)) in your code
    – RbMm
    Nov 23 '18 at 7:52








  • 1




    When an API function does not behave as expected you must re-read the documentation. One of the things you will there is how the error handling is performed for this function, and indeed all the registry api functions.
    – David Heffernan
    Nov 23 '18 at 8:33






  • 1




    Fix: DWORD cbName = MAX_KEY_LENGTH; Right now it works by accident for the first subkey and can only work again when the name is shorter. So indeed 1, 2 and 5.
    – Hans Passant
    Nov 23 '18 at 9:52
















-1














I want to enumerate name of key. but. is there something wrong here. it don't return all name. i checks subKeys it return 12 total Keys. when loop in RegEnumKeyExW it only return 3 name not 12 name. it only return 1,2 and 5 name key



in key HKEY_CURRENT_USER. there're 12 Keys.




  1. AppEvents


  2. Console


  3. Control Panel


  4. Environment


  5. EUDC


  6. Identities


  7. Keyboard Layout


  8. Network


  9. Printers


  10. Software


  11. System


  12. Volatile Environment



This the code



HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, NULL, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD subKeys;
FILETIME ftLastWriteTime;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, NULL, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
wchar_t keyName[MAX_KEY_LENGTH];
DWORD cbName;
for (DWORD i = 0; i < subKeys; i++)
{
if (RegEnumKeyExW(hKey, i, keyName, &cbName, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
std::wcout << keyName << std::endl;
}
else
{
std::cout << GetLastError() << std::endl;
}
}
}
else
{
std::cout << GetLastError() << std::endl;
}
}
else
{
std::cout << GetLastError() << std::endl;
}









share|improve this question






















  • Try to open key with fewer rights, RegEnumKeyExW only requires KEY_ENUMERATE_SUB_KEYS. You don't need to call RegQueryInfoKeyW first, just loop until RegEnumKeyExW fails.
    – zett42
    Nov 23 '18 at 7:51






  • 3




    at first Reg api not set GetLastError() but return error code. at second - your error in not initialize cbName in call RegEnumKeyExW. this is in-out parameter and before call must specifies the size of the buffer specified by the lpName parameter, in characters. so must be &(cbName = RTL_NUMBER_OF(keyName)) in your code
    – RbMm
    Nov 23 '18 at 7:52








  • 1




    When an API function does not behave as expected you must re-read the documentation. One of the things you will there is how the error handling is performed for this function, and indeed all the registry api functions.
    – David Heffernan
    Nov 23 '18 at 8:33






  • 1




    Fix: DWORD cbName = MAX_KEY_LENGTH; Right now it works by accident for the first subkey and can only work again when the name is shorter. So indeed 1, 2 and 5.
    – Hans Passant
    Nov 23 '18 at 9:52














-1












-1








-1







I want to enumerate name of key. but. is there something wrong here. it don't return all name. i checks subKeys it return 12 total Keys. when loop in RegEnumKeyExW it only return 3 name not 12 name. it only return 1,2 and 5 name key



in key HKEY_CURRENT_USER. there're 12 Keys.




  1. AppEvents


  2. Console


  3. Control Panel


  4. Environment


  5. EUDC


  6. Identities


  7. Keyboard Layout


  8. Network


  9. Printers


  10. Software


  11. System


  12. Volatile Environment



This the code



HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, NULL, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD subKeys;
FILETIME ftLastWriteTime;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, NULL, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
wchar_t keyName[MAX_KEY_LENGTH];
DWORD cbName;
for (DWORD i = 0; i < subKeys; i++)
{
if (RegEnumKeyExW(hKey, i, keyName, &cbName, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
std::wcout << keyName << std::endl;
}
else
{
std::cout << GetLastError() << std::endl;
}
}
}
else
{
std::cout << GetLastError() << std::endl;
}
}
else
{
std::cout << GetLastError() << std::endl;
}









share|improve this question













I want to enumerate name of key. but. is there something wrong here. it don't return all name. i checks subKeys it return 12 total Keys. when loop in RegEnumKeyExW it only return 3 name not 12 name. it only return 1,2 and 5 name key



in key HKEY_CURRENT_USER. there're 12 Keys.




  1. AppEvents


  2. Console


  3. Control Panel


  4. Environment


  5. EUDC


  6. Identities


  7. Keyboard Layout


  8. Network


  9. Printers


  10. Software


  11. System


  12. Volatile Environment



This the code



HKEY hKey;
if (RegOpenKeyExW(HKEY_CURRENT_USER, NULL, 0, KEY_READ, &hKey) == ERROR_SUCCESS)
{
DWORD subKeys;
FILETIME ftLastWriteTime;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, NULL, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
wchar_t keyName[MAX_KEY_LENGTH];
DWORD cbName;
for (DWORD i = 0; i < subKeys; i++)
{
if (RegEnumKeyExW(hKey, i, keyName, &cbName, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)
{
std::wcout << keyName << std::endl;
}
else
{
std::cout << GetLastError() << std::endl;
}
}
}
else
{
std::cout << GetLastError() << std::endl;
}
}
else
{
std::cout << GetLastError() << std::endl;
}






c++ windows visual-studio winapi






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 7:35









mursh damir

1




1












  • Try to open key with fewer rights, RegEnumKeyExW only requires KEY_ENUMERATE_SUB_KEYS. You don't need to call RegQueryInfoKeyW first, just loop until RegEnumKeyExW fails.
    – zett42
    Nov 23 '18 at 7:51






  • 3




    at first Reg api not set GetLastError() but return error code. at second - your error in not initialize cbName in call RegEnumKeyExW. this is in-out parameter and before call must specifies the size of the buffer specified by the lpName parameter, in characters. so must be &(cbName = RTL_NUMBER_OF(keyName)) in your code
    – RbMm
    Nov 23 '18 at 7:52








  • 1




    When an API function does not behave as expected you must re-read the documentation. One of the things you will there is how the error handling is performed for this function, and indeed all the registry api functions.
    – David Heffernan
    Nov 23 '18 at 8:33






  • 1




    Fix: DWORD cbName = MAX_KEY_LENGTH; Right now it works by accident for the first subkey and can only work again when the name is shorter. So indeed 1, 2 and 5.
    – Hans Passant
    Nov 23 '18 at 9:52


















  • Try to open key with fewer rights, RegEnumKeyExW only requires KEY_ENUMERATE_SUB_KEYS. You don't need to call RegQueryInfoKeyW first, just loop until RegEnumKeyExW fails.
    – zett42
    Nov 23 '18 at 7:51






  • 3




    at first Reg api not set GetLastError() but return error code. at second - your error in not initialize cbName in call RegEnumKeyExW. this is in-out parameter and before call must specifies the size of the buffer specified by the lpName parameter, in characters. so must be &(cbName = RTL_NUMBER_OF(keyName)) in your code
    – RbMm
    Nov 23 '18 at 7:52








  • 1




    When an API function does not behave as expected you must re-read the documentation. One of the things you will there is how the error handling is performed for this function, and indeed all the registry api functions.
    – David Heffernan
    Nov 23 '18 at 8:33






  • 1




    Fix: DWORD cbName = MAX_KEY_LENGTH; Right now it works by accident for the first subkey and can only work again when the name is shorter. So indeed 1, 2 and 5.
    – Hans Passant
    Nov 23 '18 at 9:52
















Try to open key with fewer rights, RegEnumKeyExW only requires KEY_ENUMERATE_SUB_KEYS. You don't need to call RegQueryInfoKeyW first, just loop until RegEnumKeyExW fails.
– zett42
Nov 23 '18 at 7:51




Try to open key with fewer rights, RegEnumKeyExW only requires KEY_ENUMERATE_SUB_KEYS. You don't need to call RegQueryInfoKeyW first, just loop until RegEnumKeyExW fails.
– zett42
Nov 23 '18 at 7:51




3




3




at first Reg api not set GetLastError() but return error code. at second - your error in not initialize cbName in call RegEnumKeyExW. this is in-out parameter and before call must specifies the size of the buffer specified by the lpName parameter, in characters. so must be &(cbName = RTL_NUMBER_OF(keyName)) in your code
– RbMm
Nov 23 '18 at 7:52






at first Reg api not set GetLastError() but return error code. at second - your error in not initialize cbName in call RegEnumKeyExW. this is in-out parameter and before call must specifies the size of the buffer specified by the lpName parameter, in characters. so must be &(cbName = RTL_NUMBER_OF(keyName)) in your code
– RbMm
Nov 23 '18 at 7:52






1




1




When an API function does not behave as expected you must re-read the documentation. One of the things you will there is how the error handling is performed for this function, and indeed all the registry api functions.
– David Heffernan
Nov 23 '18 at 8:33




When an API function does not behave as expected you must re-read the documentation. One of the things you will there is how the error handling is performed for this function, and indeed all the registry api functions.
– David Heffernan
Nov 23 '18 at 8:33




1




1




Fix: DWORD cbName = MAX_KEY_LENGTH; Right now it works by accident for the first subkey and can only work again when the name is shorter. So indeed 1, 2 and 5.
– Hans Passant
Nov 23 '18 at 9:52




Fix: DWORD cbName = MAX_KEY_LENGTH; Right now it works by accident for the first subkey and can only work again when the name is shorter. So indeed 1, 2 and 5.
– Hans Passant
Nov 23 '18 at 9:52












1 Answer
1






active

oldest

votes


















0














As @Hans Passant says, You should reset the length of cbName, But actually not the MAX_KEY_LENGTH. If the buff length is not enough for the key name, RegEnumKeyExW will get failure. Add parameter in RegQueryInfoKeyW above to get the max length of the subkey name:



DWORD cbMaxSubKeyLen;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, &cbMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)


Then in the for loop, reset the cbName = cbMaxSubKeyLen;






share|improve this answer





















  • It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
    – Hans Passant
    Dec 2 '18 at 9:40











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442422%2fregenumkeyexw-not-return-all-name-sub-keys%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














As @Hans Passant says, You should reset the length of cbName, But actually not the MAX_KEY_LENGTH. If the buff length is not enough for the key name, RegEnumKeyExW will get failure. Add parameter in RegQueryInfoKeyW above to get the max length of the subkey name:



DWORD cbMaxSubKeyLen;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, &cbMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)


Then in the for loop, reset the cbName = cbMaxSubKeyLen;






share|improve this answer





















  • It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
    – Hans Passant
    Dec 2 '18 at 9:40
















0














As @Hans Passant says, You should reset the length of cbName, But actually not the MAX_KEY_LENGTH. If the buff length is not enough for the key name, RegEnumKeyExW will get failure. Add parameter in RegQueryInfoKeyW above to get the max length of the subkey name:



DWORD cbMaxSubKeyLen;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, &cbMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)


Then in the for loop, reset the cbName = cbMaxSubKeyLen;






share|improve this answer





















  • It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
    – Hans Passant
    Dec 2 '18 at 9:40














0












0








0






As @Hans Passant says, You should reset the length of cbName, But actually not the MAX_KEY_LENGTH. If the buff length is not enough for the key name, RegEnumKeyExW will get failure. Add parameter in RegQueryInfoKeyW above to get the max length of the subkey name:



DWORD cbMaxSubKeyLen;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, &cbMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)


Then in the for loop, reset the cbName = cbMaxSubKeyLen;






share|improve this answer












As @Hans Passant says, You should reset the length of cbName, But actually not the MAX_KEY_LENGTH. If the buff length is not enough for the key name, RegEnumKeyExW will get failure. Add parameter in RegQueryInfoKeyW above to get the max length of the subkey name:



DWORD cbMaxSubKeyLen;
if (RegQueryInfoKeyW(hKey, NULL, NULL, NULL, &subKeys, &cbMaxSubKeyLen, NULL, NULL, NULL, NULL, NULL, &ftLastWriteTime) == ERROR_SUCCESS)


Then in the for loop, reset the cbName = cbMaxSubKeyLen;







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 26 '18 at 3:41









Drake Wu

1274




1274












  • It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
    – Hans Passant
    Dec 2 '18 at 9:40


















  • It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
    – Hans Passant
    Dec 2 '18 at 9:40
















It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
– Hans Passant
Dec 2 '18 at 9:40




It is okay to query for the size. But then it is also very, very, very important to reallocate keyName so it is big enough. Buffer overflow is quite a nasty bug.
– Hans Passant
Dec 2 '18 at 9:40


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53442422%2fregenumkeyexw-not-return-all-name-sub-keys%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

What visual should I use to simply compare current year value vs last year in Power BI desktop

How to ignore python UserWarning in pytest?

Alexandru Averescu