Help: SetEnvironmentVariable call don't work! 
Author Message
 Help: SetEnvironmentVariable call don't work!


>I cant get the SetEnvironmentVariable API call to work.


You are not going nuts, just missing one important point:  when VB starts it
reads the environment and loads it into memory and the Environ$ function is
reading this copy.  Since VB does not support changing the variables there
is no way to refresh this cache and you will never see changes.  In order to
read the "real" environment you must use the API as well.  Try this:

Private Declare Function GetEnvironmentVariable Lib "kernel32" _
  Alias "GetEnvironmentVariableA" (ByVal lpName As String, _
  ByVal lpBuffer As String, ByVal nSize As Long) As Long
Private Declare Function SetEnvironmentVariable _
  Lib "kernel32" Alias "SetEnvironmentVariableA" _
  (ByVal lpName As String, ByVal lpValue As String) As Long

Private Sub Form_Load()
Dim lRet As Long
MsgBox "before: " & GetVariable("MYENV")
lRet = SetEnvironmentVariable("MYENV", CStr(Rnd * 1000))
MsgBox "after: " & GetVariable("MYENV")
Unload Me
End Sub

Private Function GetVariable(ByVal VarName As String) As String
Dim sVal As String * 2048
Dim x As Long
x = GetEnvironmentVariable(VarName, sVal, Len(sVal) - 1)
If x > 0 Then
  GetVariable = Left$(sVal, x)
  GetVariable = ""
End If
End Function

One other warning:  the SHELL function apparently passes the original
environment so any changes you make will not be seen by anything you start
using Shell.   You need to use the CreateProcess API call which lets you
pass a pointer to an environment block.  Also, these changes will not affect
any processes outside your VB app and any children you create.

Sat, 06 Oct 2001 03:00:00 GMT  
 [ 3 post ] 

 Relevant Pages 

1. API calls of winamp don't work in vb 6

2. MovePrevious don't work but the MoveNext work's fine

3. I've programmed a game that don't work Help

4. Script works, script don't work

5. Help? QueryDef parameters don't work

6. Help files don't work after running procedure

7. Help! Scrollbars Don't Work

8. AddNew DON'T work, Need HELP!!!

9. HELP! ListBox Don't work

10. VB4 help wanted : Data Controls don't work

11. Function keys don't work until I display help

12. Wizard created disks don't work - help please


Powered by phpBB® Forum Software