Solved

Setting Environment variables from VB5

Posted on 1998-09-02
5
214 Views
Last Modified: 2012-06-21
I want to set an NT environment variable from VB5. I can amend the registry entries at either HKEY_CURRENT_USER\Environment\ (User Variables) or HKEY_CURRENT_USER\Volatile Environment (general variables).

However, an NT command prompt or a COMMAND prompt does not notice the addition or change to these variables.

The only way I've found of getting NT to recognise the new variable or value is to open Control Panel, choose the System option and press OK.

Surely there must be an Api call to reread the registry settings?

I've search MSDN and Microsoft Knowledge Base to no avail.

Please help.
0
Comment
Question by:sstew
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
waty earned 100 total points
ID: 1432546
HOWTO: Propagating Environment Variables to the System
Last reviewed: May 27, 1997
Article ID: Q104011  
The information in this article applies to:
Microsoft Win32 Application Programming Interface (API) included with: - Microsoft Windows NT versions 3.5, 3.51, 4.0


SUMMARY
User environment variables can be modified by editing the following Registry key:


   HKEY_CURRENT_USER \
         Environment

System environment variables can be modified by editing the following Registry key:

   HKEY_LOCAL_MACHINE \
               SYSTEM \
    CurrentControlSet \
              Control \
      Session Manager \
          Environment

Note, however, that modifications to the environment variables do not result in immediate change. For example, if you start another Command Prompt after making the changes, the environment variables will reflect the previous (not the current) values. The changes do not take effect until you log off and then log back on.
To effect these changes without having to log off, broadcast a WM_SETTINGCHANGE message to all windows in the system, so that any interested applications (such as Program Manager, Task Manager, Control Panel, and so forth) can perform an update.



MORE INFORMATION
For example, on Windows NT, the following code fragment should propagate the changes to the environment variables used in the Command Prompt:


   SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, 0,
    (LPARAM) "Environment", SMTO_ABORTIFHUNG,
    5000, &dwReturnValue);

None of the applications that ship with Windows 95, including Program Manager and the shell, respond to this message. Thus, while this article can technically be implemented on Windows 95, there is no effect except to notify third-party applications. The only method of changing global environment variables on Windows 95 is to modify the autoexec.bat file and reboot.  

0
 
LVL 1

Author Comment

by:sstew
ID: 1432547
The code which is in this KB article requires a pointer to a string containing the description of the registry settings which have changed, ie the  (LPARAM) "Environment" path of the call. The LPARAM function is not valid in VB5 - How can I get a pointer to a string for this function to work correctly?
0
 
LVL 14

Expert Comment

by:waty
ID: 1432548
Did you tried the following declaration
Declare Function SendMessageTimeout Lib "user32" Alias "SendMessageTimeoutA" (ByVal hwnd As Long, ByVal msg As Long, ByVal wParam As Long, ByVal lParam As String, ByVal fuFlags As Long, ByVal uTimeout As Long, lpdwResult As Long) As Long

I should probably work as the SendMessage API :
Private Declare Function SendMessageStr Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As String) As Long

0
 
LVL 1

Author Comment

by:sstew
ID: 1432549
That declaration worked to a certain degree - I was using the one from the API viewer.

With a Windows NT command prompt open, I run my program which set the environment variable. However, when I do a SET command in the command prompt, the old value is shown. Only when I close the command prompt and reopen it is the new value available. This also happens with a NT DOS command prompt.

Any ideas why the change isn't being passed through?

By the way, you've earned your points. I'll give them to you on your next comment.
0
 
LVL 14

Expert Comment

by:waty
ID: 1432550
I think, when you open a command dos, it reads all the environment (like if you start your computer in dos, it reads the environment in the config.sys).

If you have to keep your dos box, you could maybe send through the clipboard commands ?
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

I’ve seen a number of people looking for examples of how to access web services from VB6.  I’ve been using a test harness I built in VB6 (using many resources I found online) that I use for small projects to work out how to communicate with web serv…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now