/*写入成功 : 参数 :1 返回值:0
*删除成功 : 参数 :2 返回值:0
*查询:参数3 :返回值 :1:存在 2 :不存在
*
int SetBootUp(int i)
{
HKEY hKey;
if (RegOpenKeyEx(HKEY_CURRENT_USER, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) ///打开启动项
{
//2、得到本程序自身的全路径
TCHAR strExeFullDir[MAX_PATH];
GetModuleFileName(NULL, strExeFullDir, MAX_PATH);
//3、判断注册表项是否已经存在
TCHAR strDir[MAX_PATH] = {};
DWORD nLength = MAX_PATH;
long result = RegGetValue(hKey, nullptr, "exe", RRF_RT_REG_SZ, 0, strDir, &nLength);
if(i ==1)
{
if (result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0)
{
RegSetValueEx(hKey, "exe", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1)*sizeof(TCHAR));
RegCloseKey(hKey);
return 0 ;
}
}
if(i ==2)
{
if (result == ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) == 0)
{
//2、删除值
RegDeleteValue(hKey, "exe");
RegCloseKey(hKey);
return 0 ;
}
}
if(i==3)
{
if (result == ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) == 0)
{
RegCloseKey(hKey);
return 1 ;
}
else
{
RegCloseKey(hKey);
return 2;
}
}
}
return 0 ;
}