Creating an application similar to regedit
I want to create an application which does the same stuff as regedit, but simpler as i just want to enumerate the Keys in the form of a 开发者_运维技巧tree. I am trying to use RegOpenKeyEX to open a key and then RegEnumKeyex to enumerate it, but i keep getting the same key enumerated, even though i am incrementing the Index value.
Second question is, will recursion be helpful in this application.
Thanks in advance
Perhaps this code sample will help you understand what went wrong in your code: http://msdn.microsoft.com/en-us/library/ms724256(v=vs.85).aspx.
As for the second question. Recursion will probably help you in this application in order to build the Keys tree that you usually see in regedit on the left.
You probably don't want to use recursion for this. Recursion will traverse the three depth-first. What you usually want is an incremental breadth-first traversal.
You'll typically do that by creating your display tree with the standard top-level nodes (KHLM, HKCU, etc.) and use I_CHILDRENCALLBACK
to tell it that each of those has child nodes.
Then, when the user expands a node, you'll receive a TVN_ITEMEXPANDING notification. In response to that, you enumerate exactly one level of items under that node and insert them into your display tree. Again, for each that might have children, you use I_CHILDRENCALLBACK
to have it do a callback that says it has child nodes.
Edit: I should also mention: one common reason for the first problem you cite (seeming to get the same item enumerated repeatedly) is failing to update the length of name parameter every iteration. It's an in/out parameter that gets set to the length of the current name being retrieved at each call. For example:
wchar_t name[256];
size_t len = sizeof(name)/sizeof(name[0]);
int i=0;
RegEnumKeyEx(root, i, name, &len, /* ... */);
// Now, name = "XXX", len = 3;
++i;
RegEnumKeyEx(root, i, name, &len, /* ... */);
// The next name is, say, "YYYY";
// `name` still contains "XXX":
// `len=3`, and "YYYY" won't fit into 3 characters.
精彩评论