Avatar of TheFlyingCorpse
TheFlyingCorpse
Flag 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

.NET ProgrammingC#Windows XP

Avatar of undefined
Last Comment
TheFlyingCorpse

8/22/2022 - Mon
Dennis Aries

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
Dennis Aries

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
TheFlyingCorpse

ASKER
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!
All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck