前言:在用mfc框架编写应用的时候,如果注意,你会发现在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("String");  这是什么函数呢,设置记录字符串,不明觉厉,于是用心去查了一下,分享给大家。


总起:其实很明了,如果你的应用需要使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。







SetRegistryKey Causes application settings to be stored in the registry instead of .INI files.





设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了(有点类似指向切换)












WriteProfileBinary Writes binary data to an entry in the application's .INI file.
WriteProfileInt Writes an integer to an entry in the application's .INI file.
WriteProfileString Writes a string to an entry in the application's .INI file.












GetProfileBinary Retrieves binary data from an entry in the application's .INI file.
GetProfileInt Retrieves an integer from an entry in the application's .INI file.
GetProfileString Retrieves a string from an entry in the application's .INI file.


具体用法:

CString strUserName,strPassword;
WriteProfileString("LogInfo","UserName",strUserName); //向注册表HKEY_CURRENT_USER//software//boli's app//LogInfo//分支下写入 UserName 字符串行键值~
WriteProfileString("LogInfo","Password",strPassword);//同上

 strUserName = GetProfileString("LogInfo","UserName");// 这里是读取HKEY_CURRENT_USER//software//boli's app//LogInfo//分支下的 UserName 字符串键值到 strUserName
 strPassword =  GetProfileString("LogInfo","Password");

// 如果不是在CWinApp 派生的类中读写注册表,可以直接用:
 strUserName = theApp.GetProfileString("LogInfo","UserName");
 strPassword = theApp.GetProfileString("LogInfo","Password");
// 或 
 strUserName = AfxGetApp()->GetProfileString("LogInfo","UserName");


下面是MFC中的具体实现代码,可以看看是如何实现的:

////////////////////////////////////////////////////////////////////////////
// CWinApp Settings Helpers

#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif

void CWinApp::SetRegistryKey(LPCTSTR lpszRegistryKey)
{
 ASSERT(m_pszRegistryKey == NULL);
 ASSERT(lpszRegistryKey != NULL);
 ASSERT(m_pszAppName != NULL);

BOOL bEnable = AfxEnableMemoryTracking(FALSE);
 free((void*)m_pszRegistryKey);
 m_pszRegistryKey = _tcsdup(lpszRegistryKey);
 free((void*)m_pszProfileName);
 m_pszProfileName = _tcsdup(m_pszAppName);
 AfxEnableMemoryTracking(bEnable);
}

void CWinApp::SetRegistryKey(UINT nIDRegistryKey)
{
 ASSERT(m_pszRegistryKey == NULL);

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

// returns key for HKEY_CURRENT_USER/"Software"/RegistryKey/ProfileName
// creating it if it doesn't exist
// responsibility of the caller to call RegCloseKey() on the returned HKEY
HKEY CWinApp::GetAppRegistryKey()
{
 ASSERT(m_pszRegistryKey != NULL);
 ASSERT(m_pszProfileName != NULL);

HKEY hAppKey = NULL;
 HKEY hSoftKey = NULL;
 HKEY hCompanyKey = NULL;
 if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("software"), 0, KEY_WRITE|KEY_READ,
  &hSoftKey) == ERROR_SUCCESS)
 {
  DWORD dw;
  if (RegCreateKeyEx(hSoftKey, m_pszRegistryKey, 0, REG_NONE,
   REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
   &hCompanyKey, &dw) == ERROR_SUCCESS)
  {
   RegCreateKeyEx(hCompanyKey, m_pszProfileName, 0, REG_NONE,
    REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
    &hAppKey, &dw);
  }
 }
 if (hSoftKey != NULL)
  RegCloseKey(hSoftKey);
 if (hCompanyKey != NULL)
  RegCloseKey(hCompanyKey);

return hAppKey;
}

// returns key for:
//      HKEY_CURRENT_USER/"Software"/RegistryKey/AppName/lpszSection
// creating it if it doesn't exist.
// responsibility of the caller to call RegCloseKey() on the returned HKEY
HKEY CWinApp::GetSectionKey(LPCTSTR lpszSection)
{
 ASSERT(lpszSection != NULL);

HKEY hSectionKey = NULL;
 HKEY hAppKey = GetAppRegistryKey();
 if (hAppKey == NULL)
  return NULL;

DWORD dw;
 RegCreateKeyEx(hAppKey, lpszSection, 0, REG_NONE,
  REG_OPTION_NON_VOLATILE, KEY_WRITE|KEY_READ, NULL,
  &hSectionKey, &dw);
 RegCloseKey(hAppKey);
 return hSectionKey;
}

UINT CWinApp::GetProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 int nDefault)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 if (m_pszRegistryKey != NULL) // use registry
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return nDefault;
  DWORD dwValue;
  DWORD dwType;
  DWORD dwCount = sizeof(DWORD);
  LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
   (LPBYTE)&dwValue, &dwCount);
  RegCloseKey(hSecKey);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_DWORD);
   ASSERT(dwCount == sizeof(dwValue));
   return (UINT)dwValue;
  }
  return nDefault;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);
  return ::GetPrivateProfileInt(lpszSection, lpszEntry, nDefault,
   m_pszProfileName);
 }
}

CString CWinApp::GetProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 LPCTSTR lpszDefault)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 if (m_pszRegistryKey != NULL)
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return lpszDefault;
  CString strValue;
  DWORD dwType, dwCount;
  LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
   NULL, &dwCount);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_SZ);
   lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
    (LPBYTE)strValue.GetBuffer(dwCount/sizeof(TCHAR)), &dwCount);
   strValue.ReleaseBuffer();
  }
  RegCloseKey(hSecKey);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_SZ);
   return strValue;
  }
  return lpszDefault;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);

if (lpszDefault == NULL)
   lpszDefault = _T(""); // don't pass in NULL
  TCHAR szT[4096];
  DWORD dw = ::GetPrivateProfileString(lpszSection, lpszEntry,
   lpszDefault, szT, _countof(szT), m_pszProfileName);
  ASSERT(dw < 4095);
  return 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;
 if (m_pszRegistryKey != NULL)
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return FALSE;

DWORD dwType, dwCount;
  LONG lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
   NULL, &dwCount);
  *pBytes = dwCount;
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_BINARY);
   *ppData = new BYTE[*pBytes];
   lResult = RegQueryValueEx(hSecKey, (LPTSTR)lpszEntry, NULL, &dwType,
    *ppData, &dwCount);
  }
  RegCloseKey(hSecKey);
  if (lResult == ERROR_SUCCESS)
  {
   ASSERT(dwType == REG_BINARY);
   return TRUE;
  }
  else
  {
   delete [] *ppData;
   *ppData = NULL;
  }
  return FALSE;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);

CString str = GetProfileString(lpszSection, lpszEntry, NULL);
  if (str.IsEmpty())
   return FALSE;
  ASSERT(str.GetLength()%2 == 0);
  INT_PTR nLen = str.GetLength();
  *pBytes = UINT(nLen)/2;
  *ppData = new BYTE[*pBytes];
  for (int i=0;i<nLen;i+=2)
  {
   (*ppData)[i/2] = (BYTE)
    (((str[i+1] - 'A') << 4) + (str[i] - 'A'));
  }
  return TRUE;
 }
}

#ifdef AFX_CORE3_SEG
#pragma code_seg(AFX_CORE3_SEG)
#endif

BOOL CWinApp::WriteProfileInt(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 int nValue)
{
 ASSERT(lpszSection != NULL);
 ASSERT(lpszEntry != NULL);
 if (m_pszRegistryKey != NULL)
 {
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return FALSE;
  LONG lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_DWORD,
   (LPBYTE)&nValue, sizeof(nValue));
  RegCloseKey(hSecKey);
  return lResult == ERROR_SUCCESS;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);

TCHAR szT[16];
  wsprintf(szT, _T("%d"), nValue);
  return ::WritePrivateProfileString(lpszSection, lpszEntry, szT,
   m_pszProfileName);
 }
}

BOOL CWinApp::WriteProfileString(LPCTSTR lpszSection, LPCTSTR lpszEntry,
   LPCTSTR lpszValue)
{
 ASSERT(lpszSection != NULL);
 if (m_pszRegistryKey != NULL)
 {
  LONG lResult;
  if (lpszEntry == NULL) //delete whole section
  {
   HKEY hAppKey = GetAppRegistryKey();
   if (hAppKey == NULL)
    return FALSE;
   lResult = ::RegDeleteKey(hAppKey, lpszSection);
   RegCloseKey(hAppKey);
  }
  else if (lpszValue == NULL)
  {
   HKEY hSecKey = GetSectionKey(lpszSection);
   if (hSecKey == NULL)
    return FALSE;
   // necessary to cast away const below
   lResult = ::RegDeleteValue(hSecKey, (LPTSTR)lpszEntry);
   RegCloseKey(hSecKey);
  }
  else
  {
   HKEY hSecKey = GetSectionKey(lpszSection);
   if (hSecKey == NULL)
    return FALSE;
   lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_SZ,
    (LPBYTE)lpszValue, (lstrlen(lpszValue)+1)*sizeof(TCHAR));
   RegCloseKey(hSecKey);
  }
  return lResult == ERROR_SUCCESS;
 }
 else
 {
  ASSERT(m_pszProfileName != NULL);
  ASSERT(lstrlen(m_pszProfileName) < 4095); // can't read in bigger
  return ::WritePrivateProfileString(lpszSection, lpszEntry, lpszValue,
   m_pszProfileName);
 }
}

BOOL CWinApp::WriteProfileBinary(LPCTSTR lpszSection, LPCTSTR lpszEntry,
 LPBYTE pData, UINT nBytes)
{
 ASSERT(lpszSection != NULL);
 if (m_pszRegistryKey != NULL)
 {
  LONG lResult;
  HKEY hSecKey = GetSectionKey(lpszSection);
  if (hSecKey == NULL)
   return FALSE;
  lResult = RegSetValueEx(hSecKey, lpszEntry, NULL, REG_BINARY,
   pData, nBytes);
  RegCloseKey(hSecKey);
  return lResult == ERROR_SUCCESS;
 }

// convert to string and write out
 LPTSTR lpsz = new TCHAR[nBytes*2+1];
 UINT i;
 for (i = 0; i < nBytes; i++)
 {
  lpsz[i*2] = (TCHAR)((pData[i] & 0x0F) + 'A'); //low nibble
  lpsz[i*2+1] = (TCHAR)(((pData[i] >> 4) & 0x0F) + 'A'); //high nibble
 }
 lpsz[i*2] = 0;

ASSERT(m_pszProfileName != NULL);

BOOL bResult = WriteProfileString(lpszSection, lpszEntry, lpsz);
 delete[] lpsz;
 return bResult;

 

MFC函数—SetRegistryKey的更多相关文章

  1. MFC函数之BitBlt

    MFC函数之BitBlt // Cgame123View 绘制 void Cgame123View::OnDraw(CDC* pDC) { Cgame123Doc* pDoc = GetDocumen ...

  2. 破解 Rith's CrackMe #1(对比IDA查看动态分析中的MFC函数名)

    系统 : Windows xp 程序 : Rith's CrackMe #1 程序下载地址 :http://pan.baidu.com/s/1gecW9Qr 要求 : 注册机编写 使用工具 : IDA ...

  3. MFC函数—CWinApp::LoadStdProfileSettings

    从 InitInstance 成员函数内调用该函数,启用和加载最近使用的(MRU)文件和最后浏览状态的列表.void LoadStdProfileSettings(   UINT nMaxMRU = ...

  4. MFC函数—— CWnd::PreCreateWindow

     CWnd::PreCreateWindow virtual BOOL PreCreateWindow( CREATESTRUCT& cs ); 返回值: 如果要继续窗口的创建过程,则返回非零 ...

  5. 常见MFC函数

    1.MFC常用函数:WinExec()ExitWindowsEx()GlobalMemoryStatus()GetSystemInfo()GetSystemDirectory()GetWindowsD ...

  6. MFC函数—CSingleDocTemplate

    前提:在InitInstance() 函数的初始化过程中,我们可以看到代码CSingleDocTemplate* pDocTemplate; pDocTemplate = new CSingleDoc ...

  7. MFC函数—— CFrameWnd::OnCreateClient

    CFrameWnd::OnCreateClient virtual BOOL OnCreateClient(LPCREATESTRUCT lpcs,CCreateContext*pContext); ...

  8. MFC函数——CWnd::OnEraseBkgnd

    CWnd::OnEraseBkgnd afx_msg BOOL OnEraseBkgnd( CDC* pDC ); 返回值: 如果它擦除了背景,则返回非零值:否则返回0. 参数: pDC 指定了设备环 ...

  9. MFC函数——CWnd::OnCreate

    CWnd::OnCreate afx_msg int OnCreate( LPCREATESTRUCT lpCreateStruct ); 返回值: OnCreate必须返回0以继续CWnd对象的创建 ...

随机推荐

  1. .net core 与ELK(1)安装Elasticsearch

    1.安装java jdk [elsearch@localhost bin]$ java -version openjdk version "1.8.0_181" OpenJDK R ...

  2. wpf 的依赖属性只能在loaded 事件之后才能取到

    wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的  InitializeComponent(); 之后取不到 wpf 的依赖属性只能在loaded 事件之后才能取到,在构造函数的  ...

  3. ViewPager无限滑动

    2016-6-19 前言 View轮播效果在app中很常见,一想到左右滑动的效果就很容易想到使用ViewPager来实现.对于像我们常说的banner这样的效果,具备无限滑动的功能是可以用ViewPa ...

  4. 关于ajax访问webservice查询数据量稍微大一点,就会返回error500的解决方案

    只需要在web.config的configeration节点中增加如下子节点即可: <system.web.extensions>       <scripting>      ...

  5. hashMap tableSizeFor 实现原理

    基于jdk1.8 hashMap实现,要求容量大小是2的整次方,例如:2/4/8/16/32/64/128...,而不能是中间的某个值.这是为什么呢? map是数组+链表的数据结构,读写数据都需要首先 ...

  6. 上台阶问题(递归,DFS)

    题目 一共39层台阶.如果我每一步迈上1个台阶或者两个台阶,先迈左脚,再迈右脚,然后左右交换,最后一步迈右脚,也就是一共要走偶数步,那么,上完39级台阶,有多少种不同的方法? 思路 采用递归的思想,边 ...

  7. Oracle 获取本周、本月、本季、本年的第一天和最后一天

    Oracle 获取本周.本月.本季.本年的第一天和最后一天 --本周 select trunc(sysdate, 'd') + 1 from dual; select trunc(sysdate, ' ...

  8. python面试(3)

    一.语言 推荐一本看过最好的python书籍? 拉开话题好扯淡 谈谈python的装饰器,迭代器,yield? 标准库线程安全的队列是哪一个?不安全的是哪一个?logging是线程安全的吗? pyth ...

  9. ListView中的TextView实现跑马灯效果

    1.TextView首先添加android:ellipsize="marquee"   android:marqueeRepeatLimit="marquee_forev ...

  10. Spring mvc 4系列教程(三)—— Spring4.X的新特性

    1.Spring4.0的新特性 从2004年Spring的1.0发布后,后面又发布了很多重要的版本:Spring2.0提供了XML命令空间和AspectJ的支持:Spring2.5提出了注解驱动(an ...