Solved

Setting an environment variable with Delphi

Posted on 1998-08-10
10
270 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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
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

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

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
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…

758 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

21 Experts available now in Live!

Get 1:1 Help Now