[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Setting an environment variable with Delphi

Posted on 1998-08-10
10
Medium Priority
?
281 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 300 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Loops Section Overview
Suggested Courses

867 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