Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating environment variable

Posted on 2000-04-26
11
Medium Priority
?
371 Views
Last Modified: 2010-05-02
Hi-

I need to create an environment variable during installation of my VB App. I think I have found a way to do this with WinNT. There is an environment variable called %SystemDrive% which I can use to locate or create autoexec.bat. I can add the line:

SET env_var="whatever"

Once the machine has rebooted, it should work.

My problem is, this may be installed on WinNT or Win9x. There is no %SystemDrive% variable on Win9x machines.. How can I create an environment variable on these platforms? Do anyone know an easier way to create an environment variable?
0
Comment
Question by:jmnolan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 1

Expert Comment

by:BETTY
ID: 2752367
If all you have to do is locate / create autoexec.bat, you should do a file check. Think autoexec.bat is always in the root, so you may check (even from a DOS bat) if exists in the root of any of the existing drive letters.

If it's not present anywhere, you can then look for the win*.exe (tha main windows executable) in the same way, so you will know in which letter drive the system is. You can then create the text file autoexec.bat in the root of this letter.

Greetings from Spain!
0
 

Author Comment

by:jmnolan
ID: 2752498
I was hoping there would be a more reliable way than checking all of the drives...
0
 
LVL 1

Expert Comment

by:BETTY
ID: 2752557
I work with Win NT from the beginning, so I don't know about Win9x. But I suggest you to post a "linking question" to here in the Operating systems area to get a wider experts check to the question.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 3

Expert Comment

by:HATCHET
ID: 2752942
jmnolan,

Are you trying to do this in VB, or in a Windows Batch file?  If you're doing it in VB, it's easy.  There are many Windows API's that you can use to get system information and write out files.  If you're looking for Windows Batch file creation information... take this to the Windows board... not the Visual Basic board.

HATCHET
( HATCHET1998@hotmail.com )
0
 

Author Comment

by:jmnolan
ID: 2753031
I am altering the VB setup project to create environment variables. I was hoping there would be some sort of "CreateEnvironmentVariable( sName, sValue)" type of function. From what I've been hearing it looks like I have to do all the dirty work myself, I was looking for a pre-written function.
0
 
LVL 5

Accepted Solution

by:
KDivad earned 400 total points
ID: 2753271
Since Windows and autoexec must exist on the same drive (don't they?), finding the windows folder should give you the autoexec drive.

WinDir = Environ$("windir")
If WinDir <> "" Then
    Open Left$(WinDir, 3) & "autoexec.bat" For Append As 1
    Print #1, "Set EnvVar=EnvVal"
    Close
Else
    MsgBox "Windows doesn't exist on this system?!?!." & Chr$(10) & "How are you running this app?!?!", vbInformation, "Error: Windows missing?"
End If
0
 
LVL 1

Expert Comment

by:j3877
ID: 2753334
Why not savesetting the variable to the system registry? If you do this, then you can just write a simple prog to save/get a variable:

function PutEnv(VarName as string, VarValue as string) as boolean

    on error goto PUTENV_Error_Handler

    SaveSetting "Environment Variables", "Vars", VarName, VarValue
    GetEnv=True
    Exit Function

PUTENV_Error_Handler:
    GetEnv=False
    Exit Function
end function

function GetEnv(VarName as string, optional DefaultValue as string="") as string

    on error goto GETENV_Error_Handler

    GetEnv = getsetting("Environment Variables","Vars",VarName,DefaultValue)
    Exit function

GETENV_Error_Handler:
    GetEnv="ERROR"
    exit Function
end function

0
 
LVL 5

Expert Comment

by:KDivad
ID: 2753880
I'm guessing that it probably interacts with some DOS apps that can't access the registry.
0
 

Author Comment

by:jmnolan
ID: 2755172
Actually, this is for CrypKey. In order to encrypt multiple files with the same license, I have to encrypt all of the files with an environment variable which will have the path to the license file. Unfortunately, you can't know where it is going to be installed until it is actually being installed. I am supposed to create an environment variable at that time with the path to the license file.

If %windir% exists on all windows platforms and autoexec.bat can be found in the root of that directory then I can try it that way. I am already using the registry for NT:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2756955
%windir% exists on Win95, Win95B and Win98SE. I don't know about any others as I've never used any others, but I would assume that Win98 is the same. If I'm not mistaken, Windows MUST be on the boot-drive to work. Therefore, autoexec can be found/created in the root of that drive. You could check for the existence of %windir% and if it's not there, use the reg key. That should cover Win9x and WinNT.
0
 
LVL 5

Expert Comment

by:KDivad
ID: 2760440
Glad I could help!
Thanks for the points!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

597 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