|
-
Oct 18th, 2001, 03:56 PM
#1
environ
I'm trying to understand how to work with "environ" in Visual Basic 6 and i can't figure it out.
Please if someone can write me some explanation or some part of code that contained the "environ" issue.
TNX
-
Oct 18th, 2001, 04:27 PM
#2
To show what's in the Path environment variable:
MsgBox Environ("path")
What's the problem?
-
Oct 18th, 2001, 05:58 PM
#3
-= B u g S l a y e r =-
Hi there Tygur,
do u now if there are any way of setting Environment vars from winthin VB ?
-
Oct 18th, 2001, 06:04 PM
#4
Code:
Private Declare Function SetEnvironmentVariable Lib "kernel32" _
Alias "SetEnvironmentVariableA" (ByVal lpName As String, ByVal lpValue As String) As Long
Private Sub Form_Load()
SetEnvironmentVariable "TEST", "UGH"
Shell "CMD.EXE"
'Now type SET, you'll see the Environment Variable set
' lasts just for this process, goes away when the .EXE stops.
End Sub
-
Oct 18th, 2001, 06:08 PM
#5
-= B u g S l a y e r =-
thanks jim mcnamara, but (there is always a but )
it does not seem to do the job in win2k ?
-
Oct 18th, 2001, 06:16 PM
#6
-
Oct 18th, 2001, 06:21 PM
#7
-= B u g S l a y e r =-
okokok.. I know... I'd better goto bed
didnt read this either
' lasts just for this process, goes away when the .EXE stops
any suggestions on how to make it stay there ?
-
Oct 18th, 2001, 09:39 PM
#8
There is one problem with setting environment variables from within VB. If you set an environment variable using the SetEnvironmentVariable API, the changes don't show up when you try using Environ. So I've made a function that can be used in place of Environ so you can also get the changes that were made since your app started:
VB Code:
'This is the API declaration
Private Declare Function GetEnvironmentVariable Lib "kernel32" _
Alias "GetEnvironmentVariableA" (ByVal lpName As String, _
ByVal lpBuffer As String, ByVal nSize As Long) As Long
'Uses API to get an environment variable
'This is necessary because the Environ function will not detect changes to the environment vars
Function EnvironAPI(sString As String) As String
Dim EnvVar As String
Dim lLength As Long
EnvVar = Space(256)
lLength = GetEnvironmentVariable(sString, EnvVar, 256)
'If the environment variable is longer than 256 characters, we didn't get the whole thing yet
If lLength > 256 Then
EnvVar = Space(lLength)
lLength = GetEnvironmentVariable(sString, EnvVar, lLength)
End If
EnvVar = Left(EnvVar, lLength)
EnvironAPI = EnvVar
End Function
-
Oct 18th, 2001, 11:52 PM
#9
-
Jan 23rd, 2002, 02:57 PM
#10
Junior Member
Originally posted by Tygur
So I've made a function that can be used in place of Environ so you can also get the changes that were made since your app started:
LIES!!!!!!
Looks RATHER like the code here with very MINOR changes: http://www.vbapi.com/ref/g/getenvironmentvariable.html
-
Jan 23rd, 2002, 03:02 PM
#11
Frenzied Member
On defence of Tygur, the code on that site isnt in a function, its in the code for a commandbutton. So Tygur did make the function, even though he may not have written the code
You just proved that sig advertisements work.
-
Jan 23rd, 2002, 03:58 PM
#12
-= B u g S l a y e r =-
-
Jan 23rd, 2002, 04:23 PM
#13
Well, I hate to bust your bubble, but I really did write that function I posted and the code within it. For one thing, I never go to vbapi.com for api help. I always refer to the source of this information, the MSDN Library.
I've been using VB since 1997. You'd think I ought to know how to use such a simple API function by now 
The reason why the codes look so similar is because there are only so many ways of calling such a simple API function, and they are all quite similar. Can you come up with a better way? I certainly came up with a better way than whoever wrote that code at vbapi.com. Read further.
Is your problem the length I chose for the buffer (256)? 256 is a very common number for buffer lengths when people are making API calls. You'll find hundreds, maybe thousands of codes out there for api calls like GetWindowText and GetClassName that send strings with a length of 255 or 256. Everybody does it.
Also, tell me. If you wanted to abreviate "Envorironment Variable", how would you do it? I'm sure most people would agree that "EnvVar" is quite common. I doubt the vbapi.com people trademarked the name or anything And if you're going to bother me about the names of my length variable, you've really got issues 
That code is not similar to the code at vbapi.com, anyway. Look closer. If the environment variable is longer than 256 characters, my function up there will handle that and still return the whole thing. The code at vbapi.com does not. In fact, if the environment variable is longer than 256 characters long, the code at vbapi.com will not even retrieve it. Something like that is simply not acceptable for any code I make.
So, why did you think I copied that code, again?
-
Jan 23rd, 2002, 04:26 PM
#14
-
Jul 15th, 2002, 11:16 PM
#15
Lively Member
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
|