[C] looking for the first value in a key

This is a discussion on [C] looking for the first value in a key within the Windows Programming forums, part of the Platform Specific Boards category; Hi to all I want to put in a x[..] the NAME and in y[...] the DATA of the first ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    161

    [C] looking for the first value in a key

    Hi to all

    I want to put in a x[..] the NAME and in y[...] the DATA of the first string (except the default) found in a given key.

    I know how to open, create, delete keys but I dont know how to see how many strings there are in a key and how to copy the first (or second..) one.

    Example:

    (given key)
    HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\Curr entVersion\Run

    1. I want to know if there are some strings.
    2. If there are strings, look for the first (after default):

    Code:
        AVGCtrl    REG_SZ    "C:\Programmi\AVPersonal\AVGNT.EXE" /min
    3. I want to copy the string name(AVGCtrl) and value (C:\Prog..)

    Any hints ?
    This forum is the best one I've ever seen. Great ppl, great coders

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You are looking for the RegEnumValue() function. You can find additional registry functions by clicking the "Registry Functions" link that appears at the bottom of the documentation for each registry function.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    RegEnumValue() ahh... excellent
    This forum is the best one I've ever seen. Great ppl, great coders

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    161
    I had some time and I used it..
    The program opens/creates a given key, then looks in it for String and StringData. It works only for the first value and the while seems to stop but I don't understand why!

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <windows.h>
    
    #define MAXDIM 100
    #define MAX_KEY 256
    
    int main(int argc, char *argv[])
    {
      
      char ValueName[MAXDIM];
      char ValueData[MAXDIM];
      
      HKEY hKey;
      DWORD dwDisposition;
      DWORD cbValueData = MAX_KEY;
      DWORD cbValueName = MAX_KEY;
      
      int index = 0;
        
      RegCreateKeyEx(HKEY_LOCAL_MACHINE,
                     "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                     0,
                     NULL,
                     0,
                     KEY_ALL_ACCESS,
                     NULL,
                     &hKey,
                     &dwDisposition);
                     
                     
      while ((RegEnumValue(hKey,
              index++,
              ValueName,
              &cbValueName,
              NULL,
              NULL,
              ValueData,
              &cbValueData))==ERROR_SUCCESS){
      
      printf("Key %d -> %s\nString -> %s\n\n",index, ValueName, ValueData);
      }  
    
      printf("Found and printed %d Strings and Values in the given key\n\n", index-1);
      system("PAUSE");	
      return 0;
    }
    index is :dwIndex
    [in] Index of the subkey to retrieve. This parameter should be zero for the first call to the RegEnumKeyEx function and then incremented for subsequent calls.

    I try to increment it (index++) but is teems the while gets a NOT ERROR_SUCCESS so it stops.. It is strange coz if I modify by hand the index value, the program prints the correct String and StringData..

    Output (wrong, only the first string found..)

    String 1 -> NvCplDaemon
    Value -> RUNDLL32.EXE C:\WINDOWS\System32\NvCpl.dll,NvStartup

    Found and printed 1 Strings and Values in the given key
    Last edited by BianConiglio; 09-21-2004 at 02:22 PM.
    This forum is the best one I've ever seen. Great ppl, great coders

  5. #5
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    1000th Post!


    Since, lpcValueName and lpcbData are [IN, OUT] arguments, you need to reset their values after each call. You also need to remember that the output data may not be NULL terminated.

    Code:
      while ((RegEnumValue(hKey,
              index++,
              ValueName,
              &cbValueName,
              NULL,
              NULL,
              ValueData,
              &cbValueData))==ERROR_SUCCESS)
      {
         /* Make sure data is null terminated. */
         ValueData[cbValueData] = '\0';
    
         printf("Key %d -> %s\nString -> %s\n\n",index, ValueName, ValueData);
    
         cbValueName = sizeof(ValueName); /* Should be cchValueName. */
         cbValueData = sizeof(ValueData) - 1;
      }
    --
    Code:
      char ValueName[MAXDIM];
      char ValueData[MAXDIM];
      
      HKEY hKey;
      DWORD dwDisposition;
      DWORD cbValueData = MAX_KEY;
      DWORD cbValueName = MAX_KEY;
    MAX_KEY is larger than MAXDIM, not good!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. Directional Keys - Useing in Console
    By RoD in forum C++ Programming
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM
  4. FAQ: Directional Keys - Useing in Console
    By RoD in forum FAQ Board
    Replies: 38
    Last Post: 10-06-2002, 05:42 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21