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;
摘自 破空的專欄