Solved

Creating environment variable

Posted on 2000-04-26
11
339 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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

776 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