- //开机启动
- 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;
- }
二》
- void CClientDlg::SetAutoRun(BOOL bAutoRun)
- {
- HKEY hKey;
- CString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";//找到系统的启动项
- if (bAutoRun)
- {
- if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS) //打开启动项
- {
- TCHAR szModule[_MAX_PATH];
- GetModuleFileName(NULL, szModule, _MAX_PATH);//得到本程序自身的全路径
- RegSetValueEx(hKey,"Client", 0, REG_SZ, (const BYTE*)(LPCSTR)szModule, strlen(szModule)); //添加一个子Key,并设置值,"Client"是应用程序名字(不加后缀.exe)
- RegCloseKey(hKey); //关闭注册表
- }
- else
- {
- AfxMessageBox("系统参数错误,不能随系统启动");
- }
- }
- else
- {
- if (RegOpenKeyEx(HKEY_CURRENT_USER, strRegPath, 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS)
- {
- RegDeleteValue (hKey,"Client");
- RegCloseKey(hKey);
- }
- }
- }
- BOOL CClientDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Add "About..." menu item to system menu.
- .
- .
- .
- SetAutoRun(TRUE);//设置开机自动启动
- return TRUE;
- }