Borland C++ Builder 提供一個相當方便的 TRegister VCL 元件,方便開發者讀寫系統登錄檔,這個元件的功能比 VB 提供的 RegXXX 系列的函數功能強的多,使用前必須先引入 #include <Registry.hpp> 才能使用 TRegister ,下面是的程式範例是讀取登錄檔中的 Run 和 Runonce 紀錄,透過 callback 方式顯示資料。
// callback
typedef void (*EventListRunValues)(const char* key, const char* value);
typedef void (*EventListRunOnceValues)(const char* key, const char* value);
typedef void (*EventListRunOnceValues)(const char* key, const char* value);
/* 列舉系統開機自動執行(每次)
*
* 參數:
* EventListRunValues: callback 函數,第一個參數為 key 名稱,第二個參數為 key 的值
*
* 備註:開機自動執行的 Key
* HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRun
*/
void listRunKeys(EventListRunValues callback){
AnsiString S;
TRegistry *Registry = new TRegistry();
TStringList* itemSet = new TStringList();
try{
Registry->RootKey = HKEY_CURRENT_USER;
Registry->OpenKey("Software\Microsoft\Windows\CurrentVersion\Run", false);
Registry->GetValueNames(itemSet);
for(int i=0; i<itemSet->Count; ++i){
S = Registry->ReadString(itemSet->Strings[i]);
callback(itemSet->Strings[i].c_str(), S.c_str());
}
}__finally
{
delete itemSet;
delete Registry;
}
}
*
* 參數:
* EventListRunValues: callback 函數,第一個參數為 key 名稱,第二個參數為 key 的值
*
* 備註:開機自動執行的 Key
* HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRun
*/
void listRunKeys(EventListRunValues callback){
AnsiString S;
TRegistry *Registry = new TRegistry();
TStringList* itemSet = new TStringList();
try{
Registry->RootKey = HKEY_CURRENT_USER;
Registry->OpenKey("Software\Microsoft\Windows\CurrentVersion\Run", false);
Registry->GetValueNames(itemSet);
for(int i=0; i<itemSet->Count; ++i){
S = Registry->ReadString(itemSet->Strings[i]);
callback(itemSet->Strings[i].c_str(), S.c_str());
}
}__finally
{
delete itemSet;
delete Registry;
}
}
/* 列舉系統開機自動執行(一次)
*
* 參數:
* EventListRunValues: callback 函數,第一個參數為 key 名稱,第二個參數為 key 的值
*
* 備註:開機自動執行的 Key
* HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRunonce
*/
void listRunOncesKeys(EventListRunOnceValues callback){
AnsiString S;
TRegistry *Registry = new TRegistry();
TStringList* itemSet = new TStringList();
try{
Registry->RootKey = HKEY_CURRENT_USER;
Registry->OpenKey("Software\Microsoft\Windows\CurrentVersion\Runonce", false);
Registry->GetValueNames(itemSet);
for(int i=0; i<itemSet->Count; ++i){
S = Registry->ReadString(itemSet->Strings[i]);
callback(itemSet->Strings[i].c_str(), S.c_str());
}
}__finally
{
delete itemSet;
delete Registry;
}
}
*
* 參數:
* EventListRunValues: callback 函數,第一個參數為 key 名稱,第二個參數為 key 的值
*
* 備註:開機自動執行的 Key
* HKEY_CURRENT_USERSoftwareMicrosoftWindowsCurrentVersionRunonce
*/
void listRunOncesKeys(EventListRunOnceValues callback){
AnsiString S;
TRegistry *Registry = new TRegistry();
TStringList* itemSet = new TStringList();
try{
Registry->RootKey = HKEY_CURRENT_USER;
Registry->OpenKey("Software\Microsoft\Windows\CurrentVersion\Runonce", false);
Registry->GetValueNames(itemSet);
for(int i=0; i<itemSet->Count; ++i){
S = Registry->ReadString(itemSet->Strings[i]);
callback(itemSet->Strings[i].c_str(), S.c_str());
}
}__finally
{
delete itemSet;
delete Registry;
}
}
大大您好, 我有個問題請教一下
我要讀的是COM Port, 所以我的RootKey = HKEY_LOCAL_MACHINE
OpenKey(“HARDWARE\\DEVICEMAP\\SERIALCOMM", false);
我在BCB下Run時可以讀到COM Port並List出來
但是我直接執行Compiler出來的執行檔卻是空的
不知大大是否有遇過這樣的問題………
Thanks.
我是用BCB6 + Win8.1
沒遇過, 會不會是權限問題, 登陸檔存取需要最高權限, 你要不要用最高權限執行編譯產生的的檔案室試試
感謝大大的提醒,我在執行檔用「以系統管理員身分」執行就OK了~~~~~
Thanks~~~~~~