假若我的ini文件如下所示:
[Section1]
Entry1=Value1
Entry2=Value2
Entry3=Value3
...
[Section2]
Entry1=Value1
Entry2=Value2
Entry3=Value3
...
我们知道,通过pApp->GetProfileString("Section1", "Entry1")可以读取section1下Entry1的值,假若我想知道Section1下所有键的信息(包括有多少个键,键名是什么)。我该如何做?

解决方案 »

  1.   

    DWORD GetProfileSection(
      LPCTSTR lpAppName,        // address of section name
      LPTSTR lpReturnedString,  // address of return buffer
      DWORD nSize               // size of return buffer
    );
    返回的lpReturnedString包含了一个个以\0结尾的字符串,最后一个字符串以\0\0结尾。每个字符串都是Entry=Value的形式,需要你自己去解析。
      

  2.   

    Using the Registry
    The following example demonstrates the use of the RegQueryInfoKey, RegEnumKeyEx, and RegEnumValue functions. The hKey parameter passed to each function is a handle to an open key. This key must be opened before the function call and closed afterward. // QueryKey - enumerates the subkeys of a given key and the associated 
    //    values and then copies the information about the keys and values 
    //    into a pair of edit controls and list boxes. 
    // hDlg - dialog box that contains the edit controls and list boxes 
    // hKey - key whose subkeys and values are to be enumerated 
     
    VOID QueryKey(HWND hDlg, HANDLE hKey) 

        CHAR     achKey[MAX_PATH]; 
        CHAR     achClass[MAX_PATH] = "";  // buffer for class name 
        DWORD    cchClassName = MAX_PATH;  // length of class string 
        DWORD    cSubKeys;                 // number of subkeys 
        DWORD    cbMaxSubKey;              // longest subkey size 
        DWORD    cchMaxClass;              // longest class string 
        DWORD    cValues;              // number of values for key 
        DWORD    cchMaxValue;          // longest value name 
        DWORD    cbMaxValueData;       // longest value data 
        DWORD    cbSecurityDescriptor; // size of security descriptor 
        FILETIME ftLastWriteTime;      // last write time 
     
        DWORD i, j; 
        DWORD retCode, retValue; 
     
        CHAR  achValue[MAX_VALUE_NAME]; 
        DWORD cchValue = MAX_VALUE_NAME; 
        CHAR  achBuff[80]; 
     
        // Get the class name and the value count. 
        RegQueryInfoKey(hKey,        // key handle 
            achClass,                // buffer for class name 
            &cchClassName,           // length of class string 
            NULL,                    // reserved 
            &cSubKeys,               // number of subkeys 
            &cbMaxSubKey,            // longest subkey size 
            &cchMaxClass,            // longest class string 
            &cValues,                // number of values for this key 
            &cchMaxValue,            // longest value name 
            &cbMaxValueData,         // longest value data 
            &cbSecurityDescriptor,   // security descriptor 
            &ftLastWriteTime);       // last write time 
     
        SetDlgItemText(hDlg, IDE_CLASS, achClass); 
        SetDlgItemInt(hDlg, IDE_CVALUES, cValues, FALSE); 
     
        SendMessage(GetDlgItem(hDlg, IDL_LISTBOX), 
            LB_ADDSTRING, 0, (LONG) ".."); 
     
        // Enumerate the child keys, until RegEnumKeyEx fails. Then 
        // get the name of each child key and copy it into the list box.     SetCursor(LoadCursor(NULL, IDC_WAIT)); 
        for (i = 0, retCode = ERROR_SUCCESS; 
                retCode == ERROR_SUCCESS; i++) 
        { 
            retCode = RegEnumKeyEx(hKey, 
                         i, 
                         achKey, 
                         MAX_PATH, 
                         NULL, 
                         NULL, 
                         NULL, 
                         &ftLastWriteTime); 
            if (retCode == (DWORD)ERROR_SUCCESS) 
            {
                SendMessage(GetDlgItem(hDlg, IDL_LISTBOX), 
                    LB_ADDSTRING, 0, (LONG) achKey); 
            }
        } 
        SetCursor(LoadCursor (NULL, IDC_ARROW)); 
     
        // Enumerate the key values. 
        SetCursor(LoadCursor(NULL, IDC_WAIT)); 
     
        if (cValues) 
        {
            for (j = 0, retValue = ERROR_SUCCESS; 
                    j < cValues; j++) 
            { 
                cchValue = MAX_VALUE_NAME; 
                achValue[0] = '\0'; 
                retValue = RegEnumValue(hKey, j, achValue, 
                    &cchValue, 
                    NULL, 
                    NULL,    // &dwType, 
                    NULL,    // &bData, 
                    NULL);   // &bcData 
     
                if (retValue != (DWORD) ERROR_SUCCESS && 
                        retValue != ERROR_INSUFFICIENT_BUFFER) 
                { 
                    wsprintf (achBuff, 
                        "Line:%d 0 based index = %d, retValue = %d, " 
                         "ValueLen = %d", 
                         __LINE__, j, retValue, cchValue); 
                    MessageBox (hDlg, achBuff, "Debug", MB_OK); 
                } 
     
                achBuff[0] = '\0'; 
     
                // Add each value to a list box. 
                if (!lstrlen(achValue)) 
                    lstrcpy(achValue, "<NO NAME>"); 
                wsprintf(achBuff, "%d) %s ", j, achValue); 
                SendMessage(GetDlgItem(hDlg,IDL_LISTBOX2), 
                    LB_ADDSTRING, 0, (LONG) achBuff); 
            } 
     
        SetCursor(LoadCursor(NULL, IDC_ARROW)); 

      

  3.   

    [Section1]
    Entry1=Value1
    Entry2=Value2
    Entry3=Value3
    如果是类似这样标签的东西,可以这样
    [Section1]
    EntryCount = 3
    Entry1=Value1
    Entry2=Value2
    Entry3=Value3
    写一个EntryCount 表示总共有多少个标签有效。
      

  4.   

    同意oldworm(oldworm) btw,我反感注册表。不够绿色,重装系统后就丢失配置了。
      

  5.   

    我的程序是在xp下开发的,当时用的是WriteProfileString及WriteProfileInt保存信息的.用RegEnumValue取得指定Section下的所有键名及键值的,在xp下运行很好.但是在windows98下运行就不行了,原因是WriteProfileString及WriteProfileInt没有将信息写入到注册表中,而是写到了win.ini文件中,这样用RegEnumValue就不能得到指定Section下的所有键名及键值了,所以我想实现一个类似于RegEnumValue函数的功能而且既可以在xp下读注册表又可以在98下读ini的东西。怎样做呢?
      

  6.   

    to oldworm(oldworm):
    我的键名并没有如此有规律