开机自启动写入注册表失败

tixisong 2014-12-15 01:14:04

// 程序开机启动写入注册表
bool CSlitMachineApp::WriteKEY()
{
long lRet;
DWORD dwDisposition;
HKEY hAppKey = NULL;
LPCTSTR lpRun = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run");

lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, NULL,
REG_OPTION_NON_VOLATILE,
KEY_WRITE, NULL, &hAppKey, &dwDisposition);

if (lRet == ERROR_SUCCESS)
{
TCHAR pFileName[MAX_PATH] = { 0 };
//得到程序自身的全路径
DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH);
//添加一个子key,并设置值
lRet = RegSetValueEx(hAppKey, _T("DS"), 0, REG_SZ, (BYTE*)pFileName, dwRet );
//关闭注册表
RegCloseKey(hAppKey);
}
return true;
}

RegSetValueEx返回为0,没有错误,但在注册表中没有写进去。
...全文
213 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
tixisong 2014-12-16
  • 打赏
  • 举报
回复
引用 1 楼 lsq19871207 的回复:
你的操作系统是64位,而你的这个程序是Win32位的吧? 32-bit and 64-bit Application Data in the Registry 34 out of 58 rated this helpful - Rate this topic On 64-bit Windows, portions of the registry entries are stored separately for 32-bit application and 64-bit applications and mapped into separate logical registry views using the registry redirector and registry reflection, because the 64-bit version of an application may use different registry keys and values than the 32-bit version. There are also shared registry keys that are not redirected or reflected. The parent of each 64-bit registry node is the Image-Specific Node or ISN. The registry redirector transparently directs an application's registry access to the appropriate ISN subnode. Redirection subnodes in the registry tree are created automatically by the WOW64 component using the name Wow6432Node. As a result, it is essential not to name any registry key you create Wow6432Node. The KEY_WOW64_64KEY and KEY_WOW64_32KEY flags enable explicit access to the 64-bit registry view and the 32-bit view, respectively. For more information, see Accessing an Alternate Registry View. To disable and enable registry reflection for a particular key, use the RegDisableReflectionKey and RegEnableReflectionKey functions. Applications should disable reflection only for the registry keys that they create and not attempt to disable reflection for the predefined keys such as HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. To determine which keys are on the reflection list, use the RegQueryReflectionKey function. 修改: lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hAppKey, &dwDisposition); --------------------------------------------------------------------------------------->

lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, NULL,
        REG_OPTION_NON_VOLATILE, 
        KEY_WRITE|KEY_WOW64_64KEY, NULL, &hAppKey, &dwDisposition);
//or:
//lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpRun,0,KEY_READ|KEY_WRITE|KEY_WOW64_64KEY,&hAppKey);
果然是这样子,结贴
信阳毛尖 2014-12-15
  • 打赏
  • 举报
回复
你的操作系统是64位,而你的这个程序是Win32位的吧? 32-bit and 64-bit Application Data in the Registry 34 out of 58 rated this helpful - Rate this topic On 64-bit Windows, portions of the registry entries are stored separately for 32-bit application and 64-bit applications and mapped into separate logical registry views using the registry redirector and registry reflection, because the 64-bit version of an application may use different registry keys and values than the 32-bit version. There are also shared registry keys that are not redirected or reflected. The parent of each 64-bit registry node is the Image-Specific Node or ISN. The registry redirector transparently directs an application's registry access to the appropriate ISN subnode. Redirection subnodes in the registry tree are created automatically by the WOW64 component using the name Wow6432Node. As a result, it is essential not to name any registry key you create Wow6432Node. The KEY_WOW64_64KEY and KEY_WOW64_32KEY flags enable explicit access to the 64-bit registry view and the 32-bit view, respectively. For more information, see Accessing an Alternate Registry View. To disable and enable registry reflection for a particular key, use the RegDisableReflectionKey and RegEnableReflectionKey functions. Applications should disable reflection only for the registry keys that they create and not attempt to disable reflection for the predefined keys such as HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER. To determine which keys are on the reflection list, use the RegQueryReflectionKey function. 修改: lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hAppKey, &dwDisposition); --------------------------------------------------------------------------------------->

lRet = RegCreateKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, NULL,
        REG_OPTION_NON_VOLATILE, 
        KEY_WRITE|KEY_WOW64_64KEY, NULL, &hAppKey, &dwDisposition);
//or:
//lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE,lpRun,0,KEY_READ|KEY_WRITE|KEY_WOW64_64KEY,&hAppKey);

16,504

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • AIGC Browser
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧