Link to home
Create AccountLog in
Avatar of TheFlyingCorpse
TheFlyingCorpseFlag for Norway

asked on

Publish that Environment Variables have been updated

Hi,

I am using SETX from Microsoft's Support Tools to set Environment Variables during logon. Another process I have needs the information from those Environment Variables I have set.

AFAIK SETX does not do the SendMessage function to notify applications that there is an update to Environment Variables, and I've been looking for ways to do this since browsing the internet does not yield simple applications that just invoke this function and then end.


I've found a C# sample on how to import the user32.dll which has this function, but not what code I need to use to invoke the SendMessage function.

Hints or samples of what I need to enclose this to a working program would be awsome ;)

[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);

Open in new window

Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Avatar of TheFlyingCorpse

ASKER

yes, it helped me a great deal =)

Sadly, I dont know if my code works, it works only if run by administrators.

On a sidenote, I dont know if the program I wrote it to work against will actually accept this, Novell Application Explorer. If the program "I" wrote works(unable to verify), then its probable that NAL itself is not listening for these broadcasts and able to update.


Any ideas?
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
 
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            SetVariable();
        }
 
        private static void SetVariable()
        {
            int result;
            SendMessageTimeout((System.IntPtr)HWND_BROADCAST,
                WM_SETTINGCHANGE, 0, "Environment", SMTO_BLOCK | SMTO_ABORTIFHUNG |
                SMTO_NOTIMEOUTIFNOTHUNG, 5000, out result);
            if (result == 0)
                Console.WriteLine("It works");
            else
                Console.WriteLine("Errorcode " + result.ToString());
        }
        [DllImport("user32.dll",
             CharSet = CharSet.Auto, SetLastError = true)]
        [return: MarshalAs(UnmanagedType.Bool)]
        public static extern bool
            SendMessageTimeout(
            IntPtr hWnd,
            int Msg,
            int wParam,
            string lParam,
            int fuFlags,
            int uTimeout,
            out int lpdwResult
            );
 
        public const int HWND_BROADCAST = 0xffff;
        public const int WM_SETTINGCHANGE = 0x001A;
        public const int SMTO_NORMAL = 0x0000;
        public const int SMTO_BLOCK = 0x0001;
        public const int SMTO_ABORTIFHUNG = 0x0002;
        public const int SMTO_NOTIMEOUTIFNOTHUNG = 0x0008;
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Dennis Aries
Dennis Aries
Flag of Netherlands image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Very good help and also helpful in verifying the application that was written works, but the inded destination program does not use this feature anyway.

Excellent help!