我在注册表SOFTWARE下面添加了一个新项:RegCode,下面添加了一个字符串键值。但是很奇怪,每次开机后改项都会自动变没有。怪哉.我的在Xp和2003上都试过。2000没有试

解决方案 »

  1.   

    //////////注册表类///////////////////////////
    // Registry.cpp : implementation file
    //#include "stdafx.h"
    #include "Registry.h"/////////////////////////////////////////////////////////////////////////////
    // CRegistryCRegistry::CRegistry(HKEY hKey)
    {
    m_hKey=hKey;
    }CRegistry::~CRegistry()
    {
    Close();
    }/////////////////////////////////////////////////////////////////////////////
    // CRegistry FunctionsBOOL CRegistry::CreateKey(LPCTSTR lpSubKey)
    {
    ASSERT(m_hKey);
    ASSERT(lpSubKey); HKEY hKey;
    DWORD dw;
    long lReturn=RegCreateKeyEx(m_hKey,lpSubKey,0L,NULL,REG_OPTION_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dw);
        
    if(lReturn==ERROR_SUCCESS)
    {
    m_hKey=hKey;
    return TRUE;
    }

    return FALSE;

    }BOOL CRegistry::Open(LPCTSTR lpSubKey)
    {
    ASSERT(m_hKey);
    ASSERT(lpSubKey);
        
    HKEY hKey;
    long lReturn=RegOpenKeyEx(m_hKey,lpSubKey,0L,KEY_ALL_ACCESS,&hKey);
        
    if(lReturn==ERROR_SUCCESS)
    {
            m_hKey=hKey;
    return TRUE;
    }
    return FALSE;

    }void CRegistry::Close()
    {
    if(m_hKey)
    {
    RegCloseKey(m_hKey);
    m_hKey=NULL;
    }

    }BOOL CRegistry::DeleteValue(LPCTSTR lpValueName)
    {
    ASSERT(m_hKey);
    ASSERT(lpValueName);

    long lReturn=RegDeleteValue(m_hKey,lpValueName);

    if(lReturn==ERROR_SUCCESS)
    return TRUE;
    return FALSE;

    }BOOL CRegistry::DeleteKey(HKEY hKey, LPCTSTR lpSubKey)
    {
    ASSERT(hKey);
    ASSERT(lpSubKey);

    long lReturn=RegDeleteValue(hKey,lpSubKey);

    if(lReturn==ERROR_SUCCESS)
    return TRUE;
    return FALSE;

    }BOOL CRegistry::Write(LPCTSTR lpSubKey, int nVal)
    {
    ASSERT(m_hKey);
    ASSERT(lpSubKey);

    DWORD dwValue;
    dwValue=(DWORD)nVal;

    long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwValue,sizeof(DWORD));

        if(lReturn==ERROR_SUCCESS)
    return TRUE;

    return FALSE;

    }BOOL CRegistry::Write(LPCTSTR lpSubKey, DWORD dwVal)
    {
    ASSERT(m_hKey);
    ASSERT(lpSubKey);

    long lReturn=RegSetValueEx(m_hKey,lpSubKey,0L,REG_DWORD,(const BYTE *) &dwVal,sizeof(DWORD));

        if(lReturn==ERROR_SUCCESS)
    return TRUE;

    return FALSE;

    }BOOL CRegistry::Write(LPCTSTR lpValueName, LPCTSTR lpValue)
    {
    ASSERT(m_hKey);
    ASSERT(lpValueName);
    ASSERT(lpValue); long lReturn=RegSetValueEx(m_hKey,lpValueName,0L,REG_SZ,(const BYTE *) lpValue,strlen(lpValue)+1);

        if(lReturn==ERROR_SUCCESS)
    return TRUE;

    return FALSE;

    }
    BOOL CRegistry::Read(LPCTSTR lpValueName, int* pnVal)
    {
    ASSERT(m_hKey);
    ASSERT(lpValueName);
    ASSERT(pnVal);

    DWORD dwType;
    DWORD dwSize=sizeof(DWORD);
    DWORD dwDest;
    long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);

    if(lReturn==ERROR_SUCCESS)
    {
    *pnVal=(int)dwDest;
    return TRUE;
    }
    return FALSE;

    }BOOL CRegistry::Read(LPCTSTR lpValueName, DWORD* pdwVal)
    {
    ASSERT(m_hKey);
    ASSERT(lpValueName);
    ASSERT(pdwVal);

    DWORD dwType;
    DWORD dwSize=sizeof(DWORD);
    DWORD dwDest;
    long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)&dwDest,&dwSize);

    if(lReturn==ERROR_SUCCESS)
    {
    *pdwVal=dwDest;
    return TRUE;
    }
    return FALSE;

    }
    BOOL CRegistry::RestoreKey(LPCTSTR lpFileName)
    {
    ASSERT(m_hKey);
    ASSERT(lpFileName);

    long lReturn=RegRestoreKey(m_hKey,lpFileName,REG_WHOLE_HIVE_VOLATILE);

    if(lReturn==ERROR_SUCCESS)
    return TRUE;

    return FALSE;
    }BOOL CRegistry::SaveKey(LPCTSTR lpFileName)
    {
    ASSERT(m_hKey);
    ASSERT(lpFileName);

    long lReturn=RegSaveKey(m_hKey,lpFileName,NULL);

    if(lReturn==ERROR_SUCCESS)
    return TRUE;

    return FALSE;
    }
    BOOL CRegistry::Read(LPCTSTR lpValueName, CString* lpVal)
    {
    ASSERT(m_hKey);
    ASSERT(lpValueName);
    ASSERT(lpVal);

    DWORD dwType;
    DWORD dwSize=200;
    char szString[2550];

    long lReturn=RegQueryValueEx(m_hKey,lpValueName,NULL,&dwType,(BYTE *)szString,&dwSize);

    if(lReturn==ERROR_SUCCESS)
    {
    *lpVal=szString;
    return TRUE;
    }
    return FALSE;

    }
    //////////////////////////我的代码///////////////////////////////////////
    ......
    CString regcode;
    CRegistry reg;
    BOOL bReturn;
    DWORD dw=10,dw2;
    int i=1,j; bReturn=reg.Open ("SOFTWARE\\RegCode\0");
    if(bReturn==FALSE)
    {
    //reg.Close();
    reg.Open("SOFTWARE\0");
    reg.CreateKey("RegCode\0");
    reg.Write("RegDate",10);
    }
    bReturn=reg.Read("RegCode",&regcode);
    if(bReturn==FALSE)
    {
    m_bIsReged=0;//没有注册不到期
    bReturn=reg.Read ("RegDate",&i);
    i--;
    if(i<=0)
    {
    AfxMessageBox("软件已经试用到期,请购买正式版本!");
    //kill process here
    m_bIsReged=1;//没有注册到期
    }
    else
    {
    reg.Write("RegDate",i);
    char msgbuf[100];
    sprintf(msgbuf,"该软件还有%i次使用限制,请及时注册!",i);
    AfxMessageBox(msgbuf);
    }
    return FALSE;
    }
    reg.Close();
    ......
      

  2.   

    http://www.vckbase.com/document/viewdoc/?id=603
    发现同样的问题
    希望能得到一个解释
      

  3.   

    将REG_OPTION_VOLATILE改为REG_OPTION_NON_VOLATILE
      

  4.   

    dwOptions 
    Specifies special options for the key. This parameter can be one of the following values. Value Meaning 
    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: This key is volatile; the information is stored in memory and is not preserved when the system is restarted. The RegSaveKey function does not save volatile keys. This flag is ignored if the key already exists. 
    Windows 95: This value is ignored. If REG_OPTION_VOLATILE is specified, the RegCreateKeyEx function creates a nonvolatile key and returns ERROR_SUCCESS.
     
    REG_OPTION_BACKUP_RESTORE  Windows NT: 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.