开机自动启动
C++builder 写注册表:
#include <registry.hpp>
TRegistry *Reg=new TRegistry;
<span style="white-space:pre"> </span>Reg-> RootKey=HKEY_LOCAL_MACHINE;
<span style="white-space:pre"> </span>if(Reg-> OpenKey( "\\Software\\Microsoft\\Windows\\CurrentVersion\\Run",true))
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>RegShell=Reg->ReadString("BsNavShell");
<span style="white-space:pre"> </span>if(RegShell=="")
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span> if(MessageBox(NULL, "是否将程序设定为开机自动运行 ", "确定 ",MB_YESNO)==IDYES)
<span style="white-space:pre"> </span>{
<span style="white-space:pre"> </span>Reg-> WriteString( "BsNavShell",Application->ExeName);
<span style="white-space:pre"> </span>Reg-> CloseKey();
<span style="white-space:pre"> </span>delete Reg;
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>}
}
API:
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE,TEXT( "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run "),0,KEY_ALL_ACCESS,&hKey) == 0)
{
RegSetValueEx(hKey, "MyApp ",0,REG_SZ, Application-> ExeName, Application-> ExeName.Length());
RegCloseKey(hKey);
}
else
{
ShowMessage( "打开注册表时发生异常错误! ");
}
WIN.INI:
关键在于对Win.ini的读写。Win.ini为WINDOWS启动时初始配置文件,
存放在系统目录下。系统目录可以在注册表HKEY_LOCAL_MACHINE \SOFTWARE\Windows\CurrentVersion下,
通过读取SystemRoot值得到。程序自身的存放路径可以利用C++ Builder3中Application类的ExeName
变量得到。使用C++ Builder3提供的TIniFile类,我们可以十分方便地对INI文件进行操作。程序如下:
TRegister *Test;
TIniFile *IniFile;
Test = new TRegister();
Test-> RootKey = HKEY_LOCAL_MACHINE;
if(Test-> OpenKey( "SOFTWARE\\Windows\\CurrentVersion ",false))
Path = Test-> ReadString( "SystemRoot "); //获得WINDOWS系统目录
delete Test;
Path = Path + "\\Win.ini "; //获得Win.ini的完整路径
IniFile = new TIniFile(Path);
IniFile-> WriteString( "windows ", "run ",Application-> ExeName);
// run = 执行文件名
delete IniFile;
摘自 破空的专栏
补充:软件开发 , C++ ,