Einzelnen Beitrag anzeigen

Klaus01

Registriert seit: 30. Nov 2005
Ort: München
5.757 Beiträge
 
Delphi 10.4 Sydney
 
#13

Re: delphi / batch kombinieren

  Alt 27. Apr 2006, 16:26
Die Funktion die ich mit SetGlobalEnvironment setze, setzt eine globale
Environmnet Variable. Diese ist von jedem cmd Fenster aus
abfragbar. Sie überlebt den reboot.

Um ein Variable nur im activen cmd-Fenster zu haben,
reicht es das API command
SetEnvironmentVariable(PChar(Name), PChar(Value));
auszuführen.

Zu den Parametern gibt es im angehängten pdf File mehr Infos.

Grüße
Klaus

habe ich noch gefunden:
nicht getestet:

Delphi-Quellcode:
unit Environment;

interface

  procedure ActivateEnvironmentVariables;

  function AddGlobalEnvironmentVariable(const Key: string; const Value : string; const now : boolean) : boolean;
  function AddEnvironmentVariable(const Key: string; const Value : string; const now : boolean) : boolean;

  function DelGlobalEnvironmentVariable(const Key: string; const now : boolean) : boolean;
  function DelEnvironmentVariable(const Key: string; const now : boolean) : boolean;

implementation

uses
   Registry, Windows;

const
  GlobalKey = 'System\CurrentControlSet\Control\Session Manager\Environment';
  LocalKey = 'Environment';
  WM_WININICHANGE = $001A;


procedure ActivateEnvironmentVariables;
begin
  SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(PChar('Environment')));
end;

function AddGlobalEnvironmentVariable(const Key: string; const Value : string; const now : boolean) : boolean;
var
  Reg : TRegistry;
  ok : boolean;
begin
  Result := true;
  ok := false;
  try
    Reg := TRegistry.Create;
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey(GlobalKey, false) then
    begin
      Reg.WriteString(Key, Value);
      Reg.CloseKey;

      if now then ActivateEnvironmentVariables;
      ok := true;
    end;
    Result := ok;
  except
    Result := false;
  end;
end;

function AddEnvironmentVariable(const Key: string; const Value : string; const now : boolean) : boolean;
var
  Reg : TRegistry;
  ok : boolean;
begin
  Result := true;
  ok := false;
  try
    Reg := TRegistry.Create;
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey(LocalKey, false) then
    begin
      Reg.WriteString(Key, Value);
      Reg.CloseKey;

      if now then ActivateEnvironmentVariables;
      ok := true;
    end;
    Result := ok;
  except
    Result := false;
  end;
  Result := SetEnvironmentVariable(PChar(Key), PChar(Value));
end;

function DelGlobalEnvironmentVariable(const Key: string; const now : boolean) : boolean;
var
  Reg : TRegistry;
  ok : boolean;
begin
  ok := false;
  Result := true;
  try
    Reg := TRegistry.Create;
    Reg.RootKey := HKEY_LOCAL_MACHINE;
    if Reg.OpenKey(GlobalKey, false) then
    begin
      Reg.DeleteValue(Key);
      Reg.CloseKey;

      if now then ActivateEnvironmentVariables;
      ok := true;
    end;
    Result := ok;
  except
    Result := false;
  end;
end;

function DelEnvironmentVariable(const Key: string; const now : boolean) : boolean;
var
  Reg : TRegistry;
  ok : boolean;
begin
  ok := false;
  Result := true;
  try
    Reg := TRegistry.Create;
    Reg.RootKey := HKEY_CURRENT_USER;
    if Reg.OpenKey(LocalKey, false) then
    begin
      Reg.DeleteValue(Key);
      Reg.CloseKey;

      if now then ActivateEnvironmentVariables;
      ok := true;
    end;
    Result := ok;
  except
    Result := false;
  end;
  Result := SetEnvironmentVariable(PChar(Key), nil);
end;

end.
Angehängte Dateien
Dateityp: pdf setenvironmentvariable_169.pdf (84,0 KB, 3x aufgerufen)
Klaus
  Mit Zitat antworten Zitat