Solved

Setting Environment variables from VB5

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VBA error replacing data 6 37
How does CurrentUser work? 10 31
VB6 - Convert HH:MM into Decimal 8 52
Create Files based on Cell Values in a Range in Excel 12 20
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 (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

895 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