vc++ 程序开机自启动和取消启动

//开机启动
int  CMainWnd::CreateRun()
{
    //添加以下代码
    HKEY   hKey; 
    char pFileName[MAX_PATH] = {0}; 
    //得到程序自身的全路径 
    DWORD dwRet = GetModuleFileNameW(NULL, (LPWCH)pFileName, MAX_PATH); 
    //找到系统的启动项 
    LPCTSTR lpRun = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); 
    //打开启动项Key 
    long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey); 
    if(lRet== ERROR_SUCCESS)
    {
        //添加注册
        RegSetValueEx(hKey, _T("UilibDemo"), 0,REG_SZ,(const BYTE*)(LPCSTR)pFileName, MAX_PATH);
        RegCloseKey(hKey); 
    }
    return 0;
}
//取消开机启动
int CMainWnd::DeleteRun()
{
    //添加以下代码
    HKEY   hKey; 
    char pFileName[MAX_PATH] = {0}; 
    //得到程序自身的全路径 
    DWORD dwRet = GetModuleFileNameW(NULL, (LPWCH)pFileName, MAX_PATH); 
    //找到系统的启动项 
    LPCTSTR lpRun = _T("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); 
    //打开启动项Key 
    long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey); 
    if(lRet== ERROR_SUCCESS)
    {
        //删除注册
        RegDeleteValue(hKey,_T("UilibDemo"));
        RegCloseKey(hKey); 
    }
    return 0;
}

 可通过命令:regedit 查看注册表中的数据

 

posted @ 2013-07-16 12:40  偶不是大叔  阅读(5861)  评论(0编辑  收藏  举报