一、注册表位置
系统范围的开机自启动程序
当前用户的开机自启动程序
二、配置步骤
三、注意事项
四、其他方法
任务计划程序
启动文件夹
1. 创建程序快捷方式
2. 打开 Startup 文件夹
3. 将快捷方式移动到 Startup 文件夹
4. 验证程序是否自动启动
注意事项
五、开机自启动程序配置示例代码
在Windows操作系统中,注册表是一个关键的数据库,它存储了系统和应用程序的设置和选项。设置开机自启动程序通常涉及到对注册表的修改。以下是一些关键点和注意事项。
路径:HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
此路径下的所有键值对都指向将在系统启动时运行的程序。这个位置适用于所有用户。
HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
路径和文件名
权限
HKEY_LOCAL_MACHINE
时,通常需要管理员权限。确保你有合适的权限来进行这些操作。安全性
性能
除了使用注册表,还可以通过以下方法配置开机自启动程序:
使用Windows自带的任务计划程序,可以创建在登录时启动的任务。
按下 Win + R
,输入 taskschd.msc
,按回车。
schtasks /create /tn "MyProgramStartup" /tr "C:\Path\To\YourProgram.exe" /sc onlogon /rl highest
windows C++ 注册登录计划任务,用于在Windows中使用Task Scheduler COM接口创建一个在用户登录时启动的计划任务:
#include <iostream>
#include <comdef.h>
#include <taskschd.h>
#pragma comment(lib, "taskschd.lib")
#pragma comment(lib, "comsupp.lib")
void CreateTaskSchedulerTask() {
// 初始化COM库,以支持多线程
HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
if (FAILED(hr)) {
std::cerr << "CoInitializeEx failed: " << hr << std::endl;
return;
}
// 设置COM安全级别
hr = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_PKT_PRIVACY,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
0,
NULL);
if (FAILED(hr)) {
std::cerr << "CoInitializeSecurity failed: " << hr << std::endl;
CoUninitialize();
return;
}
// 创建Task Service实例
ITaskService* pService = NULL;
hr = CoCreateInstance(CLSID_TaskScheduler, NULL, CLSCTX_INPROC_SERVER, IID_ITaskService, (void**)&pService);
if (FAILED(hr)) {
std::cerr << "CoCreateInstance failed: " << hr << std::endl;
CoUninitialize();
return;
}
// 连接到Task Service
hr = pService->Connect(_variant_t(), _variant_t(), _variant_t(), _variant_t());
if (FAILED(hr)) {
std::cerr << "ITaskService::Connect failed: " << hr << std::endl;
pService->Release();
CoUninitialize();
return;
}
// 获取根任务文件夹
ITaskFolder* pRootFolder = NULL;
hr = pService->GetFolder(_bstr_t(L"\\"), &pRootFolder);
if (FAILED(hr)) {
std::cerr << "Cannot get Root Folder pointer: " << hr << std::endl;
pService->Release();
CoUninitialize();
return;
}
// 创建任务定义
ITaskDefinition* pTask = NULL;
hr = pService->NewTask(0, &pTask);
pService->Release(); // 释放服务指针,因为它不再需要
if (FAILED(hr)) {
std::cerr << "Failed to create task definition: " << hr << std::endl;
pRootFolder->Release();
CoUninitialize();
return;
}
// 获取任务的注册信息接口,用于设置任务的标识
IRegistrationInfo* pRegInfo = NULL;
hr = pTask->get_RegistrationInfo(&pRegInfo);
if (FAILED(hr)) {
std::cerr << "Cannot get identification pointer: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
pRegInfo->put_Author(L"Author Name"); // 设置任务的作者信息
pRegInfo->Release();
// 获取触发器集合
ITriggerCollection* pTriggerCollection = NULL;
hr = pTask->get_Triggers(&pTriggerCollection);
if (FAILED(hr)) {
std::cerr << "Cannot get trigger collection: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// 创建登录触发器
ITrigger* pTrigger = NULL;
hr = pTriggerCollection->Create(TASK_TRIGGER_LOGON, &pTrigger);
pTriggerCollection->Release();
if (FAILED(hr)) {
std::cerr << "Cannot create trigger: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// 获取ILogonTrigger接口
ILogonTrigger* pLogonTrigger = NULL;
hr = pTrigger->QueryInterface(IID_ILogonTrigger, (void**)&pLogonTrigger);
pTrigger->Release();
if (FAILED(hr)) {
std::cerr << "QueryInterface call failed for ILogonTrigger: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
pLogonTrigger->put_Id(_bstr_t(L"Trigger1")); // 设置触发器ID
pLogonTrigger->Release();
// 获取操作集合
IActionCollection* pActionCollection = NULL;
hr = pTask->get_Actions(&pActionCollection);
if (FAILED(hr)) {
std::cerr << "Cannot get action collection: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// 创建执行操作
IAction* pAction = NULL;
hr = pActionCollection->Create(TASK_ACTION_EXEC, &pAction);
pActionCollection->Release();
if (FAILED(hr)) {
std::cerr << "Cannot create action: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// 获取IExecAction接口
IExecAction* pExecAction = NULL;
hr = pAction->QueryInterface(IID_IExecAction, (void**)&pExecAction);
pAction->Release();
if (FAILED(hr)) {
std::cerr << "QueryInterface call failed for IExecAction: " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
// 设置要执行的程序路径
pExecAction->put_Path(_bstr_t(L"C:\\Path\\To\\YourProgram.exe"));
pExecAction->Release();
// 注册任务
IRegisteredTask* pRegisteredTask = NULL;
hr = pRootFolder->RegisterTaskDefinition(
_bstr_t(L"MyProgramStartup"),
pTask,
TASK_CREATE_OR_UPDATE,
_variant_t(L""),
_variant_t(L""),
TASK_LOGON_INTERACTIVE_TOKEN,
_variant_t(L""),
&pRegisteredTask);
if (FAILED(hr)) {
std::cerr << "Error saving the Task : " << hr << std::endl;
pRootFolder->Release();
pTask->Release();
CoUninitialize();
return;
}
std::cout << "Task successfully registered." << std::endl;
pRootFolder->Release();
pTask->Release();
pRegisteredTask->Release();
CoUninitialize();
}
int main() {
CreateTaskSchedulerTask();
return 0;
}
XML配置计划任务:
将程序快捷方式放置在 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
文件夹中。
将程序快捷方式放置在 C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
文件夹中,您可以确保该程序在每次启动 Windows 时自动运行。以下是详细的步骤:
如果您已经有程序的快捷方式,请跳过此步骤。
.exe
结尾)。Win + R
键以打开“运行”对话框。C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Startup
并按 Enter 键。这将打开 Startup 文件夹。C:\ProgramData
文件夹时。通过上述步骤,您可以轻松地将任何程序设置为开机自动启动。
#include <windows.h>
#include <iostream>
bool AddToStartup(const wchar_t* appName, const wchar_t* appPath, const wchar_t* appArgs) {
HKEY hKey;
LONG lResult;
const wchar_t* runKeyPath = L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
// 创建包含程序路径和参数的完整命令行
std::wstring command = L"\"";
command += appPath;
command += L"\" ";
command += appArgs;
// 打开注册表项
lResult = RegOpenKeyEx(HKEY_CURRENT_USER, runKeyPath, 0, KEY_WRITE, &hKey);
if (lResult != ERROR_SUCCESS) {
std::wcerr << L"Failed to open registry key: " << lResult << std::endl;
return false;
}
// 设置注册表值
lResult = RegSetValueEx(hKey, appName, 0, REG_SZ, (const BYTE*)command.c_str(), (command.size() + 1) * sizeof(wchar_t));
if (lResult != ERROR_SUCCESS) {
std::wcerr << L"Failed to set registry value: " << lResult << std::endl;
RegCloseKey(hKey);
return false;
}
// 关闭注册表项
RegCloseKey(hKey);
return true;
}
int main() {
const wchar_t* appName = L"MyApp";
const wchar_t* appPath = L"C:\\Path\\To\\YourApp.exe";
const wchar_t* appArgs = L"--example-arg";
if (AddToStartup(appName, appPath, appArgs)) {
std::wcout << L"Successfully added to startup." << std::endl;
} else {
std::wcerr << L"Failed to add to startup." << std::endl;
}
return 0;
}