CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 1999
    Location
    Earth
    Posts
    124

    Registry Enumeration problems!

    I am trying to enumerate a section of the registry. Lets say that the section I am dealing
    with has 15 subkeys, each of those have 3 subkeys, and each of those have 2 more. The
    problem is when I attempt to enum the registry, it will read the first subkey of the 15,
    then the first subkey of the 3, then the first of the 2
    and then the app stops. That leaves me with alot missing. If I change afew things, then it will
    return the top 15 subkeys, but will not enter into any of the layers. This is what I am trying.
    Any ideas as to what is wrong?


    if (lreg != ERROR_SUCCESS)
    {
    return;
    }
    if (lreg == ERROR_SUCCESS)
    {
    DWORD cnt;
    CString val;
    valname = val.GetBuffer(val.GetLength() + 20);
    regResult =RegQueryInfoKey(hKeyResult,
    clsbuf,
    &dwclsBufsize,
    NULL,
    &dwsubBufsize,
    &dwMaxsubbufsize,
    &dwMaxclsbufsize,
    &dwValnumbuf,
    &dwMaxValname,
    &dwMaxValdata,
    NULL,
    &lastWrite);
    nIndex++;
    for (dwCount=0, lreg = ERROR_SUCCESS;
    lreg == ERROR_SUCCESS; dwCount++)
    {
    // Set values
    CString buf;
    subbuf = buf.GetBuffer(buf.GetLength() + 20);
    clsbuf = buf.GetBuffer(buf.GetLength() + 20);
    DWORD subkeySize;
    HKEY htemp = hKeyResult;
    LPTSTR temp = subbuf;
    subkeySize = dwMaxsubbufsize+1;
    subbuf = new TCHAR[subkeySize];
    DWORD dwCnt;
    cnt = dwCount;
    // End set values

    // Enum keys
    lreg = RegEnumKeyEx(hKeyResult,
    dwCount,
    subbuf,
    &subkeySize,
    NULL,
    clsbuf,
    &dwclsBufsize,
    &lastWrite);

    // Send name of keys to list
    refCtrl.InsertItem(dwCount, (LPCTSTR)subbuf);
    refCtrl.SetItem(dwCount, 1, LVIF_TEXT,
    (LPCTSTR)clsbuf, NULL,
    LVIS_SELECTED, -1, NULL);
    refCtrl.UpdateWindow();
    // End list items

    if (dwValnumbuf != 0 && lreg == ERROR_SUCCESS)
    {
    for (dwCnt = 0; dwCnt < dwValnumbuf; dwCnt++)
    {
    //declare variables
    valname = '\0';
    vallength = dwMaxValname+1;
    databufsize=dwMaxValdata+1;
    char databuf[200];
    CString data;
    LPCTSTR valdata;

    // Enum values
    lreg = RegEnumValue(hKeyResult,
    dwCnt,
    valname,
    &vallength,
    NULL,
    &dwRegType,
    (LPBYTE)databuf,
    &databufsize);

    //Convert value data to readable item,
    // then add to list
    data = (LPSTR)databuf;
    valdata = data.GetBuffer(data.GetLength());

    // Send name of keys to list
    refCtrl.SetItem(dwCnt, 2, LVIF_TEXT,
    (LPCTSTR)valname, NULL,
    LVIS_SELECTED, -1, NULL);
    refCtrl.SetItem(dwCnt, 3, LVIF_TEXT,
    (LPCTSTR)valdata, NULL,
    LVIS_SELECTED, -1, NULL);
    refCtrl.UpdateWindow();
    // End list items
    }
    }
    //Enum here ???
    while (cnt > 0)
    {
    cnt--;
    char * sub = "";
    lstrcat(sub, "\\");
    lstrcat(sub, subbuf);
    subbuf = sub;
    EnumReg(hKeyResult, subbuf);
    }
    }
    }
    RegCloseKey(hKeyResult);





  2. #2
    Join Date
    May 1999
    Posts
    318

    Re: Registry Enumeration problems!

    It is difficult to well understand because your sample is not complete.

    Is EnumReg the name of your function ?

    The first problem is think is the first time you call RegEnumKeyEx. You add the subkey to your list even if the key have no subkey.
    I think you must verify lreg before adding to the list.

    Now to access all subkeys and the values of the subkeys you must call your own function (reentrant function).
    I think you do that in your 'while (cnt > 0)' loop.

    I try a simple code.
    Read it and try to take what you need for.
    I dont test it.

    For the first call you must (as for the subkey in the function)
    1) Open the master key
    2) Call the function with key handle
    3) Close the key

    I make some changes.
    For example all the browse stops on any error (looks for returning on errors in the code).
    Then if the key has no more subkeys i return success to go on with the browser.

    Forgive me for all mistakes in the code.


    LONG EnumReg( HKEY hKeyResult, LPCSTR subname ) {

    /*.. Declarations ..*/

    valname = val.GetBuffer(val.GetLength() + 20);
    regResult = RegQueryInfoKey(hKeyResult,
    clsbuf,
    &dwclsBufsize,
    NULL,
    &dwsubBufsize,
    &dwMaxsubbufsize,
    &dwMaxclsbufsize,
    &dwValnumbuf,
    &dwMaxValname,
    &dwMaxValdata,
    NULL,
    &lastWrite);
    nIndex++;
    for (dwCount=0, lreg = ERROR_SUCCESS; lreg == ERROR_SUCCESS; dwCount++) {
    // Set values
    CString buf;
    subbuf = buf.GetBuffer(buf.GetLength() + 20);
    clsbuf = buf.GetBuffer(buf.GetLength() + 20);
    DWORD subkeySize;
    HKEY htemp = hKeyResult;
    LPTSTR temp = subbuf;
    subkeySize = dwMaxsubbufsize+1;
    subbuf = new TCHAR[subkeySize];
    DWORD dwCnt;
    cnt = dwCount;
    // End set values

    // Enum keys
    lreg = RegEnumKeyEx(hKeyResult,
    dwCount,
    subbuf,
    &subkeySize,
    NULL,
    clsbuf,
    &dwclsBufsize,
    &lastWrite);

    // CHANGES
    if ( lreg!=ERROR_SUCCESS ) {
    // NO MORE SUBKEYS
    return ERROR_SUCCESS;
    }

    // Send name of keys to list
    refCtrl.InsertItem(dwCount, (LPCTSTR)subbuf);
    refCtrl.SetItem(dwCount, 1, LVIF_TEXT,
    (LPCTSTR)clsbuf, NULL,
    LVIS_SELECTED, -1, NULL);
    refCtrl.UpdateWindow();
    // End list items

    if (dwValnumbuf != 0 && lreg == ERROR_SUCCESS) {
    for (dwCnt = 0; dwCnt < dwValnumbuf; dwCnt++) {
    //declare variables
    valname = '\0';
    vallength = dwMaxValname+1;
    databufsize=dwMaxValdata+1;
    char databuf[200];
    CString data;
    LPCTSTR valdata;

    // Enum values
    lreg = RegEnumValue(hKeyResult,
    dwCnt,
    valname,
    &vallength,
    NULL,
    &dwRegType,
    (LPBYTE)databuf,
    &databufsize);

    //Convert value data to readable item,
    // then add to list
    data = (LPSTR)databuf;
    valdata = data.GetBuffer(data.GetLength());

    // Send name of keys to list
    refCtrl.SetItem(dwCnt, 2, LVIF_TEXT,
    (LPCTSTR)valname, NULL,
    LVIS_SELECTED, -1, NULL);
    refCtrl.SetItem(dwCnt, 3, LVIF_TEXT,
    (LPCTSTR)valdata, NULL,
    LVIS_SELECTED, -1, NULL);
    refCtrl.UpdateWindow();
    // End list items
    }
    }

    if ( lreg!=ERROR_SUCCESS )
    return lreg;

    // CHANGES
    // Enumrate subkeys of the key
    HKEY hsubkey;
    if ( RegOpenKeyEx( kHkeyResult, subbuf, 0, KEY_ENUMERATE_SUB_KEYS, &hsubkey ) == ERROR_SUCCESS ) {
    lreg=EnumReg(hsubkey);
    RegCloseKey(hsubkey);
    if ( lreg!=ERROR_SUCCESS )
    return lreg;
    }
    }
    return lreg;
    }




    hope this helps




  3. #3
    Join Date
    Jun 1999
    Location
    Earth
    Posts
    124

    Re: Registry Enumeration problems!

    You are right in saying that EnumReg is my function that contains this code. As a note you forgot to add the RegOpenKeyEx and open it with ENUMERATE status, before calling RegQueryInfoKey. Otherwise you will get an out of memory error. Anyway.
    The code , although it does prevent a few errors, does exactly what it did before. It still goes to the first subkey, then to the first subkey of that one, and on down then stops. It never goes back to perform enumeration on the other subkeys.


  4. #4
    Join Date
    May 1999
    Posts
    318

    Re: Registry Enumeration problems!

    I have some times to spent.
    So i make a little code that works.
    Send it to you using private mail.



Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured