|
发表于 2023-6-1 09:38:53
|
显示全部楼层
本帖最后由 python/print 于 2023-6-1 09:41 编辑
我猜您的意思是开机自启动,代码如下(可以直接使用开机启动执行您的后续操作)
- #include <Windows.h>
- #include <iostream>
- #include <string>
-
- using namespace std;
-
- int main() {
- // 获取当前应用程序的可执行文件路径
- char buffer[MAX_PATH];
- GetModuleFileNameA(NULL, buffer, MAX_PATH);
- string appPath = buffer;
-
- // 打开注册表HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run
- HKEY hKey;
- LONG lResult = RegOpenKeyExA(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);
- if (lResult == ERROR_SUCCESS) {
- // 设置键值为应用程序的可执行文件路径
- lResult = RegSetValueExA(hKey, "MyApp", 0, REG_SZ, (const BYTE*)appPath.c_str(), appPath.size());
- if (lResult == ERROR_SUCCESS) {
- cout << "设置开机启动成功!" << endl;
- } else {
- cout << "设置开机启动失败:" << GetLastError() << endl;
- }
- // 关闭注册表
- RegCloseKey(hKey);
- } else {
- cout << "打开注册表失败:" << GetLastError() << endl;
- }
- return 0;
- }
复制代码
(问题若解决,别忘了设置最佳答案) |
|