Solved

Setting an environment variable with Delphi

Posted on 1998-08-10
10
272 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Windows Drag & Drop Location 2 101
Virtuailstring tree compare node issue 14 117
How to define IfThen functions in one common unit? 4 54
How to build JSON File in Delphi 6 3 35
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
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…

831 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