Solved

Setting an environment variable with Delphi

Posted on 1998-08-10
10
273 Views
Last Modified: 2010-05-18
I would like to set an environment variable to a certain value, depending on wich operating system is used.
If the PC is running Windows 95, I want to set the value"123" in Environment Variable "TestingVar".

How can I do this ?

Example code please !!!
0
Comment
Question by:belgianbasman
  • 4
  • 4
  • 2
10 Comments
 
LVL 2

Expert Comment

by:freter
ID: 1361797
Hi bassman!

Try these:

function GetEnvString ( value : string ) : string;
var
  buffer, suche : pchar;
  len           : word;
  c             : integer;
begin
  if length(value)>0 then begin
    GetMem(buffer,200);
    for c:=0 to 199 do buffer[c]:=#0;
    GetMem(suche,200);
    len := 200;
    strpcopy(suche,value);
    GetEnvironmentVariable(suche, buffer, len);
    Result:=StrPas(buffer);
    FreeMem(buffer);
    FreeMem(suche);
  end
  else begin
    Result:='';
  end;
end;

// -----------------------------------------------------------------------------

function SetEnvString ( name,value : string ) : boolean;
var
  pname, pvalue : pchar;
begin
  getmem(pname,length(name)+2);
  getmem(pvalue,length(value)+2);
  pname:=strpcopy(pname,name);
  pvalue:=strpcopy(pvalue,value);
  SetEnvironmentVariable(pchar(name), pchar(value));
  FreeMem(pname);
  FreeMem(pvalue);
end;

(you need to USE windows in your uses clause)

rgds
Freter
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1361798
hi, belgianbasman!

2 detect the system you're running on, use something like this:

function Get_Platform: string;
begin
  OSData.dwOSVersionInfoSize := SizeOf(OSData);
  if GetVersionEx(OSData) then
    case OSData.dwPlatformId of
      VER_PLATFORM_WIN32s       : Result := 'Windows 3.x/Win32s';
      VER_PLATFORM_WIN32_WINDOWS: Result := 'Windows 95';
      VER_PLATFORM_WIN32_NT     : Result := 'Windows NT'
    end
  else
    Result := 'error';
end;

mayb this is sufficient. or do u still wanna know how 2 manipulate environment settings? if so, which one? DOS environment? nope, i think, it must b windows' env. (NT?)

so long,
Black Death.
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1361799
Hey, frete!

fast as a shark u r!
u outran me again!

kreetinx,

Black.
0
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.

 
LVL 2

Expert Comment

by:freter
ID: 1361800
eh, Black!
. I had those somewhere on the disk, so it was just a question of finding them in time...
;-)
Freter
0
 

Author Comment

by:belgianbasman
ID: 1361801
Thanks guys,

I think this solved my problem. But who should I give the points to ?  You both answered half of the question. :)

One more question:  if I set an environment variable with the method frete described, will it be put in the autoexec.bat automatically or should I add a line to this file myself to make sure that this environment variable stays up the next time the computer restarts?
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1361802
hi bas!

it won't b put in the autoexec.bat automatically, so u've gotta do it by yourself.
no poinz 4 me, please.

Black Death.
0
 
LVL 2

Expert Comment

by:freter
ID: 1361803
hi bas!
black is right. as soon as your app terminates, all changes to the environment will vanish. this is due to the fact that each app gets it's own environment. btw: i think in good ole days of msdos, this was similar if you spawned another batch with the call command.
so, you've got to say yes to another excess and put the variable into the autoexec.bat yourself (i predict your next q: how to write a parser that can handle the autoexec correctly? ;-))

rgds
Freter
0
 
LVL 1

Expert Comment

by:BlackDeath
ID: 1361804
2nd cite piese.

(justajoke, frete! -
i heard the tommyknockers knocking at my door, 2.)

would u answuh, pleez (cos i'm gonna knock off work now)?
- und take the poinz so they won't starve 2 death
(or is there any way 4 basman 2 get'em back without having the q answered?).

have a nice day, u both -

good bye til 2morrow

Black.

0
 
LVL 2

Accepted Solution

by:
freter earned 100 total points
ID: 1361805
Ok bas.
I'm willing to take the points then ;-), if you are pleased with the answer. If not, let the q be deleted by linda.

Cheers,
Freter

0
 

Author Comment

by:belgianbasman
ID: 1361806
Thank you for the comments, freter and BlackDeath.


0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…

808 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