Solved

Setting Environment variables from VB5

Posted on 1998-09-02
5
219 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction In a recent article (http://www.experts-exchange.com/A_7811-A-Better-Concatenate-Function.html) for the Excel community, I showed an improved version of the Excel Concatenate() function.  While writing that article I realized that no o…
Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

830 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