SetRegistryKey(_T("Local AppWizard-Generated Applications")); 和 LoadStdProfileSettings

AI权益加码!Claude Code、Cursor等20+工具免费用! 购周边限时加赠Coding Plan Lite,畅享主流AI工具!学习进阶更高效! 阅读详情

 

可以不要,不过会损失一点功能。如不能使用最近打开的文件列表功能。

还有不能保存窗口的状态(最大话,正常打开等等)

CWinApp::SetRegistryKey

  void SetRegistryKey( LPCTSTR lpszRegistryKey );

  void SetRegistryKey( UINT nIDRegistryKey );

  参数

  lpszRegistryKey

  字符串指针,包含了键的名字。

  nIDRegistryKey

  注册表中键的ID/索引。

  注释

  这个函数将应用程序的设置保存在注册表而不是INI文件中。这个函数设置m_pszRegistry Key,它被CWinApp的成员函数GetProfileInt,GetProfileString,WriteProfileInt和WriteProfileString使用。如果调用了这个函数,最近使用(MRU)的文件也被保存到注册表中。通常注册表的键为公司的名字。它保存在如下形式的键中:HKEY_CURRENT_USER/Software/<公司名>/<应用程序名>/<section name>/<value name>.

  CWinApp Overview | Class Members | Hierarchy Chart

  参见 CWinApp::InitInstance, CWinApp::GetProfileInt, CWinApp::GetProfileString, CWinApp::WriteProfileInt, CWinApp::WriteProfileString

 

在利用mfc框架的时候,在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("字符串XXX"),不知道究竟有何用处,这天仔细查看了一下,发现如果你使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。

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

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

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

WriteProfileBinaryWrites binary data to an entry in the application's .INI file.
WriteProfileIntWrites an integer to an entry in the application's .INI file.
WriteProfileStringWrites a string to an entry in the application's .INI file.

GetProfileBinaryRetrieves binary data from an entry in the application's .INI file.
GetProfileIntRetrieves an integer from an entry in the application's .INI file.
GetProfileStringRetrieves a string from an entry in the application's .INI file.

MSDN上面写上面6个函数是写到INI文件的。所以俺就忽略了其访问注册表的功能。无意中看了其MFC实现才有所了解。

例子如下:
SetRegistryKey(_T("boli's app")); //这里是准备在注册表HKEY_CURRENT_USER//software 下面生成一个boli's app 分支~为什么说是准备呢?因为如果不调用相关函数,如上面提到的6个函数,它是不会真正读写注册表的。具体本文最最下面的MFC实现摘录。
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中CWinApp配置管理:从GetProfileInt到SetRegistryKey的实战解析 本文深入解析MFC中CWinApp类的配置管理机制,重点探讨SetRegistryKey函数如何作为核心开关,在Windows注册表与INI文件两种存储模式间切换。通过对比分析两种模式的路径、权限及适用场景,并结合GetProfileInt、WriteProfileString等实战方法,详细演示了如何高效、安全地保存与恢复应用程序配置,如窗口状态。文章还提供了进阶技巧与避坑指南,帮助开发者根据项目需求选择最佳配置管理策略。 阅读详情

相关推荐

SetRegistryKey

SetRegistryKey 这个函数功能是设置MFC程序的注册表访问键,并把读写 ini 文件的成员函数映射到读写注册表。只要调用一下 SetRegistryKey 并指定注册表键值,那么下面6个成员函数,就被映射到进行注册表读取了~ WriteProfileBinary Writes binary data to an entry in the application'

yangmeng900816的专栏 1247

SetRegistryKey函数

MFC的CWinApp类的启动成员函数中,会一个SetRegistryKey函数,他的作用是在程序启动时读写注册表数据 SetRegistryKey(_T(""));//这一句必须有,否则下面语句无效,创建一个主键,可以为空,即以应用程序名为主键 CString a=_T("hello"); WriteProfileString(_T("Login"),_T("UserName"),a)

chinayu2007的专栏 2254

MFC中,设置配置文件(SetRegistryKey),读取配置文件,写入配置文件,代码举例

【代码】MFC中,设置配置文件(SetRegistryKey),读取配置文件,写入配置文件,代码举例。

weixin_49146002的博客 656

MFC函数—SetRegistryKey

前言:在用mfc框架编写应用的时候,如果注意,你会发现在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("String"); 这是什么函数呢,设置记录字符串,不明觉厉,于是用心去查了一下,分享给大家。 总起:其实很明了,如果你的应用需要使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。 ...

weixin_30788239的博客 506

MFC常见函数详解:SetRegistryKey

在利用mfc框架的时候,在App应用类的InitInstance()函数中,初始化时总有一个 SetRegistryKey("字符串XXX"),不知道究竟有何用处,这天仔细查看了一下,发现如果你使用注册表,则它为你提供了很便利的方法,当然如果不使用系统注册表,这句可以注释掉。 来自官方的解释: SetRegistryKey             Causes applicatio

wanglei9876开发人生 2062

SetRegistryKey的作用

使用CWinApp类的WriteProfileString 、GetProfileString等函数。 1、如果不调用SetRegistryKey(),CWinApp 会把信息保存系统目录的的Test.ini文件中(C:\Windows\Test.ini) 2、如果调用SetRegistryKey(),CWinApp 会把信息保存系统目录的的注册表中    HKEY_CURRENT_USER

wzk456的专栏 910

SetRegistrykey的作用

在利用MFC框架的时候,在应用程序应用类的的InitInstance()函数中,初始化时总有一个 以下是我在网上找到的一些资料: 过SetRegistrykey 导致应用程序设置,而不是存储在注册表中。INI文件。 名SetRegistryKey这个函数功能是设置MFC程序的注册表访问键,并把读写INI文件的成员函数映射到读写注册表。只要调用

农村的我的专栏 3050

SetRegistryKey作用

使用CWinApp类的WriteProfileString 、GetProfileString等函数。 1、如果不调用SetRegistryKey(),CWinApp 会把信息保存系统目录的的tool.ini文件中(C:\Windows\tool.ini) 2、如果调用SetRegistryKey(),CWinApp 会把信息保存系统目录的的注册表中 HKEY_CURRENT_USER\Software<company name>\Test<section name><valu

COMAC_919的博客 280

WriteProfileInt

CWinApp类中提供了一组用于读写应用程序配置的方法: GetProfileInt WriteProfileInt GetProfileString WriteProfileString 可方便的用于读写应用程序配置。 1.关于CWinApp::SetRegistryKey方法 用VC++的向导建立MFC项目之后,在InitInstance中可以看到

I am a coder 5747

MFC操作注册表的两种方法(方法二超简单 )

操作注册表的两种方法实现 方法一:一般操作注册表的方法 注册表内部结构: key-&gt;subkey-&gt;value-&gt;名称,类型,数据 函数: RegCreateKey() RegCreateKeyEx() RegOpenKey() RegOpenKeyEx() RegSetValue() RegSetValueEx() RegQueryValue() Reg...

c1learning的博客 4784

MFC中DockPane初始化停靠换乱问题解决办法

MFC中DockPane初始化停靠换乱问题解决办法 ———————————————————————————— 问题的提出:最近在做界面过程中遇到要使用MFC中的停靠窗口,发现停靠窗口创建时候定义的停靠位置一直没有作用,停靠窗口停靠的非常换乱,在网上找了很久,终于找到了一个解决办法。 解决办法如下:  1、在APP类中的InitInstance()函数中有SetRegistryKe...

weixin_30340745的博客 307

取消 CDockablePane 自动保存界面配置到注册表

int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct) { ...... // 已创建 Outlook 栏,应允许在左侧停靠。 EnableDocking(CBRS_ALIGN_LEFT); EnableAutoHidePanes(CBRS_ALIGN_RIGHT); if(!m_testDockpane.Create(_

mowwwcom的专栏 2522

MFC Wizard创建的空应用程序中各个文件内容的解析

创建的MFC应用程序名为:wd,那么: 一、wd.h解析 // wd.h : main header file for the WD application // #if !defined(AFX_WD_H__89BE48D2_F377_4DF1_8C44_4D7372A61CE0__INCLUDED_) #define AFX_WD_H__89BE48D2_F377_4DF1...

weixin_34290000的博客 249

安天系统安全管理软件之便携化

安天管理软件是一个比较好的系统安全工具(不过现在的新版满界面都是广告,哈哈) 他的1.015版还是不错的,首次运行界面如下 但是当你某一天再打开时,会发现右边会出现一个广告条,即使用exescope等去除相应图片资源,广告位置也会显示一个非常碍眼的灰色浮动条 今天我们的

kiuudavidgong的专栏 879

对注册表文件进行操作

一、向注册表文件中写信息:拷一个字符串到Win.ini文件当中,它仅用于16位操作系统当中. 平台SDK中的函数: WriteProfileString(  LPCTSTR lpAppName, LPCTSTR lpKeyName, LPCTSTR lpString ); 它需要在CFileApp中的Initstance()函数中完成,这个函数是应用程序初始化时需要调用的一个虚函数,

dyz_123456的博客 487
上一篇: SAFEARRAY、COleSafeArray、VARTYPE(转)
下一篇: 如何修改CJlibrary608在VC.net环境下运行(转)
ydr001ml
博客等级 码龄17年 3粉丝 1原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值