Solved

How to set environnement strings in Pascal?

Posted on 1997-06-14
9
354 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
 
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Unsigned int64 to unsigned int32 10 1,631
Delphi 2007 printer setup problem 8 745
Delphi Skype API 2 1,730
How do you get the IP address of a network? 12 273
Owning a franchise can be the dream of a lifetime. It provides a chance for economic growth. You can be as successful as you want.  To make your franchise successful, you need to market it successfully. Here are six of the best marketing strategies …
Hyena v12.2 is now available for downloading and is available in English, French, German and Spanish versions.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now