使程序随系统启动 如何做? - C++ Builder / Windows SDK/API
如题!!
[解决办法]
常用得写注册表,或者放到程序-->启动目录等
if(CheckBox1->Checked) { TRegistry *RegistryBoot = new TRegistry(KEY_WRITE); RegistryBoot->RootKey = HKEY_LOCAL_MACHINE; if(RegistryBoot->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",false)) { RegistryBoot->WriteString("Interface",Application->ExeName); RegistryBoot->CloseKey(); RegistryBoot->Free(); } } else { TRegistry *RegistryBoot = new TRegistry(KEY_WRITE); RegistryBoot->RootKey = HKEY_LOCAL_MACHINE; if(RegistryBoot->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",false)) { RegistryBoot->DeleteValue("Interface"); RegistryBoot->CloseKey() ; RegistryBoot->Free(); } }
[解决办法]
//写入注册表,开机自启动 HKEY hKey; //找到系统的启动项 LPCTSTR lpRun = "Software\\Microsoft\\Windows\\CurrentVersion\\Run"; //打开启动项Key long lRet = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpRun, 0, KEY_WRITE, &hKey); if(lRet == ERROR_SUCCESS) { char pFileName[MAX_PATH] = {0}; //得到程序自身的全路径 DWORD dwRet = GetModuleFileName(NULL, pFileName, MAX_PATH); //添加一个子Key,并设置值 // 下面的"getip"是应用程序名字(不加后缀.exe) lRet = RegSetValueEx(hKey, "getip", 0, REG_SZ, (BYTE *)pFileName, dwRet); //关闭注册表 RegCloseKey(hKey); if(lRet != ERROR_SUCCESS) { AfxMessageBox("系统参数错误,不能随系统启动"); } }
[解决办法]
如果是系统登录后自动其中的程序就用写注册表的方式
如果是想在系统进入登录输入密码框的时候 就运行你的程序的话 就需要做个一个NTSrever
//写注册表方式void __fastcall TForm1::Button1Click(TObject *Sender){ TRegistry *Test; Test = new TRegistry (); Test->RootKey = HKEY_LOCAL_MACHINE; if(Test->OpenKey("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",false)) { Test->WriteString("My Program",Application->ExeName); ShowMessage("设置自动启动成功!"); } delete Test;}
[解决办法]
都有三人说话了,我就观望吧!~~