Solved

How to set environnement strings in Pascal?

Posted on 1997-06-14
9
356 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
TZConnection connected in separate thread 2 1,002
Lazarus and Excel formulas via OLE 7 274
delphi custom sort exception 6 162
proper way to parse url in delphi 2 204
Does your audience prefer people in photos or no people? How can you best highlight what you’re selling? What are your competitors doing, and what can you do that is different and unique from them?  Continue reading to learn how to make your images …
This article outlines why you need to choose a backup solution that protects your entire environment – including your VMware ESXi and Microsoft Hyper-V virtualization hosts – not just your virtual machines.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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