运行这个之后,开机启动会自动运行本身,如果要让指定程序开机自动运行,自己改下路径就OK。
#include <stdio.h>
#include <windows.h>
int main(int argc, char* argv[])
{
HKEY hRoot = HKEY_LOCAL_MACHINE;
char *szSubKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Run";
HKEY hKey;
DWORD dwDisposition = REG_OPENED_EXISTING_KEY;
LONG lRet = ::RegCreateKeyEx(hRoot, szSubKey, 0, NULL,
REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, &dwDisposition);
if(lRet != ERROR_SUCCESS)
return -1;
char szModule[MAX_PATH] ;
::GetModuleFileName (NULL, szModule, MAX_PATH);
lRet = ::RegSetValueEx(hKey, "SelfRunDemo", 0, REG_SZ, (BYTE*)szModule, strlen(szModule));
if(lRet == ERROR_SUCCESS)
{
printf(" 自动启动设置成功!\n");
}
::RegCloseKey(hKey);
getchar();
return 0;
}