Solved

How to set environnement strings in Pascal?

Posted on 1997-06-14
9
358 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
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
Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

 
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 120 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to resolve an IP Address to a host name 6 1,707
Text alignment in Delphi XE3 TStringGrid when style is gdsThemed. 5 2,003
.dat problems 39 211
Intraweb submit form as a POST request 4 360
Did you know that more than 4 billion data records have been recorded as lost or stolen since 2013? It was a staggering number brought to our attention during last week’s ManageEngine webinar, where attendees received a comprehensive look at the ma…
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

697 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