MFC 프로그램 켜기 자동 시작 설정

1572 단어 MFC
//        
BOOL AutoBootSet()
{
	HKEY hKey; 
	  
	//           
	CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 

	//     Key   
	long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);   
	if(lRet == ERROR_SUCCESS)   
	{ 
		TCHAR pFileName[MAX_PATH] = {0};
		   
		//             
		DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH); 
		
		_tcscat(pFileName, _T(" -auto"));
		
		//     Key,    ,         (    .exe)  
		lRet = RegSetValueEx(hKey, _T("AppName"), 0, REG_SZ, (LPBYTE)pFileName,  (lstrlen(pFileName) + 1)*sizeof(TCHAR));   
		
		//        
		RegCloseKey(hKey); 
			
		if(lRet != ERROR_SUCCESS)
		{
			return false;
		}
		return true;
	}
	return false;
}


//        
BOOL AutoBootCancel()
{
	HKEY hKey;   
	
	//           
	CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"); 
	
	long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);   
	if(lRet == ERROR_SUCCESS)   
	{
		RegDeleteValue(hKey, _T("AppName"));
		
		//        
		RegCloseKey(hKey);

		return true;  
	}
	return false;
}


//            
BOOL IsAutoBoot()
{
	LPWSTR *szArglist = NULL;
	int nArgs = -1;
	BOOL bRet = FALSE;
	szArglist = CommandLineToArgvW(GetCommandLine(), &nArgs);
	if((NULL == szArglist) || (nArgs == 2))
	{
		if (_tcsicmp(szArglist[1], _T("-auto")) == 0)
		{
			bRet = TRUE;			//    
		}
		else
		{
			bRet = FALSE;
		}
	}
	else
	{
		bRet = FALSE;
	}

	LocalFree(szArglist);
	
	return bRet;
}

좋은 웹페이지 즐겨찾기