Solved

Setting an environment variable with Delphi

Posted on 1998-08-10
10
271 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
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…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

943 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

9 Experts available now in Live!

Get 1:1 Help Now