[Interest] about QSettings

Bob Hood bhood2 at comcast.net
Thu Jul 19 04:47:07 CEST 2012


On 7/18/2012 8:16 PM, Tony Rietwyk wrote:
>>> *Sent:* Tuesday, July 17, 2012 9:22 PM
>>>
>>> hi,
>>> when i use the qt 4.7.4 with windows xp, i found that when i use the
>>> QSettings to write sub key of HKEY_LOCAL_MACHINE, it fails.
>>> but when i write the sub key of HKEY_CURRENT_USER, it success.
>>>
>>> i know that QSettings use the windows api (eg RegCreateKey....) to
>>> do it, so I use the RegCreateKey to do it directly, but it fails too.
>>>
>>> my windows account is administrator.
>>>
>>> thanks in advance.
>
> Hi, 
>
> What error do you get from calling RegCreateKey directly? ...

As Tony points out, you should use something like GetLastError() to see if it
gives you any significant clues as to why RegCreateKey() failed.  I often use
a Windows-specific function to wrap GetLastError() in order to give me a
humanly understandable version of the last error result:

    LPTSTR GetLastErrorText( LPTSTR lpszBuf, DWORD dwSize )
    {
        DWORD dwRet;
        LPTSTR lpszTemp = NULL;

        dwRet = FormatMessage( FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |FORMAT_MESSAGE_ARGUMENT_ARRAY,
                               NULL,
                               GetLastError(),
                               LANG_NEUTRAL,
                               (LPTSTR)&lpszTemp,
                               0,
                               NULL );

        // supplied buffer is not long enough
        if ( !dwRet || ( (long)dwSize < (long)dwRet+14 ) )
            lpszBuf[0] = TEXT('\0');
        else
        {
            lpszTemp[lstrlen(lpszTemp)-2] = TEXT('\0');  //remove cr and
newline character
            _stprintf( lpszBuf, TEXT("%s (0x%x)"), lpszTemp, GetLastError() );
        }

        if ( lpszTemp )
            LocalFree((HLOCAL) lpszTemp );

        return lpszBuf;
    }




More information about the Interest mailing list