如题,对注册表进行写操作,RegSetValueEx返回非零,GetLastError返回零是什么原因。
代码如下:    HKEY  hk;
    DWORD dwData;
    TCHAR szBuf[80];
    BOOL  succ;    //create or open the reg.
    if (RegCreateKey(HKEY_CURRENT_USER,
        TEXT("Software\\MySoft\\1.0"),
        &hk))
    {
        // cannot create the reg.
        return false;
    }    dwData = 0;
    if (0 != RegSetValueEx(hk,
                      TEXT("value"),
                      0,
                      REG_DWORD,
                      (LPBYTE) dwData,
                      sizeof(DWORD)))
    {
        // close the reg and return false.
        // RegCloseKey(hk);
        // return false;
        LPVOID lpMsgBuf;
        FormatMessage( 
            FORMAT_MESSAGE_ALLOCATE_BUFFER | 
            FORMAT_MESSAGE_FROM_SYSTEM | 
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            GetLastError(),
            0, // Default language
            (LPTSTR) &lpMsgBuf,
            0,
            NULL 
            );
        // Process any inserts in lpMsgBuf.
        // ...
        // Display the string.
        MessageBox( NULL, (LPCTSTR)lpMsgBuf, NULL, MB_OK | MB_ICONINFORMATION );
        // Free the buffer.
        LocalFree( lpMsgBuf );
        
    }    RegCloseKey(hk);    return true;

解决方案 »

  1.   

    long lr = RegSetValueEx(...);
    if( lr != ERROR_SUCCESS )
    {
        ...
        FormatMessage(...,lr,...);
    }
      

  2.   

    用formatmessage,或者使用VS的Err Lookup工具输入错误码,就会返回错误信息(事实上该工具也是使用formatmessage的)
      

  3.   

    RegCreateKey
    The RegCreateKey function creates the specified registry key. If the key already exists in the registry, the function opens it. Note  This function is provided only for compatibility with 16-bit versions of Windows. Applications should use the RegCreateKeyEx function. 
    不知对你是否有用