设置开机启动代码如下:
int SetAutoRun()
{
SavePassword();
//DelAutoRun();
HKEY hRoot = HKEY_LOCAL_MACHINE;
wchar_t *szSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
wchar_t *szModule=GetPath(); //获取程序的绝对路径
wchar_t szModule2[MAX_PATH];
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)
{
printf("查找注册表失败!错误代码:%d\n",lRet);
return -1;
}
wsprintf(szModule2,L"\"%ls\"",szModule);
wcscat(szModule2,L" -a"); //加参数
lRet = RegSetValueEx(hKey,L"Connection", 0, REG_SZ, (BYTE *)szModule2, 2*wcslen(szModule2));
RegCloseKey(hKey);
if(lRet != ERROR_SUCCESS)
{
printf("注册表写入失败!错误代码:%d\n",lRet);
return -1;
}
return 0;
}
int SetAutoRun()
{
SavePassword();
//DelAutoRun();
HKEY hRoot = HKEY_LOCAL_MACHINE;
wchar_t *szSubKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
wchar_t *szModule=GetPath(); //获取程序的绝对路径
wchar_t szModule2[MAX_PATH];
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)
{
printf("查找注册表失败!错误代码:%d\n",lRet);
return -1;
}
wsprintf(szModule2,L"\"%ls\"",szModule);
wcscat(szModule2,L" -a"); //加参数
lRet = RegSetValueEx(hKey,L"Connection", 0, REG_SZ, (BYTE *)szModule2, 2*wcslen(szModule2));
RegCloseKey(hKey);
if(lRet != ERROR_SUCCESS)
{
printf("注册表写入失败!错误代码:%d\n",lRet);
return -1;
}
return 0;
}