|
-
Mar 16th, 2001, 02:39 PM
#1
Thread Starter
New Member
I am new to VB. I am building a utility that needs to grab a current environment variable (ie "CLIENTNAME"), manipulate that string and return a value back to "CLIENTNAME".
I need a good resource or a code snippit that will interact with DOS Environment variables (NT OS specifically).
Could it be as easy as utilizing %Clientname% within VB?
-
Mar 16th, 2001, 06:56 PM
#2
Hyperactive Member
MsgBox Environ("CLIENTNAME")
-
Mar 16th, 2001, 07:43 PM
#3
Hyperactive Member
Originally posted by jmcswain
MsgBox Environ("CLIENTNAME")
But that only returns the variable, it doesn't allow you to set it.
-
Mar 16th, 2001, 08:05 PM
#4
Hyperactive Member
craighope,
I would suggest that instead of using the environment to store information that
needs to be modified and restored, that you use the registry. This is what the regestry
was intended to do.
I just don't think that the environment can be changed easily from VB. Even if you
shell to DOS and change an environment variable, the change is only in the current
running MS-DOS prompt. Once that Window is closed, the variable is destroyed if
created or reverts to the previous value if changed.
-
Mar 16th, 2001, 08:32 PM
#5
Perhaps he's trying to communicate with a dos program.
Assuming that this is the case, you could use an Environmental variable to signify change to a file that both the dos program and your program read/writes to.
-
Mar 16th, 2001, 09:06 PM
#6
Hyperactive Member
True, if VB would allow you to change the environment variable; which it doesn't(at least not in VB 5).
Perhaps a search of the APIs may turn up something...
...ah, yes!
Code:
Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Private Sub Form_Load()
'This code was submitted by Brad McDonald ([email protected])
SetEnvironmentVariable "API-Guide", "Rocks!"
Shell "command.com"
'In the command windows type SET, you'll see the Environment Variable set, just for that session.
'Didn't show it in my Window since it starts a new seesion - dsy5
End Sub
and
Code:
Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Function GetEnvironmentVar(sName As String) As String
GetEnvironmentVar = String(255, 0)
GetEnvironmentVariable sName, GetEnvironmentVar, Len(GetEnvironmentVar)
If InStr(1, GetEnvironmentVar, Chr$(0)) > 0 Then GetEnvironmentVar = Left$(GetEnvironmentVar, InStr(1, GetEnvironmentVar, Chr$(0)) - 1)
GetEnvironmentVar = sName + ": " + GetEnvironmentVar
End Function
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Me.AutoRedraw = True
Me.Print GetEnvironmentVar("USERNAME")
Me.Print GetEnvironmentVar("USERDOMAIN")
Me.Print GetEnvironmentVar("PROCESSOR_IDENTIFIER")
Me.Print GetEnvironmentVar("NUMBER_OF_PROCESSORS")
Me.Print GetEnvironmentVar("OS")
End Sub
But again, it only sets it for the current process.
Last edited by dsy5; Mar 16th, 2001 at 09:25 PM.
Donald Sy - VB (ab)user
-
Mar 16th, 2001, 09:19 PM
#7
Dammit he beat me 
are you sure about your "that task only" info? You didnt even create the shell until after you set the variable.
Declare Function SetEnvironmentVariable& Lib "kernel32" Alias _
"SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String)
Parameter Type/Description
Code:
lpName String—The name of the environment variable to set. If an environment variable with this name does not yet exist, this function will create it.
lpValue String—The new value for the variable. NULL to clear an existing value (use the vbNullString constant to pass a null to this function).
Last edited by Lord Orwell; Mar 16th, 2001 at 09:30 PM.
-
Mar 16th, 2001, 09:40 PM
#8
Hyperactive Member
It seems that you couldn't communicate with a DOS program using this API technique.
Since each time you run a DOS program it creates its own instance of DOS, the
environment variable won't exist. Try this:
In your autoexec.bat file add a line:
Set TEST=junk
save and reboot.
Then start a new project, add a command button and put this code behind the form:
Code:
Private Declare Function SetEnvironmentVariable Lib "kernel32" Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Private Declare Function GetEnvironmentVariable Lib "kernel32" Alias "GetEnvironmentVariableA" (ByVal lpName As String, ByVal lpBuffer As String, ByVal nSize As Long) As Long
Function GetEnvironmentVar(sName As String) As String
GetEnvironmentVar = String(255, 0)
GetEnvironmentVariable sName, GetEnvironmentVar, Len(GetEnvironmentVar)
If InStr(1, GetEnvironmentVar, Chr$(0)) > 0 Then GetEnvironmentVar = Left$(GetEnvironmentVar, InStr(1, GetEnvironmentVar, Chr$(0)) - 1)
GetEnvironmentVar = sName + ": " + GetEnvironmentVar
End Function
Private Sub Command1_Click()
Me.AutoRedraw = True
Me.Print GetEnvironmentVar("TEST")
End Sub
'
Private Sub Form_Load()
'KPD-Team 2000
'URL: http://www.allapi.net/
'E-Mail: [email protected]
Me.AutoRedraw = True
Me.Print GetEnvironmentVar("TEST")
'This code was submitted by Brad McDonald ([email protected])
SetEnvironmentVariable "TEST", "Rocks!"
Shell "Command.com"
'In the CMD windows type SET, you'll see the Environment Variable set, just for that session.
End Sub
Now run it.
In the MS-Dos Prompt box, type set and press return.
You will see that when you shell to DOS, the environment variable doesn't
change - but it does within the VB project for the entire time it is running.
And if you just run it from the IDE, not making an EXE, it remains until VB is closed.
So if you have a DOS program running, it can't see your change.
Last edited by dsy5; Mar 16th, 2001 at 09:45 PM.
Donald Sy - VB (ab)user
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|