⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stregini.pas

📁 条码控件: 一维条码控件 二维条码控件 PDF417Barcode MaxiCodeBarcode
💻 PAS
📖 第 1 页 / 共 5 页
字号:
(* ***** BEGIN LICENSE BLOCK *****
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 *
 * The Original Code is TurboPower SysTools
 *
 * The Initial Developer of the Original Code is
 * TurboPower Software
 *
 * Portions created by the Initial Developer are Copyright (C) 1996-2002
 * the Initial Developer. All Rights Reserved.
 *
 * Contributor(s):
 *
 * ***** END LICENSE BLOCK ***** *)

{*********************************************************}
{* SysTools: StRegIni.pas 4.03                           *}
{*********************************************************}
{* SysTools: Registry and INI file access                *}
{*********************************************************}

{$I StDefine.inc}

unit StRegIni;

interface

uses
  Windows,
  Graphics, Classes, SysUtils,
  {$IFDEF HStrings} STStrL, {$ELSE} STStrS, {$ENDIF}
  StDate, STConst, STBase;

type
{.Z+}
  TRegIniType = (riIniType, riRegType);
  TRegIniMode = (riSet, riGet);
  TWinVerType = (riWin31,riWin32s,riWin95,riWinNT);
{.Z-}

  TQueryKeyInfo = record
    QIKey       : HKey;         {Value of key being queried}
    QIClassName : string;       {Class Name associated with key}
    QINumSubKeys: DWORD;        {Number of Subkeys under queried key}
    QIMaxSKNLen : DWORD;        {Length of longest subkey name}
    QIMaxCNLen  : DWORD;        {Length of longest class name found}
    QINumValues : DWORD;        {Number of values found in queried key ONLY, i.e., values in subkeys not included}
    QIMaxVNLen  : DWORD;        {Length of longest value name}
    QIMaxDataLen: DWORD;        {Largest size (in bytes) of values in queried key}
    QISDescLen  : DWORD;        {Length of Security Descriptor}
    QIFileTime  : TFileTime;    {Time/date file/key was last modified}
  end;

const
{.Z+}
  RI_INVALID_VALUE = -1;
  RIVOLATILE = REG_WHOLE_HIVE_VOLATILE;
  ShortBufSize = 255;
  MaxBufSize = 8192;
  MaxByteArraySize = 127;
{.Z-}

  RIMachine = 'MACHINE';
  RIUsers   = 'USERS';
  RIRoot    = 'ROOT';
  RICUser   = 'C_USERS';


type
  TStRegIni = class(TObject)
{.Z+}
  protected {private}
    riMode           : TRegIniMode;

    riWinVer         : TWinVerType;
    riType           : TRegIniType;
    riHoldPrimary,
    riPrimaryKey     : HKey;
    riRemoteKey      : HKey;

    riCurSubKey,
    riTrueString,
    riFalseString    : PAnsiChar;

{$IFDEF ThreadSafe}
    riThreadSafe     : TRTLCriticalSection;
{$ENDIF}

    function GetAttributes : TSecurityAttributes;
      {-get security attributes record or value}
    procedure SetAttributes(Value : TSecurityAttributes);
      {-get security attributes record or value}

    function GetCurSubKey : string;
      {-get current subkey/section}
    procedure SetCurSubKey(Value : string);
      {-set current subkey/section}

    function GetIsIniFile : Boolean;
      {-get whether current instance in IniFile or no}

    procedure ParseIniFile(SList : TStrings);
      {-adds section names in an INI file to a string list}

  protected
    FCurSubKey       : string;
    FriSecAttr       : TSecurityAttributes;
    FIsIniFile       : Boolean;

    riRootName       : PAnsiChar;

    BmpText,
    BmpBinary        : TBitMap;

    {protected procedures to manage open/closing}
    function OpenRegKey : HKey;
      {-opens/creates key or ini file}
    procedure CloseRegKey(const Key : HKey);
      {-closes open key or ini file}

    procedure EnterCS;
      {- call EnterCriticalSection procedure}
    procedure LeaveCS;
      {- call LeaveCriticalSection procedure}

    function WriteIniData(const ValueName : string; Data : string) : Boolean;
      {-write data to an Ini file}

    function ReadIniData(const ValueName : string; var Value : string;
                         Default : string) : Integer;
      {-read data from an Ini file}

    function WriteRegData(Key : HKey; const ValueName : string; Data : Pointer;
                          DType : DWORD; Size : Integer) : LongInt;
      {-write data to the registry}

    function ReadRegData(Key : HKey; const ValueName : string; Data : Pointer;
                         Size : LongInt; DType : DWORD) : LongInt;
      {-read data from the registry}

{.Z-}
  public
    constructor Create(RootName : String; IsIniFile : Boolean); virtual;
    destructor Destroy; override;

    procedure SetPrimary(Value : string);
      {-change INI filename or primary key of registry}
    function GetPrimary : string;
      {-return current INI filename or primary key of registry}

    function GetDataInfo(Key : HKey; const ValueName : string;
                         var Size : LongInt; var DType : DWORD) : LongInt;
      {-get size and type of data for entry in registry}

    function BytesToString(Value : PByte; Size : Cardinal) : string;
      {-converts byte array to string with no spaces}
    function StringToBytes(const IString : string; var Value; Size : Cardinal) : Boolean;
      {-converts string (by groups of 2 char) to byte values}


    function GetFullKeyPath : string;

    procedure WriteBoolean(const ValueName : string; Value : Boolean);
      {-set boolean data in the ini file or registry}
    function  ReadBoolean(const ValueName : string; Default : Boolean) : Boolean;
      {-get boolean data in the ini file or registry}
    procedure WriteInteger(const ValueName : string; Value : DWORD);
      {-set integer data in the ini file or registry}
    function  ReadInteger(const ValueName : string; Default : DWORD) : DWORD;
      {-get integer data in the ini file or registry}
    procedure WriteString(const ValueName : string; const Value : string);
      {-set string data in the ini file or registry}
    function  ReadString(const ValueName : string; const Default : string) : string;
      {-get string data in the ini file or registry}
    procedure WriteBinaryData(const ValueName : string; const Value; Size : Integer);
      {-set byte array in the ini file or registry}
    procedure ReadBinaryData(const ValueName : string; const Default; var Value; var Size : Integer);
      {-get byte array from the ini file or registry}
    procedure WriteFloat(const ValueName : string; const Value : Double);
      {-set float value in the ini file or registry}
    function ReadFloat(const ValueName : string; const Default : TStFloat) : TStFloat;
      {-get float from the ini file or registry}
    procedure WriteDate(const ValueName : string; const Value : TStDate);
      {-set date value in the ini file or registry}
    function ReadDate(const ValueName : string; const Default : TStDate) : TStDate;
      {-get date value from the ini file or registry}
    procedure WriteDateTime(const ValueName : string; const Value : TDateTime);
      {-set datetime value in the ini file or registry}
    function ReadDateTime(const ValueName : string; const Default : TDateTime) : TDateTime;
      {-get datetime value from the ini file or registry}
    procedure WriteTime(const ValueName : string; const Value : TStTime);
      {-set time value in the ini file or registry}
    function ReadTime(const ValueName : string; const Default : TStTime) : TStTime;
      {-get time value from the ini file or registry}


    procedure CreateKey(const KeyName : string);
      {-creates Section in INI file or Key in Registry}
    procedure GetSubKeys(SK : TStrings);
      {-lists sections in INI file or subkeys of SubKey in Registry}
    procedure GetValues(SKV : TStrings);
      {-lists values in INI section or in Registry SubKey}
    procedure DeleteKey(const KeyName : string; DeleteSubKeys : Boolean);
      {-Deletes section in INI file or key in Registry file}
    procedure DeleteValue(const ValueName : string);
      {-Deletes a value from an INI section or Registry key}
    procedure QueryKey(var KeyInfo : TQueryKeyInfo);
      {-lists information about an INI section or Registry SubKey}
    function KeyExists(KeyName : string) : Boolean;
      {-checks if exists in INI file/Registry}
    function IsKeyEmpty(Primary, SubKey : string) : Boolean;
      {-checks if key has values and/or subkeys}

    procedure SaveKey(const SubKey : string; FileName : string);
      {-saves an INI Section with values or Registry Subkey with all values and
        subkeys to specified file}
    procedure LoadKey(const SubKey, FileName : string);
      {-loads an INI file section or Registry key with all subkeys/values}
    procedure UnLoadKey(const SubKey : string);
      {-same as DeleteKey for INI file; removes key/subkeys loaded with LoadKey}
    procedure ReplaceKey(const SubKey, InputFile, SaveFile : string);
      {-replaces an INI file section or Registry key/subkeys
        from InputFile, saves old data in SaveFile}
    procedure RestoreKey(const SubKey, KeyFile : string; Options : DWORD);
      {-restores an INI section or Registry key/subkeys from KeyFile}

    procedure RegOpenRemoteKey(CompName : string);
      {-connects to Registry on another computer on network}
    procedure RegCloseRemoteKey;
      {-closes connection made with RegConnectRegistry}

    property Attributes : TSecurityAttributes
      read GetAttributes
      write SetAttributes;

    property CurSubKey : string
      read GetCurSubKey
      write SetCurSubKey;

    property IsIniFile : Boolean
      read GetIsIniFile;
    procedure RegGetKeySecurity(const SubKey : string; var SD : TSecurityDescriptor);
      {-gets KeySecurity information on WinNT machines}
    procedure RegSetKeySecurity(const SubKey : string; SD : TSecurityDescriptor);
      {-sets KeySecurity information on WinNT machines}
  end;


implementation

procedure RaiseRegIniError(Code : LongInt);
var
  E : ESTRegIniError;
begin
  E := ESTRegIniError.CreateResTP(Code, 0);
  E.ErrorCode := Code;
  raise E;
end;

{==========================================================================}

procedure RaiseRegIniErrorFmt(Code : LongInt; A : array of const);
var
  E : ESTRegIniError;
begin
  E := ESTRegIniError.CreateResFmtTP(Code, A, 0);
  E.ErrorCode := Code;
  raise E;
end;

{==========================================================================}

constructor TStRegIni.Create(RootName : String; IsIniFile : Boolean);
var
  S   : string;
  OSI : TOSVERSIONINFO;
begin
{$IFDEF ThreadSafe}
  Windows.InitializeCriticalSection(riThreadSafe);
{$ENDIF}

  {check if a primary key or ini file is specified}
  if (Length(RootName) = 0) then
    RaiseRegIniError(stscNoFileKey);
  RootName := ANSIUpperCase(RootName);

  {get False string from resource}
  S := SysToolsStr(stscFalseString);
  GetMem(riFalseString,Length(S)+1);
  StrPCopy(riFalseString,S);

  {get True string from resource}
  S := SysToolsStr(stscTrueString);
  GetMem(riTrueString,Length(S)+1);
  StrPCopy(riTrueString,S);

  GetMem(riCurSubKey,1);
  riCurSubKey[0] := #0;

  BmpText   := TBitMap.Create;
  BmpBinary := TBitMap.Create;

  BmpText.Handle := LoadBitmap(HInstance, 'STBMPTEXT');
  BmpBinary.Handle := LoadBitmap(HInstance, 'STBMPBINARY');

  {setup ini file/primary key via riRootName}
  if (IsIniFile) then begin
    riType := riIniType;
    GetMem(riRootName,Length(RootName)+1);
    StrPCopy(riRootName,RootName);
  end else begin
    riType := riRegType;

    riPrimaryKey := 0;
    riHoldPrimary := 0;
    if (RootName = RIMachine) then
       riPrimaryKey := HKEY_LOCAL_MACHINE
    else if (RootName = RIUsers) then
       riPrimaryKey := HKEY_USERS
    else if (RootName = RIRoot) then
       riPrimaryKey := HKEY_CLASSES_ROOT
    else if (RootName = RICUser) then
       riPrimaryKey := HKEY_CURRENT_USER
    else
       riPrimaryKey := HKEY_CURRENT_USER;

    OSI.dwOSVersionInfoSize := SizeOf(OSI);
    if (GetVersionEX(OSI)) then begin
      case OSI.dwPlatformID of
         VER_PLATFORM_WIN32S : RaiseRegIniError(stscNoWin32S);
         VER_PLATFORM_WIN32_WINDOWS : riWinVer := riWin95;
         VER_PLATFORM_WIN32_NT : riWinVer := riWinNT;
      end;
    end;

    if (FriSecAttr.nLength <> sizeOf(TSecurityAttributes)) then begin
      FriSecAttr.nLength := sizeof(TSecurityAttributes);
      FriSecAttr.lpSecurityDescriptor := nil;
      FriSecAttr.bInheritHandle := TRUE;
    end;

  end;
end;

{==========================================================================}

destructor TStRegIni.Destroy;
begin
  {no need to check for local key since none are kept open}
  {longer than needed for a specific method}
  if (riRemoteKey <> 0) then
    RegCloseRemoteKey;

  if (riRootName <> nil) then
    FreeMem(riRootName,StrLen(riRootName)+1);
  if (riFalseString <> nil) then
    FreeMem(riFalseString,StrLen(riFalseString)+1);
  if (riTrueString <> nil) then
    FreeMem(riTrueString,StrLen(riTrueString)+1);
  if (riCurSubKey <> nil) then
    FreeMem(riCurSubKey,StrLen(riCurSubKey)+1);

  BmpText.Free;
  BmpBinary.Free;

{$IFDEF ThreadSafe}
  Windows.DeleteCriticalSection(riThreadSafe);
{$ENDIF}
  inherited Destroy;
end;

{==========================================================================}


procedure TStRegIni.SetPrimary(Value : string);
  {-change working Ini file or top level key in registry}
begin
  if riType = riIniType then begin
    if CompareText(Value,StrPas(riRootName)) = 0 then Exit;

    if (riRootName <> nil) then
      FreeMem(riRootName,StrLen(riRootName)+1);
    GetMem(riRootName,Length(Value)+1);
    StrPCopy(riRootName,Value);
  end else begin
    if (riRemoteKey <> 0) then
      RegCloseRemoteKey;

    if (Value = RIMachine) then
      riPrimaryKey := HKEY_LOCAL_MACHINE
    else if (Value = RIUsers) then
       riPrimaryKey := HKEY_USERS
    else if (Value = RIRoot) then
       riPrimaryKey := HKEY_CLASSES_ROOT
    else if (Value = RICUser) then
       riPrimaryKey := HKEY_CURRENT_USER
    else
       riPrimaryKey := HKEY_CURRENT_USER;
  end;
end;

{==========================================================================}

function TStRegIni.GetPrimary : string;
  {-return working Ini file or top level registry key}
begin
  if (riType = riIniType) then
    Result := StrPas(riRootName)
  else begin
    case riPrimaryKey of
      HKEY_LOCAL_MACHINE : Result := RIMachine;
      HKEY_USERS         : Result := RIUsers;
      HKEY_CLASSES_ROOT  : Result := RIRoot;
      HKEY_CURRENT_USER  : Result := RICUser;
    else
      Result := 'Invalid primary key'
    end;
  end;
end;

{==========================================================================}

procedure TStRegIni.EnterCS;
begin
{$IFDEF ThreadSafe}
  EnterCriticalSection(riThreadSafe);
{$ENDIF}
end;

{==========================================================================}

procedure TStRegIni.LeaveCS;
begin
{$IFDEF ThreadSafe}
  LeaveCriticalSection(riThreadSafe);
{$ENDIF}
end;

{==========================================================================}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -