Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Library: Windows API / MS.NET Framework API (https://www.delphipraxis.net/20-library-windows-api-ms-net-framework-api/)
-   -   Delphi Arbeiten mit der Registry ohne TRegistry (https://www.delphipraxis.net/2588-arbeiten-mit-der-registry-ohne-tregistry.html)

Luckie 30. Jan 2003 10:41


Arbeiten mit der Registry ohne TRegistry
 
Delphi-Quellcode:
function WriteStringToRegAPI(const p_sSubKey : string;const p_sValueName : string;const p_sValue : string) : integer;

var
  hResult : HKEY;

begin
  // Es ginge auch RegOpenKeyEx, so wird ein nicht existenter Key aber angelegt.
  // Existiert der SubKey, wird er geöffnet
  Result := RegCreateKeyEx(HKEY_CURRENT_USER,PChar(p_sSubKey),0,nil,0,KEY_ALL_ACCESS,nil,hResult,nil);
  if Result <> ERROR_SUCCESS then
  begin
    exit;
  end;
  try
    Result := RegSetValueEx(hResult,PChar(p_sValueName),0,REG_SZ,@p_sValue[1],Length(p_sValue)+1);
  finally
    RegCloseKey(hResult);
  end;
end;

function ReadStringFromRegAPI(const p_sSubKey : string; const p_sValueName : string;var p_sResult : string) : integer;

var
  hResult      : HKEY;
  dwMaxValueLen : DWORD;
  szResult     : PChar;

begin
  Result := RegOpenKeyEx(HKEY_CURRENT_USER,PChar(p_sSubKey),0,KEY_ALL_ACCESS,hResult);
  if Result <> ERROR_SUCCESS then
  begin
    exit;
  end;
  try
    Result := RegQueryInfoKey(hResult,nil,nil,nil,nil,nil,nil,nil,nil,@dwMaxValueLen,nil,nil);
    if Result <> ERROR_SUCCESS then
    begin
      exit;
    end;
    inc(dwMaxValueLen);
    szResult := StrAlloc(dwMaxValueLen);
    try
      Result := RegQueryValueEx(hResult,PChar(p_sValueName),nil,nil,PByte(szResult),@dwMaxValueLen);
      if Result <> ERROR_SUCCESS then
      begin
        exit;
      end;
      p_sResult := trim(szResult);
    finally
      StrDispose(szResult);
    end;
  finally
    RegCloseKey(hResult);
  end;
end;
procedure TForm1.Button1Click(Sender: TObject);

var
  iResult : integer;
  sResult : string;

begin
  iResult := WriteStringToRegAPI('Software\CSE\TEST','TESTWERTNAME','TESTWERT');
  if iResult <> ERROR_SUCCESS then
  begin
    ShowMessage(SysErrorMessage(iResult));
  end;
  iResult := ReadStringFromRegAPI('Software\CSE\TEST','TESTWERTNAME',sResult);
  if iResult <> ERROR_SUCCESS then
  begin
    ShowMessage(SysErrorMessage(iResult));
  end
  else
  begin
    ShowMessage(sResult);
  end;
end;
Christian Seehase


Alle Zeitangaben in WEZ +1. Es ist jetzt 16:14 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz