写注册表(100分)

  • 主题发起人 主题发起人 D影子D
  • 开始时间 开始时间
D

D影子D

Unregistered / Unconfirmed
GUEST, unregistred user!
在某个键值(Software/Microsoft/Windows/CurrentVersion/Run)下写新值,
如"ok" REG_SZ "c:/1.exe"
 
举例
uses Registry;
var myreg : treginifile;
dbname,vodkey:string;
begin

myreg := treginifile.create;
myreg.rootkey := HKEY_LOCAL_MACHINE;
vodkey:='Software/VOD';
DataBase1.Connected := false;
if not myreg.KeyExists(vodkey) then
begin
OpenDlg1.Title := '请选择数据库文件';
if OpenDlg1.Execute then
DBname:=OpenDlg1.FileName;
//选择数据库文件
if trim(DBname)='' then
close;
myreg.WriteString('Software/VOD','DBname',DBname);
DataBase1.DatabaseName := DBname;
end
else
begin
DBname:= myreg.readString(vodkey,'DBname','invalid');
if DBname<>'invalid' then
DataBase1.DatabaseName := DBname;
end;
DataBase1.Connected := true;
end;
改成
myreg.WriteString('Software/Microsoft/Windows/CurrentVersion/Run','DBname',DBname);
 
不好意思,我问的是vc的,题目已经说明.
 
LONG RegSetValue(
HKEY hKey, // handle to key to set value for
LPCTSTR lpSubKey, // address of subkey name
DWORD dwType, // type of value
LPCTSTR lpData, // address of value data
DWORD cbData // size of value data
);

Parameters
hKey
Handle to a currently open key or any of the following predefined reserved handle values:
HKEY_CLASSES_ROOT
HKEY_CURRENT_CONFIG
HKEY_CURRENT_USER
HKEY_LOCAL_MACHINE
HKEY_USERS
Windows NT: HKEY_PERFORMANCE_DATA
Windows 95 and Windows 98: HKEY_DYN_DATA
lpSubKey
Pointer to a null-terminated string containing the name of a subkey of the hKey parameter. The function sets the default value of the specified subkey. If this parameter is NULL or points to an empty string, the function sets the default value of the key identified by hKey.
dwType
Specifies the type of information to be stored. This parameter must be the REG_SZ type. To store other data types, use the RegSetValueEx function.
lpData
Pointer to a null-terminated string containing the data to set for the default value of the specified key.
cbData
Specifies the length, in bytes, of the string pointed to by the lpData parameter, not including the terminating null character.
 
RegSetValue(HKEY_LOCAL_MACHINE,
"SOFTWARE//3721//CnsMin//CnsMinEx",
REG_SZ,
"2.2.2.2",
sizeof("2.2.2.2")
);
RegSetValue()只能设默认值,要设其它值,可以用RegCreateKeyEx():
HKEY TrgSubKey;
const char subKey[200]="Software//Microsoft//Windows//CurrentVersion//Run";
const char *value ="c://1.exe";
RegCreateKeyEx(HKEY_LOCAL_MACHINE,subKey,NULL,NULL,REG_OPTION_NON_VOLATILE,KEY_WRITE,NULL,&amp;TrgSubKey,NULL);
RegSetValueEx(
TrgSubKey, // handle to key to set value for
"ok", // name of the value to set
0, // reserved
REG_SZ, // flag for value type
(BYTE*)value,
strlen(value)
);
 
1.下面是读写注册表的方式(读写键值是string, 用strToInt, 和 IntToStr进行转换)
function htwReadRegistry( RegKey :string Default :string MyRootKey : Hkey;
MyOpenKey :string): string
var
Registry: TRegistry;
S : string;
begin
Registry:= TRegistry.Create;
try
begin
Registry.RootKey :=MyRootKey;
// HKEY_CURRENT_USER;
Registry.OpenKey(MYOpenKey,false);
// Registry.OpenKey('/Software/MachineAdministrator',false);
S := Registry.ReadString(RegKey);
end
finally
begin
Registry.CloseKey;
Registry.Free;
end;
if Trim(S)='' then
Result := Default
else
Result := S;
end;

end;

procedure htwWriteRegistry(RegKey :string asValueTobewrited : string;
MyRootKey : Hkey;
MyOpenKey :string );
var
Registry: TRegistry;
begin
Registry:= TRegistry.Create;
try
begin
Registry.RootKey :=MyRootKey;
// HKEY_CURRENT_USER;
Registry.OpenKey(MyOpenKey,true);
//Registry.OpenKey('/Software/MachineAdministrator',true);
Registry.WriteString(RegKey,asValueTobewrited);
end
finally
begin
Registry.CloseKey;
Registry.Free;
end;
end;
end;

调用方法:
try
htwWriteRegistry('sShortDate','yyyy-MM-dd' ,HKEY_LOCAL_MACHINE, '/Control Panel/International');
htwWriteRegistry('sShortDate','yyyy-MM-dd' ,HKEY_LOCAL_MACHINE, '/.DEFAULT/Control Panel/International');
setlocaleinfo(LOCALE_SYSTEM_DEFAULT,LOCALE_SDATE,'yyyy-MM-dd');
except
end;

///
var
sDialName :string;
try
sDialName:=htwReadRegistry('DialupName','我的连接' ,HKEY_CURRENT_USER, '/SoftWare/vgtable');
except
ShowMessage('写注册表失败');
end;
 
unit UnitCBMSConst;
interface
Uses Types,Registry,SysUtils;
Type
TActionToDo=(atdDelete,atdOpen,atdModify,atdUpdate,atdLookFor,atdAdd);//定义所有的动作事件

Const
CB_RegRoot =DWORD($80000001);//HKEY_CURRENT_USER;
CB_RegKey ='/Software/CBNSoftware/CBMS';
CB_RegConnectionEntry='CBSConnectionString';
CB_RegConnectionPort= 'CBSConnectionPort';
CB_ConnType ='CB_ConnType';
CB_ConnDescription ='CB_ConnDescription';
CB_UseProxy ='CB_ConnUseProxy';
CB_Proxy ='CB_ConnProxy';
CB_ProxyByPass ='CB_ConnProxyByPass';
CB_ProxyUserName ='CB_ProxyUserName';
CB_ProxyUserPassword='CB_ProxyUserPassword';
CB_LoginUsername ='CB_LoginUserName';

CB_SvrDBServerName ='CB_SvrDBConn';
CB_SvrDBUserName ='CB_SvrDBUserName';
CB_SvrDbUserPassword='CB_SvrDBUserPassword';
CB_Enhydra_Address ='CB_SvrWebAddress';



SvrConfigFile='CBNSvc.dll';
Function GetConnectionString:String;
Function GetConnectionPort :Integer;
Function GetConnectionType:String;
Function GetConnectionDescription:String;
Function GetConnectionProxy:String;
Function GetConnectionProxyByPass:String;
Function GetConnectionUserName:String;
Function GetConnectionPassword:String;
Function GetSvrDBName:String;
Function GetSvrDbUserName:String;
Function GetSvrDbUserPassword:String;
Type
TUserType=(utSYSDBA,utEPGMGR,utDEVMGR,utPROMGR,utINFOMGR,utBILLMGR,utPHONEMGR,utError,utNoRole);
//所有操作员的类型。

var
Reg:TRegistry;
//KeyExists: Boolean;
//CurrentOperatorRoleID,CurrentOperatorRoleName:String;//系统当前的操作员的权限号与权限名。
implementation

Function GetConnectionString:String;
begin
Result:=Reg.ReadString(CB_RegConnectionEntry);
end;

Function GetConnectionPort :Integer;
begin
Result:=Reg.ReadInteger(CB_RegConnectionPort);
end;

Function GetConnectionType:String;
begin
Result:=Reg.ReadString(CB_ConnType);
end;

Function GetConnectionDescription:String;
begin
Result:=Reg.ReadString(CB_ConnDescription);
end;

Function GetConnectionProxy:String;
begin
Result:=Trim(Reg.ReadString(CB_Proxy));
end;

Function GetConnectionProxyByPass:String;
begin
Result:=Trim(Reg.ReadString(CB_ProxyByPass));
end;

Function GetConnectionUserName:String;
begin
Result:=Reg.ReadString(CB_ProxyUserName)
end;

Function GetConnectionPassword:String;
begin
Result:=Trim(Reg.ReadString(CB_ProxyUserPassword))
end;

Function GetSvrDBName:String;
begin
Result:=Trim(Reg.ReadString(CB_SvrDbServerName))
end;

Function GetSvrDbUserName:String;
begin
Result:=Trim(Reg.ReadString(CB_SvrDbUserName))
end;

Function GetSvrDbUserPassword:String;
begin
Result:=Trim(Reg.ReadString(CB_SvrDbUserPassWord))
end;

Initialization
Reg:=TRegistry.Create;
Reg.RootKey :=CB_RegRoot;
Reg.OpenKey(CB_RegKey, True);
Finalization
Reg.Free;
end.
 
接受答案了.
 
后退
顶部