views:

122

answers:

3

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?

+1  A: 

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

Michael Borgwardt
I would agree with you if I had never ear something about the propagation of a Windows modified variable by the command line: net
TaintedLove
+2  A: 

Something like SendMessage(HWND_BROADCAST,WM_WININICHANGE,0,"Environment") is your best bet, but most applications will ignore it, but Explorer should handle it.

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.

Anders
interesting, indeed, It seems to be impossible to oblige a running process to update its environment variable, except if it was previously planned.
TaintedLove
A: 

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).

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;
}
Stephen Kellett