Solved

Creating environment variable

Posted on 2000-04-26
11
333 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
  • 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 5

Accepted Solution

by:
KDivad earned 100 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

759 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

18 Experts available now in Live!

Get 1:1 Help Now