现在的位置: 首页 > 综合 > 正文

过SetRegistrykey的作用

2018年06月09日 ⁄ 综合 ⁄ 共 8068字 ⁄ 字号 评论关闭

在利用MFC框架的时候,在应用程序应用类的的InitInstance()函数中,初始化时总有一个

以下是我在网上找到的一些资料:

过SetRegistrykey 导致应用程序设置,而不是存储在注册表中。INI文件。

名SetRegistryKey这个函数功能是设置MFC程序的注册表访问键,并把读写INI文件的成员函数映射到读写注册表。只要调用一下名SetRegistryKey并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了〜

WriteProfileBinary 写入二进制数据的应用程序的INI文件中的条目。
WriteProfileInt 整数写入应用程序的INI文件中的条目。
WriteProfileString 将一个字符串写入到应用程序的INI文件中的条目。



GetProfileBinary 检索二进制数据从应用程序的INI文件中的一个条目。
GetProfileInt 从应用程序的INI文件中的一个条目检索一个整数。
GetProfileString 从应用程序的INI文件中的一个条目检索字串。


应用程序“)); / /这里是准备在注册表HKEY_CURRENT_USER / /软件下面生成一个薄利的应用程序strUserName中,strPassword WriteProfileString(的“LOGINFO”,“用户名”,strUserName中)在/ /向注册表HKEY_CURRENT_USER / /软件/ /勃利的应用程序/
/ / /分支下写入用户名字符串行键值〜WriteProfileString(“LOGINFO LOGINFO “,”密码“,strPassword);/ /同上~~  strUserName中GetProfileString(的“LOGINFO”,“用户名”);/ /这里是读取HKEY_CURRENT_USER / /软件/ /勃利的应用程序/ / / / LOGINFO分支下的用户名字符串键值到strUserName中〜 strPassword = GetProfileString(的“LOGINFO”,“密码”);在如果不是在CWinApp的派生的类中读写注册表,可以直接用: strUserName中theApp.GetProfileString(的“LOGINFO”,“用户名”) strPassword
= theApp.GetProfileString(的“LOGINFO”,“密码”); 
 strUserName中= AfxGetApp() - > GetProfileString(的“LOGINFO”,“用户名”); 条条大路通罗马。












下列是MFC实现的代码:

/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / 
/ / CWinApp的设置的助手

#IFDEF AFX_INIT_SEG 
的#pragma code_seg(AFX_INIT_SEG)的
#ENDIF

无效的CWinApp ::过SetRegistrykey(LPCTSTR lpszRegistryKey)

 断言(m_pszRegistryKey == NULL); 
 的ASSERT(lpszRegistryKey = NULL); 
 ASSERT(m_pszAppName = NULL);

 BOOL bEnable = AfxEnableMemoryTracking的(FALSE); 
 免费((void *)个m_pszRegistryKey); 
 m_pszRegistryKey = _tcsdup(lpszRegistryKey); 
 免费((void *)个m_pszProfileName); 
 m_pszProfileName = _tcsdup(m_pszAppName); 
 AfxEnableMemoryTracking(bEnable); 
}

无效的CWinApp ::过SetRegistrykey(UINT nIDRegistryKey)

 断言(m_pszRegistryKey == NULL);

 TCHAR szRegistryKey [256] 
 VERIFY(AfxLoadString(nIDRegistryKey,szRegistryKey)); 
 过SetRegistrykey(szRegistryKey); 
}

/ /返回键为的HKEY_CURRENT_USER /“软件”/ RegistryKey的/ 
/ /创建PROFILENAME 如果它不存在,
/ /调用者承担RegCloseKey()上返回HKEY 
HKEY的CWinApp :: GetAppRegistryKey()

 ASSERT(m_pszRegistryKey = NULL); 
 ASSERT(的m_pszProfileName!= NULL);

 HKEY hAppKey = NULL; 
 HKEY hSoftKey = NULL; 
 HKEY hCompanyKey = NULL; 
 (RegOpenKeyEx的(HKEY_CURRENT_USER _T(“软件”),0,KEY_WRITE | KEY_READ,
  hSoftKey)== ERROR_SUCCESS)
 { 
  DWORD DW; 
  如果(RegCreateKeyEx的(hSoftKey m_pszRegistryKey,0,REG_NONE,
   REG_OPTION_NON_VOLATILE,KEY_WRITE | KEY_READ,NULL,
   &hCompanyKey,与DW)== ERROR_SUCCESS)
  { 
   RegCreateKeyEx的(0 m_pszProfileName hCompanyKey REG_NONE,
    REG_OPTION_NON_VOLATILE,KEY_WRITE | KEY_READ,NULL,
    &hAppKey,与DW); 
  } 
 } 
 如果( = NULL)
  RegCloseKey hSoftKey!(hSoftKey); 
 (hCompanyKey!= NULL)
  RegCloseKey(hCompanyKey);

 返回hAppKey; 
}

/ /返回键:
/ / HKEY_CURRENT_USER / /“软件”的RegistryKey / AppName的/ lpszSection 
/ /创建它,如果它不存在。
/ /者承担调用RegCloseKey()返回的HKEY 
HKEY的CWinApp :: GetSectionKey (LPCTSTR lpszSection)

 断言(lpszSection!= NULL);

 HKEY hSectionKey = NULL; 
 HKEY hAppKey = GetAppRegistryKey(); 
 如果(hAppKey == NULL)
  返回NULL;


 ( DWORD DW RegCreateKeyEx的,lpszSection hAppKey,0,REG_NONE,
  REG_OPTION_NON_VOLATILE,KEY_WRITE | KEY_READ,NULL,
  &hSectionKey,DW); 
 RegCloseKey(hAppKey); 
 返回hSectionKey的; 
}

UINT CWinApp :: GetProfileInt函数(LPCTSTR lpszSection LPCTSTR lpszEntry,
 INT n默认)

 ASSERT(的lpszSection!= NULL); 
 断言(lpszEntry!= NULL);:
 (m_pszRegistryKey!= NULL)/ /使用注册表
 { 
  HKEY hSecKey = GetSectionKey(lpszSection ); 
  如果(hSecKey == NULL)
   回报n默认; 
  DWORD dwValue的; 
  DWORD dwType; 
  DWORD dwCount = SIZEOF(DWORD); 
  龙LRESULT =函数RegQueryValueEx(hSecKey,(LPTSTR)lpszEntry,NULL,&dwType,
   (LPBYTE)&dwValue的,与dwCount); 
  RegCloseKey(hSecKey),
  如果(LRESULT == ERROR_SUCCESS)
  { 
   断言(dwType == REG_DWORD); 
   ASSERT(dwCount == sizeof(dwValue的)的); 
   dwValue的回报(UINT); 
  } 
  n默认; 
 } 
 否则
 { 
  ASSERT(m_pszProfileName! NULL); 
  返回:: GetPrivateProfileInt(lpszSection,lpszEntry,n默认,
   m_pszProfileName); 
 } 
}

CString的的CWinApp :: GetProfileString(LPCTSTR lpszSection LPCTSTR lpszEntry,
 LPCTSTR lpszDefault)

 ASSERT(的lpszSection!= NULL); 
 ASSERT(lpszEntry!= NULL); 
 (m_pszRegistryKey!= NULL)
 { 
  HKEY hSecKey = GetSectionKey(lpszSection); 
  ( == NULL,hSecKey)
   返回lpszDefault 
  的CString strValue的
  DWORD dwType,dwCount 
  龙LRESULT =函数RegQueryValueEx(hSecKey,(LPTSTR)lpszEntry,NULL,&dwType,
   NULL,&dwCount); 
  (LRESULT == ERROR_SUCCESS)
  { 
   ASSERT(dwType == REG_SZ); 
   LRESULT =函数RegQueryValueEx(hSecKey,(LPTSTR)lpszEntry,NULL,&dwType,
    (LPBYTE)strValue.GetBuffer“(dwCount /大小(TCHAR)),&dwCount); 
   strValue.ReleaseBuffer(); 
  } 
  RegCloseKey(hSecKey)的; 
  ( LRESULT == ERROR_SUCCESS)
  { 
   断言(dwType == REG_SZ); 
   strValue的回报; 
  } 
  回报lpszDefault; 
 } 
 其他
 { 
  ASSERT(的m_pszProfileName!= NULL);

  如果(lpszDefault == NULL)
   lpszDefault = _T(“”); / /不传递NULL 
  TCHAR SZT [4096]; 
  DWORD DW = :: GetPrivateProfileString(lpszEntry lpszSection,
   lpszDefault,SZT,_countof(SZT),m_pszProfileName, ); 
  ASSERT(DW <4095); 
  返回SZT; 
 } 
}

:BOOL CWinApp :: GetProfileBinary函数(LPCTSTR lpszSection,LPCTSTR lpszEntry,
 BYTE ** ppData,UINT * pBytes)

 ASSERT(的lpszSection!= NULL); 
 的ASSERT(lpszEntry = NULL); 
 ASSERT(ppData = NULL); 
 ASSERT(pBytes! = NULL); 
 * ppData = NULL; 
 * 
 如果pBytes = 0; (m_pszRegistryKey!= NULL)
 { 
  HKEY hSecKey = GetSectionKey(lpszSection); 
  (hSecKey == NULL)
   返回FALSE;

  DWORD dwType,dwCount; 
  龙LRESULT =函数RegQueryValueEx((LPTSTR)lpszEntry,NULL,&dwType,
   NULL,&dwCount hSecKey ),
  * pBytes = dwCount; 
  (LRESULT == ERROR_SUCCESS)
  { 
   ASSERT(dwType == REG_BINARY)
   * ppData =新的字节[* pBytes]; 
   LRESULT =函数RegQueryValueEx(hSecKey,(LPTSTR)lpszEntry,NULL,&dwType,
    * ppData,dwCount); 
  } 
  RegCloseKey(hSecKey); 
  (LRESULT == ERROR_SUCCESS)
  { 
   断言(dwType == REG_BINARY); 
   返回TRUE; 
  }   {    删除[] * ppData    * ppData = NULL;   }   返回FALSE;  }  其他 {   ASSERT(的m_pszProfileName!= NULL);

  CString的STR = GetProfileString(lpszSection,lpszEntry,NULL); 
  (str.IsEmpty())
   返回FALSE; 
  ASSERT(str.GetLength()%2 == 0); 
  INT_PTR nLen = str.GetLength()
  * pBytes = UINT (nLen)/ 2; 
  * ppData =新的字节[* pBytes]; 
  (诠释我= 0;我<nLen;我+ = 2)
  { 
   (* ppData)[我/ 2] =(BYTE)
    (((STR [ I +1] - 'A')<< 4)+(STR [] - 'A')); 
  } 
  返回TRUE; 
 } 
}

#IFDEF AFX_CORE3_SEG 
的#pragma code_seg(AFX_CORE3_SEG)的
#ENDIF

BOOL CWinApp :: WriteProfileInt函数(LPCTSTR lpszSection LPCTSTR lpszEntry,
 INT n值)

 ASSERT(的lpszSection!= NULL); 
 断言(lpszEntry!= NULL); 
 (m_pszRegistryKey!= NULL)
 { 
  HKEY hSecKey = GetSectionKey(lpszSection); 
  ( hSecKey == NULL)
   返回FALSE; 
  龙LRESULT =函数RegSetValueEx,lpszEntry hSecKey,NULL,REG_DWORD 
   (LPBYTE),及n值,大小(n值); 
  RegCloseKey(hSecKey); 
  返回LRESULT == ERROR_SUCCESS; 
 } 
 否则
 { 
  ASSERT(m_pszProfileName的! = NULL);

  TCHAR SZT [16]; 
  wsprintf(SZT,_T(“%D”),n值); 
  :: WritePrivateProfileString(lpszSection,lpszEntry,SZT,
   m_pszProfileName)的; 
 } 
}

BOOL的CWinApp :: WriteProfileString(LPCTSTR lpszSection LPCTSTR lpszEntry,
   LPCTSTR lpszValue)

 ASSERT(的lpszSection!= NULL),
 (m_pszRegistryKey!= NULL)
 { 
  龙LRESULT; 
  如果(lpszEntry == NULL)/ /删除整段
  { 
   HKEY hAppKey = GetAppRegistryKey(); 
   如果(hAppKey == NULL)
    返回FALSE; 
   LRESULT = :: RegDeleteKey(hAppKey,lpszSection); 
   RegCloseKey(hAppKey); 
  } 
  否则,如果(lpszValue == NULL)
  { 
   HKEY hSecKey = GetSectionKey(lpszSection); 
   如果(hSecKey == NULL)
    返回FALSE 
   / /有必要抛弃常量低于
   LRESULT = :: RegDeleteValue(hSecKey,(LPTSTR)lpszEntry。); 
   RegCloseKey(hSecKey); 
  } 
  否则
  { 
   HKEY hSecKey = GetSectionKey(lpszSection)
   (hSecKey == NULL)
    返回FALSE; 
   LRESULT =函数RegSetValueEx(,lpszEntry hSecKey,NULL,REG_SZ,
    (LPBYTE)lpszValue。,(lstrlen(lpszValue)+1)* sizeof(TCHAR的)的); 
   RegCloseKey(hSecKey); 
  } 
  返回LRESULT == ERROR_SUCCESS ; 
 }  {   ASSERT(的m_pszProfileName!= NULL);   ASSERT(lstrlen(m_pszProfileName)<4095); / /无法读取更大的  回报::的WritePrivateProfileString(lpszSection lpszEntry,lpszValue    m_pszProfileName);  } }

BOOL CWinApp :: WriteProfileBinary函数(
 LPBYTE pData所LPCTSTR lpszEntry,LPCTSTR lpszSection,UINT nBytes个)

 ASSERT(的lpszSection!= NULL); 
 (m_pszRegistryKey!= NULL)
 { 
  龙LRESULT; 
  HKEY hSecKey = GetSectionKey(lpszSection); 
  如果(hSecKey = = NULL)
   返回FALSE; 
  LRESULT =函数RegSetValueEx(,lpszEntry hSecKey,NULL,REG_BINARY,
   pData所,nBytes个); 
  RegCloseKey(hSecKey); 
  返回LRESULT == ERROR_SUCCESS; 
 }

 / /转换字符串和写出来
 LPTSTR lpsz =新TCHAR [nBytes个* 2 +1]; 
 UINTí; 
 (我= 0;我<nBytes个;我+ +)
 { 
  lpsz [* 2] =(TCHAR)((pData所[I]&0x0F的)+'A'); / /低四位
  lpsz [* 2 +1] =(TCHAR)(((pData所[I] >> 4)&0x0F的)+'A')/ /高四位
 } 
 lpsz [* 2] = 0;

 ASSERT(的m_pszProfileName!= NULL);

 BOOL bResult = WriteProfileString(lpszSection,lpszEntry,lpsz); 
 删除[] lpsz 
 返回bResult; 
}

/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / /

【上篇】
【下篇】

抱歉!评论已关闭.