?
Solved

How to set environnement strings in Pascal?

Posted on 1997-06-14
9
Medium Priority
?
364 Views
Last Modified: 2012-06-21
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
0
Comment
Question by:kilobug
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
9 Comments
 
LVL 4

Expert Comment

by:feenix
ID: 1215371
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.
0
 

Author Comment

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

Bye
0
 

Expert Comment

by:messineo
ID: 1215373
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


0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1215374
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
0
 

Author Comment

by:kilobug
ID: 1215375
Thanx... But this is to read environnement strings, not to write them!
0
 

Expert Comment

by:Tomahawk
ID: 1215376
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.
0
 
LVL 2

Accepted Solution

by:
obg earned 360 total points
ID: 1215377
There are undocumented ways to do this. The problem is that
your program inherits the system environment, and makes changes
in it's own, that does not export back.

I solved the problem in a very ugly way by simply searching for
the environment strings in memory. They are standard C-strings,
terminated by the NUL-character, and the anvironment area itself
is terminated by a double NUL. The size of the area is stored in
the first 16-bit word (I think)... Of course, this method will
only work in 16-bit environments such as DOS.

I have seen another undocumented way to do this, using INT 2E. I
have never used that method myself, however. It is also said to
be very unreliable (no comments about my method...)
0
 
LVL 6

Expert Comment

by:Stuart_Johnson
ID: 1215378
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
0
 

Author Comment

by:kilobug
ID: 1215379
Thanx, I will try this tomorrow.....
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The well known Cerber ransomware continues to spread this summer through spear phishing email campaigns targeting enterprises. Learn how it easily bypasses traditional defenses - and what you can do to protect your data.
The top devops trends for 2017 are focused on improved deployment frequency, decreased lead time for change and decreased MTTR.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

801 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question