Skip to content
⌘ NSIS Forum Archive

Enviroment Variable not set when running installer Run as admin

9 posts

usmaan.saeed#

Enviroment Variable not set when running installer Run as admin

I am using below mention function to set Environment Variable but when i run that installer as Run as Admin it fails to set environment variable.

Function myfunc

!define ReadEnvStr_RegKey \
'HKLM "SYSTEM\CurrentControlSet\Control\Session Manager\Environment"'

Push JAVA_HOME
Push '${JAVA_HOME}'
Call WriteEnvStr

ReadEnvStr $R0 "PATH"

ReadRegStr $0 ${ReadEnvStr_RegKey} 'JAVA_HOME'

StrCpy $R0 "$R0;$0"

System::Call 'Kernel32::SetEnvironmentVariableA(t, t) i("PATH", R0).r0'
ReadEnvStr $R0 "PATH"

FunctionEnd

Please suggest the solutions.
Anders#
Environment variables work like this:

When you log on winlogon.exe reads the environment from the registry (might be cached somewhere though) and explorer.exe inherits these variables.

SetEnvironmentVariable changes the environment for the current process and all child processes started from this process.

Your code needs one final trick, add this after changing the registry:

!include Winmessages.nsh
SendMessage ${HWND_BROADCAST} ${WM_WININICHANGE} 0 "STR:Environment" /TIMEOUT=5000
Explorer.exe will then read from the registry and update it's environment. Other processes will not update so it still might not work but there is not much you can do about it...
Deepu kukreja#
I am also facing same problem

It works fine without run as administrator but i need an another task after this which must needs to run as administrator so i can not run my installer without "Run As Administrator"

Please help how can i set JAVA_HOME in environment variables with "Run as administrator"


Originally Posted by Anders View Post
Environment variables work like this:

When you log on winlogon.exe reads the environment from the registry (might be cached somewhere though) and explorer.exe inherits these variables.

SetEnvironmentVariable changes the environment for the current process and all child processes started from this process.

Your code needs one final trick, add this after changing the registry:



Explorer.exe will then read from the registry and update it's environment. Other processes will not update so it still might not work but there is not much you can do about it...
usmaan.saeed#
Above mention suggestion is not working fine in my case.
please!! suggest more for above mention my issue
usmaan.saeed#
i have check registry settings.
using above mention my function 'myfunc' sets environment variable for current user.
Now my question is how to set environment variable for System using NSIS.??
JasonFriday13#
I've been working on a plugin to work with environment variables, but it still isn't stable yet (dynamic memory allocation causes program exceptions, don't know where to start because the debug location changes most of the time, so it's just static allocation for now).

Also, it sometimes causes string corruption, so don't ask for a copy because I won't give one to you.

Regarding your post, you need Administrator access to write to the system environment variables.
Anders#
Originally Posted by usmaan.saeed View Post
i have check registry settings.
using above mention my function 'myfunc' sets environment variable for current user.
Now my question is how to set environment variable for System using NSIS.??
When you are writing to HKLM you are setting it for the system but some things might require a reboot to pick up the changes.

You really should investigate other solutions, don't solve a local problem with a global setting. What if the machine has both 32-bit and 64-bit Java installed? Multiple Java versions?

Have you tried one of the Java launchers? Or a simple NSIS loader application?
BobTman#
Originally Posted by Anders View Post
Environment variables work like this:

When you log on winlogon.exe reads the environment from the registry (might be cached somewhere though) and explorer.exe inherits these variables.

SetEnvironmentVariable changes the environment for the current process and all child processes started from this process.

Your code needs one final trick, add this after changing the registry:



Explorer.exe will then read from the registry and update it's environment. Other processes will not update so it still might not work but there is not much you can do about it...
Originally Posted by Anders View Post
When you are writing to HKLM you are setting it for the system but some things might require a reboot to pick up the changes.

You really should investigate other solutions, don't solve a local problem with a global setting. What if the machine has both 32-bit and 64-bit Java installed? Multiple Java versions?

Have you tried one of the Java launchers? Or a simple NSIS loader application?
Hi Anders, SendMessage on my machine took more than 5 minutes to completed but it ran OK on other machines, what could slow down SendMessage and cause it not timeout after 5 seconds?
Anders#
If you don't specify a timeout then it can hang as long as there is a toplevel window on your system that is not responding to messages. If you specify a timeout then NSIS calls SendMessageTimeout with the SMTO_NORMAL flag. The timeout is per window.

From MSDN:
If the message is a broadcast message, each window can use the full time-out period. For example, if you specify a five second time-out period and there are three top-level windows that fail to process the message, you could have up to a 15 second delay.