Hi Thomas,
This is what I do:
External function declarations:
function boolean SetEnvironmentVariable(ref string lpName, ref string lpBuffer) library "KERNEL32.DLL" Alias For "SetEnvironmentVariableW"
function boolean GetEnvironmentVariable(ref string lpName, ref string lpBuffer, ref ulong nSize) library "KERNEL32.DLL" Alias For "GetEnvironmentVariableW"
In the code below, there's a lot more than you need, like logging the set path to a txt file and other things, but just search on "SetEnvironmentVariable" and "GetEnvironmentVariable"
If you need more help, I can try to make a small sample app to achieve what you want, but I'm pretty busy right now, so it'll take me some time (like the weekend maybe).
// u6, v1, mjl, 12/11/19: have to set the path to the runtime SHARED in earlier stage:
// For checks on path's:
string ls_pathInsertList[]
int li_pathInsert
string ls_check_setPath, ls_what = 'PATH'
ulong lul_size = 2048
boolean lb_rc
string ls_temp, ls_value, ls_path2Shared, ls_path, ls_tempPath, ls_folder
//n_cst_platformunicode lnv_platform
boolean lb_valid_path = false
//f_SetPlatform(lnv_platform, TRUE)
//ls_path = lnv_platform.of_getPath()
ls_path = is_appDir // if not running the EXE, this won't work well, path is different
// see if we have a valid shared directory:
ls_path2Shared = mid(ls_path, 1, lastpos(ls_path, '\')) + "SHARED"
int li_rc, li_filewriterc, li_filewriterc2
n_cst_filesrv lnv_filesrv
li_rc = f_SetFilesrv_no_gnv_app(this, lnv_filesrv, True)
// u1, mjl, 16/01/17: get windows temp dir.
is_win_temp_dir = lnv_filesrv.of_getTempPath()
// u3, mjl, 17/10/17: string returned already ends on '\', but let's make sure
if len(is_win_temp_dir) > 0 then
if right(is_win_temp_dir, 1) <> '\' then
is_win_temp_dir += '\'
end if
end if
try
li_filewriterc = lnv_filesrv.of_FileWrite (ls_path + "\_setPathLog.txt", 'SETTING THE PATH:~r~n', FALSE)
catch ( throwable e)
// nothing
end try
// v1, mjl, 03/10/19: write to temp folder too, since might not have permissions to write in application's folder:
string lsWinTempDir
lsWinTempDir = this.is_win_temp_dir
if right(lsWinTempDir, 1 ) = '\' then
lsWinTempDir = Mid(lsWinTempDir, 1, len(lsWinTempDir) - 1)
end if
try
li_filewriterc2 = lnv_filesrv.of_FileWrite (lsWinTempDir + "\_setPathLog.txt", 'SETTING THE PATH:~r~n================', FALSE)
catch ( throwable e2)
// nothing
end try
// add ls_path2Shared to the userPath:
string ls_pathInsert = ""
// first we try to add the missing path:
ls_pathInsert = (ls_path2Shared + "\JRE\BIN;")
li_pathInsert ++
ls_pathInsertList[li_pathInsert] = ls_pathInsert
// first we try to add the missing path:
ls_pathInsert = ls_path2Shared + "\JRE;"
li_pathInsert ++
ls_pathInsertList[li_pathInsert] = ls_pathInsert
// first we try to add the missing path:
ls_pathInsert = (ls_path2Shared + ";" )
li_pathInsert ++
ls_pathInsertList[li_pathInsert] = ls_pathInsert
//ls_path = (ls_pathInsert + ls_path) // u2, mjl, 29/06/18: commented because I want \shared to be the first path added, then the rest:
int li_i
for li_i = 1 TO li_pathInsert
// v1, mjl, 31/07/19: only add the path if it's valid, if not could break the validity of following folders in the rest of the path:
if DirectoryExists( mid(ls_pathInsertList[li_i], 1, len(ls_pathInsertList[li_i]) - 1) ) then
ls_path = (ls_pathInsertList[li_i] + ls_path)
else
if li_rc > 0 then
try
li_filewriterc = lnv_filesrv.of_FileWrite (is_appdir + "\_setPathLog.txt", '~r~nTHE PARTIAL PATH:~r~n"' + mid(ls_pathInsertList[li_i], 1, len(ls_pathInsertList[li_i]) - 1) + '"~r~n, COULD NOT BE ACCESSED OR DOES NOT EXIST!', true)
catch ( throwable e3)
// nothing
end try
try
li_filewriterc2 = lnv_filesrv.of_FileWrite (is_win_temp_dir + "\_setPathLog.txt", '~r~nTHE PARTIAL PATH:~r~n"' + mid(ls_pathInsertList[li_i], 1, len(ls_pathInsertList[li_i]) - 1) + '"~r~n, COULD NOT BE ACCESSED OR DOES NOT EXIST!', true)
catch ( throwable e4)
// nothing
end try
end if
end if
next
if isnull(ls_path) then ls_path = ""
// u6, v1, mjl, 12/11/19: append the current path to new path:
string ls_oldPath
boolean lb_success
ls_oldPath = space(lul_size)
lb_success = GetEnvironmentVariable( ref ls_what, ref ls_oldPath, ref lul_size)
ls_oldPath = trim(ls_oldPath)
if NOT lb_success then
ls_oldPath = ""
end if
ls_path += ";" + ls_oldPath
if li_rc > 0 then
try
li_filewriterc = lnv_filesrv.of_FileWrite (is_appdir + "\_setPathLog.txt", "~r~nSETTING THE PATH TO:~r~n" + ls_path, true)
catch ( throwable e5)
// nothing
end try
try
li_filewriterc2 = lnv_filesrv.of_FileWrite (is_win_temp_dir + "\_setPathLog.txt", "~r~nSETTING THE PATH TO:~r~n" + ls_path, true)
catch ( throwable e6)
// nothing
end try
end if
lb_success = SetEnvironmentVariable(ref ls_what, ref ls_path)
if NOT lb_success then
if li_rc > 0 then
try
li_filewriterc = lnv_filesrv.of_FileWrite (is_appdir + "\_setPathLog.txt", "~r~nFAILED TO SET THE PATH!" + ls_path, true)
catch ( throwable e7)
// nothing
end try
try
li_filewriterc2 = lnv_filesrv.of_FileWrite (is_win_temp_dir + "\_setPathLog.txt", "~r~nFAILED TO SET THE PATH!" + ls_path, true)
catch ( throwable e8)
// nothing
end try
end if
Messagebox("Connection Error", "Failed to include the Path to the SHARED folder: ~r~n" + &
"( " + ls_path2Shared + " ).~r~n~r~nPlease contact Credica.", StopSign!)
lb_valid_path = false
else
// v1, mjl, 31/07/19: double check if it really worked:
ls_check_setPath = space(lul_size)
lb_rc = GetEnvironmentVariable( ref ls_what, ref ls_check_setPath, ref lul_size)
ls_check_setPath = trim(ls_check_setPath)
if NOT lb_rc then
ls_check_setPath = ""
end if
if pos(ls_check_setPath, ls_path) > 0 then
lb_valid_path = true // in case it wasn't
if li_rc > 0 then
try
li_filewriterc = lnv_filesrv.of_FileWrite (is_appdir + "\_setPathLog.txt", '~r~n~r~n"OF_SETPATH_RUNTIME()" HAS SUCCEEDED TO SET THE PATH.', true)
catch ( throwable e9)
// nothing
end try
try
li_filewriterc2 = lnv_filesrv.of_FileWrite (is_win_temp_dir + "\_setPathLog.txt", '~r~n~r~n"OF_SETPATH_RUNTIME()" HAS SUCCEEDED TO SET THE PATH.', true)
catch ( throwable e10)
// nothing
end try
end if
else
if li_rc > 0 then
try
li_filewriterc = lnv_filesrv.of_FileWrite (is_appdir + "\_setPathLog.txt", '~r~n~r~nSOMEHOW "OF_SETPATH_RUNTIME()" HAST FAILED TO SET THE PATH AND THE CURRENT PATH IS:~r~n' + ls_path, true)
catch ( throwable e11)
// nothing
end try
try
li_filewriterc2 = lnv_filesrv.of_FileWrite (is_win_temp_dir + "\_setPathLog.txt", '~r~n~r~nSOMEHOW "OF_SETPATH_RUNTIME()" HAST FAILED TO SET THE PATH AND THE CURRENT PATH IS:~r~n' + ls_path, true)
catch ( throwable e12)
// nothing
end try
end if
lb_valid_path = false
end if
end if
if li_rc > 0 then
li_rc = f_SetFilesrv_no_gnv_app(this, lnv_filesrv, false)
end if
if NOT lb_valid_path then
messagebox("Error Shared Directory", "Your environment's PATH variable does not contain a valid SHARED directory")
end if
Tom