在Win2000下,我在程序中给注册表新建一项,但是在机器重新启动后该项又没有了,谁能告诉我什么原因?

解决方案 »

  1.   

    unsigned char tmp[256];HKEY m_hKEY;
    DWORD type=REG_SZ;
    DWORD size;
    LPCTSTR path="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\TEST";long ret=::RegOpenKeyEx(HKEY_LOCAL_MACHINE,path,0,KEY_WRITE, &m_hKEY);
    if(ret!=ERROR_SUCCESS)

    DWORD dw; LONG ReturnValue = ::RegCreateKeyEx (HKEY_LOCAL_MACHINE, path, 0L, NULL,REG_OPTION_VOLATILE, KEY_CREATE_SUB_KEY, NULL, 
    &m_hKEY, &dw); if(ReturnValue == ERROR_SUCCESS)

    CString str="12345";
    size=str.GetLength()+1;
    sprintf((char *)tmp,"%s",str);
    ret=::RegSetValueEx(m_hKEY,"NUM",NULL,type,tmp,size);
    }
    }
    ::RegCloseKey(m_hKEY);
      

  2.   

    可以先在win98上试试,win98没有权限要求
      

  3.   

    对比以下例子:bool CreateKey(LPCTSTR pszPath)
    {
    DWORD dw; LONG ReturnValue = RegCreateKeyEx (HKEY_LOCAL_MACHINE, pszPath, 0L, NULL,
    REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, 
    &hKEY, &dw); if(ReturnValue == ERROR_SUCCESS)
    return true; return false;
    }bool OpenReg()
    {
    LPCTSTR data_Set="Software\\BC\\VideoServer\\";
    long lRet = (::RegOpenKeyEx(HKEY_LOCAL_MACHINE,data_Set, 0, KEY_ALL_ACCESS, &hKEY)); if(lRet == ERROR_SUCCESS)
    return true; bool bRet = CreateKey(data_Set);
    return false;
    }REG_OPTION_NON_VOLATILE This key is not volatile; this is the default. The information is stored in a file and is preserved when the system is restarted. The RegSaveKey function saves keys that are not volatile.  
    REG_OPTION_VOLATILE 
    Windows NT/2000: All keys created by the function are volatile. The information is stored in memory and is not preserved when the corresponding registry hive is unloaded. For HKEY_LOCAL_MACHINE, this occurs when the system is shut down. For registry keys loaded by the RegLoadKey function, this occurs when the corresponding RegUnloadKey is performed. The RegSaveKey function does not save volatile keys. This flag is ignored for keys that already exist. 
    Windows 95: This value is ignored. If REG_OPTION_VOLATILE is specified, the RegCreateKeyEx function creates nonvolatile keys and returns ERROR_SUCCESS.
     
    REG_OPTION_BACKUP_RESTORE  Windows NT/2000: If this flag is set, the function ignores the samDesired parameter and attempts to open the key with the access required to backup or restore the key. If the calling thread has the SE_BACKUP_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_READ access. If the calling thread has the SE_RESTORE_NAME privilege enabled, the key is opened with ACCESS_SYSTEM_SECURITY and KEY_WRITE access. If both privileges are enabled, the key has the combined accesses for both privileges.  
      

  4.   

    你RegSetValueEx后也必须RegCloseKey