Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Setting Environment variables from VB5

Posted on 1998-09-02
5
Medium Priority
?
230 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 14

Accepted Solution

by:
waty earned 200 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses

671 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