pindare
pindare

Reputation: 2470

environment variables propagation on Windows system

It is possible to propagate in already opened application the value(environment variables of Windows) of a variable of Windows after its creation or its modification without having to restart the applications which turn?

How to?

Perhaps, using server fault to post a such question would be better?

Upvotes: 1

Views: 1820

Answers (3)

Anders
Anders

Reputation: 101569

Something like SendMessage(HWND_BROADCAST,WM_SETTINGCHANGE,0,TEXT("Environment")) is your best bet, but most applications will ignore it, but Explorer should handle it. (Allow applications to pick up updates)

If you want to go into crazy undocumented land, you could use WriteProcessMemory and update the environment block in every process you have access to.

Upvotes: 4

Stephen Kellett
Stephen Kellett

Reputation: 3236

Yes, this is possible.

Method

It is involved though. I'll outline the basic steps. The detail for each step is documented in many places on the web, including Stack Overflow.

  1. Create a helper dll. The dll does nothing except set the environment variables you want to set. It can do this from DllMain without causing any problems. Just don't got mad with other function calls from inside DllMain. How you communicate to the DLL what variables to set and what values to set them is left for you to decide (read a file, read from registry...)

  2. Enumerate all processes that you wish to update (toolhelp32 will help with this).

  3. For each process you wish to update, inject your helper dll. CreateRemoteThread() will help with this. This will fail for 2% of all apps on NT 4, rising to 5% on XP. Most likely higher percentage failures for Vista/7 and the server versions.

Things you have to live with:

If you are running a 32 bit process on a 64 bit OS, CreateRemoteThread will fail to inject your DLL into 32 bit apps 100% of the time (and cannot inject into 64 bit apps anyway as that is a job for a 64 bit app).

EDIT: Turns out 100% isn't correct. But it is very hit and miss. Don't rely on it.

Don't remain resident

If you don't want your helper DLL to remain resident in the target application, return FALSE for the DLL_PROCESS_ATTACH notification.

BOOL APIENTRY DllMain(HANDLE hModule,
                      DWORD  ul_reason_for_call,
                      LPVOID lpReserved)
{
    if (ul_reason_for_call == DLL_PROCESS_ATTACH)
    {
        // set our env vars here

        SetEnvironmentVariable("weebles", "wobble but they don't fall down");

        // we don't want to remain resident, our work is done

        return FALSE;
    }

    return TRUE;
}

Upvotes: 3

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

No, I'm pretty sure that's not possible.

Upvotes: 2

Related Questions