0
点赞
收藏
分享

微信扫一扫

Window开机自启动操作注册表

背景浅知识

0:在VS链接器->清单文件->UAC执行级别 可以设置应用程序的启动权限

1:使用mklink指令在window的cmd中创建软链接与创建快捷方式是不同的;

2:在system(“”),中使用cmd \K 能保证窗口不一闪而过,但会阻塞Window窗口程序

3:在win10x64和win7x64在OS:\window\system32路径下用VS创建软连接,生成的文件会出现在OS:\window\sysWOW64中;

4:使用GetSystemDirectory函数获取OS:\window\system32将该目录作为PathFileExists的参数能找到对应OS:\window\sysWOW64目录下文件名相同的文件,PathFileExists函如使用OS:\window\system32路径将被强制替换成OS:\window\sysWOW64路径;即使在CMD命令行中也是同样的效果,OS:\window\system32正常情况是操作系统一个特殊的目录,当在OS:\window\system32目录下创建一个文件夹可成功访问该文件夹

5:如果用RegSetValueEx目设置自启动,system32和sysWOW64目录必须要正确;

6:保证文件放在OS:\window\system32目录下可以使用system(“move oldfile newfile”);

      CString strPath = CString("C:\\Windows\\SysWOW64\\filename.exe");
CString strSubKey = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");//HKEY_LOCAL_MACHINE系统注册表自启动固定位置
char sPath[MAX_PATH] = "";
char sSys[MAX_PATH] = "";
std::string strExe = "\\RemoteCtrl.exe ";
GetCurrentDirectoryA(MAX_PATH, sPath);//获取可执行文件的路径
GetSystemDirectoryA(sSys, sizeof(sSys));//在系统盘:\\Windows\\System32目录下创建软连接
std::string strCmd = "mklink "+std::string(sSys)+ strExe + sPath+ strExe;
system(strCmd.c_str());
HKEY hKey = NULL;
ret = RegOpenKeyEx(HKEY_LOCAL_MACHINE, strSubKey, 0, KEY_ALL_ACCESS|KEY_WOW64_64KEY, &hKey);
if (ret != ERROR_SUCCESS) {
RegCloseKey(hKey);
MessageBox(NULL, _T("设置自动开机启动失败!是否权限不足"), _T("错误"), MB_ICONERROR | MB_TOPMOST);
::exit(0);
}
TCHAR sSysPath[MAX_PATH] =_T("");
GetSystemDirectoryW(sSysPath, MAX_PATH);
int ret = RegSetValueEx(hKey, _T("RemoteCtrl"), 0, REG_EXPAND_SZ, (BYTE*)(LPCTSTR)strPath, strPath.GetLength()*sizeof(TCHAR));
if (ret != ERROR_SUCCESS) {
RegCloseKey(hKey);
MessageBox(NULL, _T("设置自动开机启动失败!是否权限不足"), _T("错误"), MB_ICONERROR | MB_TOPMOST);
::exit(0);
}
RegCloseKey(hKey);
举报

相关推荐

0 条评论