Link to home
Start Free TrialLog in
Avatar of jmnolan
jmnolan

asked on

Creating environment variable

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?
Avatar of BETTY
BETTY
Flag of Spain image

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!
Avatar of jmnolan
jmnolan

ASKER

I was hoping there would be a more reliable way than checking all of the drives...
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.
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 )
Avatar of jmnolan

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of KDivad
KDivad

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

I'm guessing that it probably interacts with some DOS apps that can't access the registry.
Avatar of jmnolan

ASKER

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
%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.
Glad I could help!
Thanks for the points!