**
注册表中注册程序实现开机自启动
**
#ifdef WIN32
#include<Windows.h>
#include<string>
#include<stdio.h>
#include<stdlib.h>
#endif
/*
*@bref 在注册表中注册程序实现开机自启动
*/
void ComputerStartRegister(char *path)
{
char *szSubKey = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
int k = RegOpenKeyExA(HKEY_CURRENT_USER, szSubKey, 0, KEY_ALL_ACCESS, &hKey);
if (k == ERROR_SUCCESS)
{
RegSetValueEx(hKey, "APPStart", 0, REG_SZ, (BYTE *)path, strlen(path));
//关闭注册表
RegCloseKey(hKey);
printf_s("APPStart is registered success !!!\n");
}
else
{
printf_s("APPStart is registered failure !!!\n");
}
}
测试代码
int main()
{
#ifdef WIN32
char curpathName[MAX_PATH] = { 0 };
GetCurrentDirectory(MAX_PATH, curpathName);
#ifdef DEBUG
sprintf_s(curpathName, "%s\\", curpathName, sizeof(curpathName));
strcat_s(curpathName, "Startd.exe");
#else
sprintf_s(curpathName, "%s\\", curpathName, sizeof(curpathName));
strcat_s(curpathName, "Start.exe");
#endif
ComputerStartRegister(curpathName);
#endif
return 0;
}