jonathanbrickman0000 wrote:That comes awful close to nearly-malware :) Definitely, if I installed any code off the Net that did that to my machine without very explicit permission, it would be deinstalled very quickly!
;-) Look at your PATH lately? The last thing I installed off the 'net was GNU wget. It modded the global PATH (I of course installed as Admin..).
XP is (still) a Great Gaping Security Hole. A regular user can (still) cripple the OS, either deliberately or with the help of malware/viruses. I administered University Unix systems for years, opened them up to students for external access, never had a problem (OK, one version of Apache was hacked once) - and they
tried to break them.
Sigh. I think that VISTA will allow me to RUNAS a regular user, with Admin access as needed (when accessed). But I'll have to be dragged to that upgrade kicking and screaming.
-----
Ok, here's Set/GetEnv (see Notes:):
Code: Select all
'set/getenv, Windows only - REQUIRES WinNT or newer!
' Don't use on/for Win9x
' tested using fb v0.16 release on XP only
'Mods the registry - use at your own risk!
option explicit
#include "windows.bi"
#include "win\winreg.bi"
#define HKCU (CAST(HKEY,&H80000001))
#define SAFE
declare function SetEnv(mykey as string,myval as string) as integer
declare function GetEnv(mykey as string) as string
dim as string mykey,myval,envres
mykey="Zippy" 'key in HKCU\Environment to set
myval="c:" 'value to set
SetEnv(mykey,myval)
envres=GetEnv(mykey)
print mykey;"=";envres
1 'mykey="PATH"
2 'myval="%PATH%" & ";z:"
3 'SetEnv(mykey,myval)
4 'envres=GetEnv(mykey)
5 'print mykey;"=";envres
sleep
end
'''''''''''''''''''''''''''''''''''
function SetEnv(mykey as string,myval as string) as integer
dim as HKEY keyhandle
dim as integer res
#ifdef SAFE
if left(mykey,1)<>"Z" then
beep
print "Key must begin with 'Z',"
print " sleeping to EXIT.."
sleep
end
end if
#endif
res=RegCreateKeyEx(HKCU,_
"Environment",_
0,_
0,_
REG_OPTION_NON_VOLATILE,_
KEY_SET_VALUE,_
NULL,_
@keyhandle,_
0)
res=RegSetValueEx(keyhandle,_
strptr(mykey),_
0,_
REG_SZ,_
strptr(myval),_
len(myval))
RegCloseKey(keyhandle)
return 0
end function
'
function GetEnv(mykey as string) as string
dim as HKEY keyhandle
dim as integer res,dtype,lenvbuf
dim as string envbuf
if len(mykey)<1 then
beep
print "Invalid Key..,"
print " sleeping to EXIT.."
sleep
end
end if
res=RegCreateKeyEx(HKCU,_
"Environment",_
0,_
0,_
REG_OPTION_NON_VOLATILE,_
KEY_QUERY_VALUE ,_
NULL,_
@keyhandle,_
0)
envbuf=space(2048)
lenvbuf=len(envbuf)
res=RegQueryValueEx(keyhandle,_
strptr(mykey),_
NULL,_
@dtype,_
strptr(envbuf),_
@lenvbuf)
RegCloseKey(keyhandle)
return left(trim(envbuf),lenvbuf)
end function
Notes:
1. @yetifoot and @oldirty contributed to this code, albeit I doubt they know this.. This addresses only HKCU\Environment.
2. SetEnv() is crippled, deliberately. There's zero error checking beyond that the key must begin with a "Z", change at will but don't cry to me if you hose your system. Comment-out "#define SAFE" if you want to enable/write arbitrary environ vars.
3. There doesn't appear to be a reliable method to notify all programs of a/the changed environment, short of a reboot or relogin. Thus "GetEnv()" which does reflect all changes. fb environ() does not (but does/will after reboot/relogin) - this is NOT a bug in fb, rather it is expected behavior.
4. Both Set/GetEnv are string-based. REG_SZ.
5. The user's PATH can be modded without changing the global PATH, which
may have been the original question/problem.. One should either append or prepend to the global path, not replace it outright. Lines #1-#5 demo an append to PATH (requires the "enable" in Note 2 above).
ETA: Requires WinNT or newer