c++builder如何访问注册表

wld2003 2003-03-01 09:28:08
各位老大,c++builder如何访问注册表
...全文
114 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
jemyzhang 2003-04-19
  • 打赏
  • 举报
回复
为什么在98下能修改或添加值,但在2000下却无效呢
qiuafa 2003-04-14
  • 打赏
  • 举报
回复
劫分
xc2927 2003-04-01
  • 打赏
  • 举报
回复
用C++ Builder为计算机增加启动日志





1. 在C++ Builder 5环境中建立新工程文件:执行菜单命令File/New Application,然后将工程文件另存为“Logon.bpr”。

2. 执行菜单命令Project/View Source,打开Logon.cpp文件,将其中的
USEFORM(“Unit1.cpp”, Form1);

Application->CreateForm(__classid(Tform1), &Form1);
两条语句删除,目的是创建无窗体程序。

3. 文件Logon.cpp的全部代码如下:
#include
#include //添加行
#pragma hdrstop
USERES("logon.res");
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
try
{ //添加部分开始
int num=0;
TRegistry *Registry = new TRegistry; //创建注册表堆对象指针
Registry->RootKey = HKEY_USERS; //定位注册表根键
if(!Registry->OpenKey(".DEFAULT\\Logon\\Records", false))
{ //如果打开主键失败
Registry->CreateKey(".DEFAULT\\Logon\\Records"); //创建主键
if(!(Registry->OpenKey(".DEFAULT\\Logon\\Records", false)))
return 0;
else
{
TRegistry *Reg = new TRegistry; // ①
Reg->RootKey = HKEY_LOCAL_MACHINE;
Reg->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",false);
Reg->WriteString("Logon",Application->ExeName); //在注册表中注册本程序
Reg->CloseKey();
delete Reg;
Application->MessageBox("登录程序注册成功!","结果",MB_OK); // ②
Registry->WriteInteger("开机次数",num);
Registry->WriteString("程序注册时间",Date()+Time());
return 0;
}
}
else
{
num = Registry->ReadInteger("开机次数"); // ③
Registry->WriteInteger("开机次数",num+1); //记录开机次数
Registry->WriteString("第"+AnsiString(num+1)+"次开机",Date()+Time()); //记录开机时间
}
Registry->CloseKey();
delete Registry;
//添加部分结束
Application->Initialize();
Application->Run();
}
catch (Exception &exception)
{
Application->ShowException(&exception);
}
return 0;
}
其中从语句①到②的程序段将在注册表中产生如的效果。




qiuafa 2003-03-31
  • 打赏
  • 举报
回复
接分
  • 打赏
  • 举报
回复
如何搜索注册表?感兴趣的朋友进来看看



用递归!!
这是以前写的Delphi代码,

function registryWriteAGroup(iRootKey :HKEY; strKeyName : string; var flReg : TextFile) : integer;
var
Reg:TRegistry;
// keyInfo:TRegKeyInfo;
DataInfo:TRegDataInfo;

i:integer;
strRootKey:string;

stsKeyValue:TStringList;
begin
case iRootKey of
HKEY_CLASSES_ROOT: strRootKey:='HKEY_CLASSES_ROOT';
HKEY_CURRENT_USER: strRootKey:='HKEY_CURRENT_USER';
HKEY_LOCAL_MACHINE: strRootKey:='HKEY_LOCAL_MACHINE';
HKEY_USERS: strRootKey:='HKEY_USERS';
HKEY_PERFORMANCE_DATA: strRootKey:='HKEY_PERFORMANCE_DATA';
HKEY_CURRENT_CONFIG: strRootKey:='HKEY_CURRENT_CONFIG';
HKEY_DYN_DATA: strRootKey:='HKEY_DYN_DATA';
end;

try
writeln(flReg,''); // write a seperate line
Writeln(flReg,'['+strRootKey+strKeyName+']'); // write position information
except
result :=-1;
exit;
end;

Reg := TRegistry.Create;
stsKeyValue:=TStringList.Create;
result :=0;
try
// open key
Reg.RootKey:=iRootKey;
Reg.Access:=KEY_READ;
Reg.OpenKey(strKeyName,False);

// get values
Reg.GetValueNames(stsKeyValue);
for i:=0 to stsKeyValue.Count-1 Do begin
Reg.GetDataInfo(stsKeyValue.Strings[i],DataInfo);
case DataInfo.RegData of
rdString:
Writeln(flReg,'"'+stsKeyValue.Strings[i]+'"="'+
CStyleString(Reg.ReadString(stsKeyValue.Strings[i]))+'"');
rdExpandString:
Writeln(flReg,'"'+stsKeyValue.Strings[i]+'"="'+
CStyleString(Reg.ReadString(stsKeyValue.Strings[i]))+'"');
rdInteger:
Writeln(flReg,'"'+stsKeyValue.Strings[i]+'"=dword:'+
intToHex(Reg.ReadInteger(stsKeyValue.Strings[i]),8));
// other type are ignored
end;
end;

// get sub keys
Reg.GetKeyNames(stsKeyValue);
for i:=0 to stsKeyValue.Count-1 do begin
if registryWriteAGroup(iRootKey,
strKeyName +'\'+stsKeyValue.Strings[i],flReg) <>0 then
result :=-1;
end;

finally
Reg.CloseKey;
Reg.Free;
stsKeyValue.Free;
end;
end;




  • 打赏
  • 举报
回复
#inculde<registry.hpp>

void __fastcall SelfRoot(const AnsiString Name)
{
TRegistry * SelfReg=new TRegistry();
SelfReg->RootKey=HKEY_LOCAL_MACHINE ;
if(SelfReg->KeyExists("Software\\Microsoft\\Windows\\CurrentVersion\\Run"))
{
if(SelfReg->OpenKey("Software\\Microsoft\\Windows\\CurrentVersion\\Run",true))
{
if(SelfReg->ValueExists(Name)&&SelfReg->ReadString(Name)==ExtractFilePath(Application->ExeName))
{
delete SelfReg;
return;
}
else
{
SelfReg->WriteString(Name,Application->ExeName);
}
}
}
delete SelfReg;
}
牧牛人软件 2003-03-11
  • 打赏
  • 举报
回复
与注册表无关!
是我的程序。
“MainForm->RegistryStat=0”对我的程序来说是未注册。
xyz16 2003-03-11
  • 打赏
  • 举报
回复
MainForm->RegistryStat=0;是什么意思呀?为什么要?
vcebook 2003-03-05
  • 打赏
  • 举报
回复
请教直接的WINDOWS API写注册表的函数???
pp616 2003-03-02
  • 打赏
  • 举报
回复
有点累了。7点过了。该去睡觉了。
gary_jojo 2003-03-02
  • 打赏
  • 举报
回复
查一下HELP里的TRegistry类吧,里面有OPENKEY,CLOSEKEY,READSTRING,WRITESTRING,GETDATAINFO,CREATEKEY,DELETEKEY很多方法。
Sammo 2003-03-01
  • 打赏
  • 举报
回复
我来迟了!:)
Sammo 2003-03-01
  • 打赏
  • 举报
回复
#include <Registry.hpp>
TRegistry* Reg= new TRegistry;
try
{
Reg->RootKey=HKEY_CURRENT_USER;
if(Reg->OpenKey("\\Software\\MySoft",FALSE)==FALSE)
{
Reg->CreateKey("\\Software\\MySoft");
Reg->OpenKey("\\Software\\MySoft",true);
Reg->WriteInteger("MyKey",123);
}
}
__finally
{
Reg->CloseKey();
delete Reg;
}
牧牛人软件 2003-03-01
  • 打赏
  • 举报
回复
#include <registry.hpp>

读:


AnsiString Sno, Year;
int ret;

MainForm->RegistryStat=0;
const String RegKey = "\\SoftWare\\WGHSoftware\\School";
TRegistry *MyRegistry = new TRegistry();
MyRegistry->RootKey = HKEY_LOCAL_MACHINE;
try {
if(MyRegistry->OpenKey(RegKey, false)) {
RegCode=MyRegistry->ReadString("SN");
Unit=MyRegistry->ReadString("UnitName");
SysName=MyRegistry->ReadString("SystemName");
SysCaption=MyRegistry->ReadString("SystemCaption");
MyRegistry->CloseKey();
}
}

catch(ERegistryException &E) {
ShowMessage(E.Message);
delete MyRegistry;
return;
}



写:


const String RegKey = "\\SoftWare\\WGHSoftware\\School";
TRegistry *MyRegistry = new TRegistry();
MyRegistry->RootKey = HKEY_LOCAL_MACHINE;
try {
if(MyRegistry->OpenKey(RegKey, true)) {
MyRegistry->WriteString("UnitName",UnitEdit->Text);
MyRegistry->WriteString("SN",RegCodeEdit->Text);
MyRegistry->WriteString("SystemName",SysNameEdit->Text);
MyRegistry->CloseKey();
}
}
catch(ERegistryException &E) {
ShowMessage(E.Message);
delete MyRegistry;
return;
}
delete MyRegistry;
ShowMessage("注册成功!");
Label1->Caption="已注册";

13,825

社区成员

发帖
与我相关
我的任务
社区描述
C++ Builder相关内容讨论区
社区管理员
  • 基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧