首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

C++关于用注册表修改开机启动项的异常 求大神指点

2013-06-26 
C++关于用注册表修改开机启动项的错误求大神指点啊#include stdafx.h#include Win32Kaijiqidon.hint A

C++关于用注册表修改开机启动项的错误 求大神指点啊
#include "stdafx.h"
#include "Win32Kaijiqidon.h"
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance,
                     _In_opt_ HINSTANCE hPrevInstance,
                     _In_ LPTSTR    lpCmdLine,
                     _In_ int       nCmdShow)
{

TCHAR system[MAX_PATH]; //路径
HMODULE GetModH = GetModuleHandle(NULL);

//得到当前执行文件的全路径
GetModuleFileName(GetModH,system,sizeof(system));
MessageBox(NULL,system,TEXT("提示"),MB_OK);


//写入注册表,以便开机自动运行
HKEY hKey;
char cha[MAX_PATH];

//打开注册表
LONG openRes =RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"),0,KEY_READ,&hKey);
if (openRes==ERROR_SUCCESS) {
       MessageBox(NULL,TEXT("打开"),TEXT("提示"),MB_OK);
    } 
else {
        MessageBox(NULL,TEXT("没有"),TEXT("提示"),MB_OK);
    }
WideCharToMultiByte(CP_ACP,MB_ERR_INVALID_CHARS,system,-1,cha,sizeof(cha),"/0",NULL);
//新增一个值,名称随意命名,值为要开机运行的文件的完整路径  KEY_ALL_ACCESS|KEY_WOW64_64KEY
LONG setRes =RegSetValueEx(hKey,TEXT("xcvb"),0,REG_SZ,(const unsigned char*)cha,sizeof(system));//这里老是出错 哪位大神指教下啊(写不进注册表)
 if (setRes == ERROR_SUCCESS) {
        MessageBox(NULL,TEXT("成功"),TEXT("提示"),MB_OK);
    } 
else {
        MessageBox(NULL,TEXT("失败"),TEXT("提示"),MB_OK);
    }

//关闭注册表:
RegCloseKey(hKey);
MessageBox(NULL,TEXT("开机启动试验!"),TEXT("提示"),MB_OK);
return 0;
}

注:我用的是VS2012 系统是WIN8系统(64位)
[解决办法]
这是一个用法
你的最后2个参数用错了吧

 CString strSubkey;
 CString strValue("0xff");
 strSubkey="Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer";
 HKEY hKey;
 DWORD dwDisposition;
 long info=RegCreateKeyEx(HKEY_CURRENT_USER,strSubkey,0,NULL,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hKey,&dwDisposition);
 if(info!=ERROR_SUCCESS)
 {
  MessageBox("无法正确打开注册表键,请重试!!");
  return;
 }
 info=RegSetValueEx(hKey,"NoDriveTypeAutoRun",0,REG_DWORD,(unsigned char*)(LPCTSTR)strValue,strValue.GetLength());

[解决办法]
http://translate.google.cn/?hl=zh-cn&tab=wT#en/zh-CN/The%20RegCloseKey%20function%20does%20not%20necessarily%20write%20information%20to%20the%20registry%20before%20returning%3B%20it%20can%20take%20as%20much%20as%20several%20seconds%20for%20the%20cache%20to%20be%20flushed%20to%20the%20hard%20disk.%20If%20an%20application%20must%20explicitly%20write%20registry%20information%20to%20the%20hard%20disk%2C%20it%20can%20use%20the%20RegFlushKey%20function.%20RegFlushKey%2C%20however%2C%20uses%20many%20system%20resources%20and%20should%20be%20called%20only%20when%20necessary.


C++关于用注册表修改开机启动项的异常  求大神指点

热点排行