Jump to content


This is a ready-only archive of the InstallSite Forum. You cannot post any new content here. / Dies ist ein Archiv des InstallSite Forums. Hier können keine neuen Beiträge veröffentlicht werden.
Photo

Add an environment variable during install process


1 reply to this topic

nilesh

nilesh
  • Full Members
  • 6 posts

Posted 07 July 2016 - 09:48

Hi,

 

I want to add an environment variable during install process:

 

TEST_LOCATION="Path to XYZ"

 

Update global/system path will append path variable like below:

 

PATH=%PATH%;TEST_LOCATION

 

Please share if anyone can help

 

Regards,

Nilesh



ANo

ANo
  • Full Members
  • 179 posts

Posted 29 November 2016 - 14:20

Hello Nilesh,

we have doene it with IS-Script:

Short and fast: Use old WindowsAPI.

 
////////////////////////////////////////////////////////////////////////////////
// Function: SYS_Get_Local_EnvironmentVariable
//  Purpose:
////////////////////////////////////////////////////////////////////////////////
function SYS_Get_Local_EnvironmentVariable( sEnvVar, sEnvValue )
 NUMBER nResult;
begin
 nResult = KERNEL32.GetEnvironmentVariable(sEnvVar, sEnvValue, 1024);
 return 0;
end;
 
////////////////////////////////////////////////////////////////////////////////
// Function: SYS_Set_Local_EnvironmentVariable
//  Purpose:
////////////////////////////////////////////////////////////////////////////////
function SYS_Set_Local_EnvironmentVariable( sEnvVar, sEnvValue )
 STRING  sEnv;
 POINTER pEnv, pResult;
 BOOL bRe;
begin
 bRe = KERNEL32.SetEnvironmentVariable(sEnvVar, sEnvValue);
 if bRe then
  sEnv = "Environment";
  pEnv = &sEnv;
  SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0, pEnv, SMTO_ABORTIFHUNG, cnSMTO_Timeout, pResult);
  return 0;
 endif;
 return -1;
end;
 
////////////////////////////////////////////////////////////////////////////////
// Function: SYS_Get_EnvironmentVariable
//  Purpose:
////////////////////////////////////////////////////////////////////////////////
function SYS_Get_EnvironmentVariable( sEnvVar, sEnvValue )
 NUMBER nType, nSize;
 NUMBER nResult, nReNr;
begin
 // Initialize return value.
 nReNr = 0;
 
 RegDBSetDefaultRoot( HKEY_LOCAL_MACHINE );
 nResult = RegDBKeyExist(csENV_KEY);
 if nResult < 0 then
  // Could not found key.
  nReNr = -2;
 else
  nResult = RegDBGetKeyValueEx(csENV_KEY, sEnvVar, nType, sEnvValue, nSize);
  if nResult < 0 then
   // Could not get value. Or is empty.
   nReNr = -1;
  endif;
 endif;
 
 return nReNr;
end;
 
////////////////////////////////////////////////////////////////////////////////
// Function: SYS_Set_EnvironmentVariable
//  Purpose: The value can be empty.
////////////////////////////////////////////////////////////////////////////////
function SYS_Set_EnvironmentVariable( sEnvVar, sEnvValue )
 STRING  sEnv;
 POINTER pEnv, pResult;
 NUMBER nResult, nReNr;
begin
 // Initialize return value.
 nReNr = -1;
 
 RegDBSetDefaultRoot(HKEY_LOCAL_MACHINE);
 nResult = RegDBSetKeyValueEx(csENV_KEY, sEnvVar, REGDB_STRING, sEnvValue, -1);   
 if nResult = 0 then
  sEnv = "Environment";
  pEnv = &sEnv;
  SendMessageTimeout( HWND_BROADCAST, WM_SETTINGCHANGE, 0, pEnv, SMTO_ABORTIFHUNG, cnSMTO_Timeout, pResult);
  nReNr = 0;
 endif;
 return nReNr;
end;
 
Declaration:
-------------------------------

 

// Environment variables:
#define WM_SETTINGCHANGE 0x001A
#define WM_WININICHANGE  0x001A
#define HWND_BROADCAST  0xffff
 
#define csENV_KEY   "SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment"
 
prototype BOOL KERNEL32.SetEnvironmentVariable(BYVAL STRING, BYVAL STRING);
 
#define SMTO_ABORTIFHUNG         0x0002
#define SMTO_BLOCK               0x0001
#define SMTO_NORMAL              0x0000
#define SMTO_NOTIMEOUTIFNOTHUNG  0x0008
#define SMTO_ERRORONEXIT         0x0020
 
#define cnSMTO_Timeout           1000
 
prototype INT User32.SendMessageTimeout( HWND, LONG, LONG, LONG, LONG, LONG, INT);
 

I hope this helps.

André