RegEnumKeyExW not return all name sub-keys
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.
AppEvents
Console
Control Panel
Environment
EUDC
Identities
Keyboard Layout
Network
Printers
Software
System
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
add a comment |
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.
AppEvents
Console
Control Panel
Environment
EUDC
Identities
Keyboard Layout
Network
Printers
Software
System
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
Try to open key with fewer rights,RegEnumKeyExW
only requiresKEY_ENUMERATE_SUB_KEYS
. You don't need to callRegQueryInfoKeyW
first, just loop untilRegEnumKeyExW
fails.
– zett42
Nov 23 '18 at 7:51
3
at first Reg api not setGetLastError()
but return error code. at second - your error in not initializecbName
in callRegEnumKeyExW
. this is in-out parameter and before call must specifies the size of the buffer specified by thelpName
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
add a comment |
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.
AppEvents
Console
Control Panel
Environment
EUDC
Identities
Keyboard Layout
Network
Printers
Software
System
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
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.
AppEvents
Console
Control Panel
Environment
EUDC
Identities
Keyboard Layout
Network
Printers
Software
System
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
c++ windows visual-studio winapi
asked Nov 23 '18 at 7:35
mursh damirmursh damir
1
1
Try to open key with fewer rights,RegEnumKeyExW
only requiresKEY_ENUMERATE_SUB_KEYS
. You don't need to callRegQueryInfoKeyW
first, just loop untilRegEnumKeyExW
fails.
– zett42
Nov 23 '18 at 7:51
3
at first Reg api not setGetLastError()
but return error code. at second - your error in not initializecbName
in callRegEnumKeyExW
. this is in-out parameter and before call must specifies the size of the buffer specified by thelpName
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
add a comment |
Try to open key with fewer rights,RegEnumKeyExW
only requiresKEY_ENUMERATE_SUB_KEYS
. You don't need to callRegQueryInfoKeyW
first, just loop untilRegEnumKeyExW
fails.
– zett42
Nov 23 '18 at 7:51
3
at first Reg api not setGetLastError()
but return error code. at second - your error in not initializecbName
in callRegEnumKeyExW
. this is in-out parameter and before call must specifies the size of the buffer specified by thelpName
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
add a comment |
1 Answer
1
active
oldest
votes
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;
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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;
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
add a comment |
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;
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
add a comment |
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;
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;
answered Nov 26 '18 at 3:41
Drake Wu - MSFTDrake Wu - MSFT
2425
2425
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
Try to open key with fewer rights,
RegEnumKeyExW
only requiresKEY_ENUMERATE_SUB_KEYS
. You don't need to callRegQueryInfoKeyW
first, just loop untilRegEnumKeyExW
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 initializecbName
in callRegEnumKeyExW
. this is in-out parameter and before call must specifies the size of the buffer specified by thelpName
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