C语言实现应用程序开机自启动(写进注册表)

时间:2022-09-05 07:52:55
#include <windows.h>
#include <stdio.h>

int main()
{
//找到系统的启动项
char *szSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
char *szModule ="C:\\Users\\hello\\AppData\\Local\\Temp\\test.exe";
HKEY hKey;

//打开注册表启动项
if(RegOpenKeyEx(HKEY_CURRENT_USER, szSubKey, 0, KEY_ALL_ACCESS, &hKey)== ERROR_SUCCESS)
{
//添加一个子Key,并设置值,"Mytest"并不一定是应用程序名字(不加后缀.exe) ,可以自己设置;
RegSetValueEx(hKey, "Mytest", 0, REG_SZ, (BYTE *)szModule, strlen(szModule));
//关闭注册表
RegCloseKey(hKey);
}
else
{
return -1;
}
return 0;
}