请给出简单实例。

解决方案 »

  1.   

    注册表于ini文件有相似的作用
    C**App::InitInstance()
    {SetRegistryKey(_T("XX\\kdkf"));//建立键值
    }
    CMainFrame::OnClose()
    {CWinApp*pApp=AfxGetApp();
     pApp->WriteProfileInt("My Windows","top",123);
    ..
    }
    CMainFrame::PreCreateWindow()
    {int aa;
    CWinApp*pApp=AfxGetApp();
     aa=pApp->GetProfileInt("My Windows","top",123);
    ..
    }
      

  2.   

    写错了
    CMainFrame::PreCreateWindow()
    {int aa;
    CWinApp*pApp=AfxGetApp();if( pApp->GetProfileInt("My Windows","top",-1)!=-1)
    {aa=pApp->GetProfileInt("My Windows","top",-1);
    }
    ..
    }
      

  3.   

    我总结的对注册表进行操作的函数
    CreateKey( HKEY_LOCAL_MACHINE,"software\\key1\\key2\\key3");
    */
    BOOL CreateKey(HKEY cs_RootKey, CString cs_sKeyRoad, REGSAM cs_Access=KEY_ALL_ACCESS)
    {
    HKEY hRegKey;
    DWORD dw; if(::RegCreateKeyEx(cs_RootKey, cs_sKeyRoad, 0, NULL, REG_OPTION_NON_VOLATILE, 
                    cs_Access, NULL, &hRegKey, &dw)==ERROR_SUCCESS)
    {
    ::RegCloseKey(hRegKey);
    return TRUE;
    }
    else
    {
    return FALSE;
    }
    }
      

  4.   

    SetValueToKey(HKEY_LOCAL_MACHINE, "software\\key1\\key2\\key3", "zsf", "hello");
    */
    BOOL SetValueToKey(HKEY cs_RootKey, CString cs_sKeyRoad, CString cs_sKeyItem, CString cs_sItemValue, REGSAM cs_Access=KEY_ALL_ACCESS){
    HKEY hRegKey; 
    LPBYTE ItemValue;
    ItemValue= new BYTE [cs_sItemValue.GetLength()+1];
    if(ItemValue==NULL)//fail
    {
    return FALSE;
    } for(int i=0;i<cs_sItemValue.GetLength(); i++)
    {
    ItemValue[i]=cs_sItemValue.GetAt(i);
    }
    ItemValue[cs_sItemValue.GetLength()]=0; if(::RegOpenKeyEx(cs_RootKey, cs_sKeyRoad, 0, cs_Access, &hRegKey)==ERROR_SUCCESS)
    //open key
    {
    //save value to key
    if(::RegSetValueEx(hRegKey, cs_sKeyItem, 0, REG_SZ, ItemValue, cs_sItemValue.GetLength())==ERROR_SUCCESS)
    {
    ::RegCloseKey(hRegKey);
    delete [] ItemValue;
    ItemValue=NULL;
    return TRUE;
    }
    else
    {
    ::RegCloseKey(hRegKey);
    delete [] ItemValue;
    ItemValue=NULL;
    return FALSE;
    }
    }
    else
    {
    delete [] ItemValue;//free memory
    ItemValue=NULL;
    return FALSE;
    }
    }
      

  5.   

    ReadValueFromKey(m_txt, HKEY_LOCAL_MACHINE, "software\\KEY1\\KEY2\\KEY3", "zsf");
    */
    BOOL ReadValueFromKey(CString &cs_sRetValue, HKEY cs_RootKey, CString cs_sKeyRoad, CString cs_sKeyItem, REGSAM cs_Access=KEY_ALL_ACCESS)
    {
    HKEY hRegKey;
    DWORD lpT;
    char sKeyValue[300];
    DWORD dwBuffLength=300;

    if(::RegOpenKeyEx(cs_RootKey, cs_sKeyRoad, 0, cs_Access, &hRegKey)==ERROR_SUCCESS)
    {

    if(::RegQueryValueEx(hRegKey, cs_sKeyItem, NULL, &lpT, (LPBYTE)sKeyValue, &dwBuffLength)==ERROR_SUCCESS)
    {
    cs_sRetValue=sKeyValue; 
    ::RegCloseKey(hRegKey);
    return TRUE;;
    }
    else
    {
    ::RegCloseKey(hRegKey);
    return FALSE;
    }
    }
    else
    {
    return FALSE;
    }

    }