Link to home
Start Free TrialLog in
Avatar of kilobug
kilobug

asked on

How to set environnement strings in Pascal?

hi!
   I'm making a progral in Pascal (BP 7.00) and need to change environnement settings, like with the DOS "set" command. How to do that???????

thanx
Avatar of feenix
feenix
Flag of Finland image

Well, at least in Turbo Pascal there was (if I recall correctly)
a function called SetEnv that allowed you to set environment
variables. Could be that there was only GetEnv, but I think I
have used SetEnv sometime.
Avatar of kilobug
kilobug

ASKER

Thanx for responding, but the SetEnv works only with Turbo Pascal for Windows (or with BP for Windows target) not for DOS...

Bye
Try using the swapvectors procedure to call Dos commands from your program.

As an example:

SwapVectors;
Exec(GetEnv('COMSPEC','/c'+Command));
SwapVectors;

Where Command is some DOS Command string.  Hope this helps


To retrieve all the DOS environment variables use :

var i: Integer;

begin
   for i := 1 to EnvCount do
      WriteLn(EnvStr(i));
end;

This will return all your environment strings set in DOS.  If you are searching for just one string, use :

const SString = 'PATH'; {or any other string}

var Found: Boolean;
        i: String;

begin
   i := 0;
   Found := False;
   While (I < EnvCount) and (Not Found) do
      Found := (Pos(SString, EnvStr(I)) > 0);
end;

As DOS Env. strings are uppercase, ensure you use an uppercase search string, otherwise this routine wont find what you are looking for.

Hope this helps.


Stuart
Avatar of kilobug

ASKER

Thanx... But this is to read environnement strings, not to write them!
I seem to remember using SetEnv to set a DOS Envirnment variable.
The only problem I found was that the variable didn't stay when
the program exited - it was a local environment variable to the
program.
ASKER CERTIFIED SOLUTION
Avatar of obg
obg

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sorry about the last response I left.  I didnt really read the question properly.  Anyway, I think this will solve all you problems.

Function SetEnvStr(Env : EnvRec; Search, Value : String) : Boolean;
Var
  SLen : Byte Absolute Search;
  VLen : Byte Absolute Value;
  EPtr : EnvArrayPtr;
  ENext : Word;
  EOfs : Word;
  MOfs : Word;
  OldLen : Word;
  NewLen : Word;
  NulLen : Word;
begin
  With Env do begin
    SetEnvStr := False;
    if (EnvSeg = 0) or (SLen = 0) then
      Exit;
    EPtr := Ptr(EnvSeg, 0);

    {Find the search String}
    EOfs := SearchEnv(EPtr, Search);

    {Get the index of the next available environment location}
    ENext := EnvNext(EPtr);

    {Get total length of new environment String}
    NewLen := SLen+VLen;

    if EOfs <> $FFFF then begin
      {Search String exists}
      MOfs := EOfs+SLen;
      {Scan to end of String}
      SkipAsciiZ(EPtr, MOfs);
      OldLen := MOfs-EOfs;
      {No extra nulls to add}
      NulLen := 0;
    end else begin
      OldLen := 0;
      {One extra null to add}
      NulLen := 1;
    end;

    if VLen <> 0 then
      {Not a pure deletion}
      if ENext+NewLen+NulLen >= EnvLen+OldLen then
        {New String won't fit}
        Exit;

    if OldLen <> 0 then begin
      {OverWrite previous environment String}
      Move(EPtr^[MOfs+1], EPtr^[EOfs], ENext-MOfs-1);
      {More space free now}
      Dec(ENext, OldLen+1);
    end;

    {Append new String}
    if VLen <> 0 then begin
      Move(Search[1], EPtr^[ENext], SLen);
      Inc(ENext, SLen);
      Move(Value[1], EPtr^[ENext], VLen);
      Inc(ENext, VLen);
    end;

    {Clear out the rest of the environment}
    FillChar(EPtr^[ENext], EnvLen-ENext, 0);

    SetEnvStr := True;
  end;
end;


Regards,


Stuart
Avatar of kilobug

ASKER

Thanx, I will try this tomorrow.....