Click to See Complete Forum and Search --> : access to environment variables
jan s.
September 6th, 2001, 04:56 AM
Hi
Does anyone know how to access dos-environment-variables in vb?
I want to check if there´s a certain ocx in the
"%windir%\system32"
Thank you
Cakkie
September 6th, 2001, 05:48 AM
To get the system path, you can use the GetSystemDirectory API
private Declare Function GetSystemDirectory Lib "kernel32" Alias "GetSystemDirectoryA" (byval lpBuffer as string, byval nSize as Long) as Long
private Sub Form_Load()
Dim strPath as string
strPath = Space(255)
GetSystemDirectory strPath, 255
MsgBox strPath
End Sub
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
jan s.
September 6th, 2001, 07:30 AM
Hi
Thanks for your answer.
But i still got a little problem.
Using your code i´ve got "c:\winnt\system32"
in strPath. The next thing i want to do is to check if thers a mswinsck.ocx in that directory.
I tried it the following way:
dim d,x as string
x=strPath+"\mswinsck.ocx"
d=dir(x)
if(d="mswinsck.ocx")then
...
end if
This doesnt work. The problem is that
d=strPath+"\mswinsck.ocx"
doesnt work. d still only contains "c:\winnt\system32".
I guess this is because of all the spaces at the end of strPath, but i dont know how to fix that
(i already tried strPath=trim(strPath)).
Thank you
@Cakkie
PS: I´ve also send you sent you this message as a private message by mistake. Sorry for that.
Cakkie
September 6th, 2001, 07:32 AM
I already replied to you private message :)
use & in stead of +
x = strPath & "\mswinsock.ocx"
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
jan s.
September 6th, 2001, 07:49 AM
Still doesnt work.
I can add a string in front of srtPath
but not on the end of it.
x="xxx" & strPath works
x=strPath & "xxx" doesnt
???
Cakkie
September 6th, 2001, 08:00 AM
I reviewed my code and discovered a bug. The GetSystemDirectory function returns a null-terminated string. this is why you cannot append, cause null and something = null.
This should fix the problem. Place this line after the call to GetSystemDirectory
strPath = Left(strPath, len(Trim(strPath)) - 1)
Tom Cannaerts
slisse@planetinternet.be
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
John G Duffy
September 6th, 2001, 08:46 AM
Thats not really a bug. Most, if not all APIs that handle strings, Fill the space allocated for strings with nulls to avoid left over garbage in the data area for the string.
John G
Iouri
September 6th, 2001, 08:50 AM
Try this to read DOS environment
Private Sub Command1_Click()
Dim Env As String
Dim x As Integer
Dim t As String
x = 1
Env = Environ(x)
Do Until Env = ""
Env = Environ(x)
Print t; Env
x = x + 1
Loop
End Sub
Iouri Boutchkine
iouri@hotsheet.com
Iouri
September 6th, 2001, 08:51 AM
Also try this
Code: Environ("<Param>")
Returns a string, where <Param> can be any of the following:
ALLUSERSPROFILE
APPDATA
CommonProgramFiles
COMPUTERNAME
ComSpec
HOMEDRIVE
HOMEPATH
LOGONSERVER
NUMBER_OF_PROCESSORS
OS
Os2LibPath
Path
PATHEXT
PROCESSOR_ARCHITECTURE
PROCESSOR_IDENTIFIER
PROCESSOR_LEVEL
PROCESSOR_REVISION
ProgramFiles
SystemDrive
SystemRoot
TEMP
TMP
USERDOMAIN
USERNAME
USERPROFILE
windir
If you wish to dump all of the environment variables, this simple piece
of code will work:
For i = 1 To 26
Debug.Print Environ(i) & vbCrLf
Next i
Iouri Boutchkine
iouri@hotsheet.com
teradyne
February 14th, 2002, 11:22 AM
How do you go about setting environment variables from VB? Does environ have a matching function to do this?
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If the only tool you have is a hammer you will
tend to see all problems as nails
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.