1.

Solve : Need proper syntax for getevn (or set/environ)?

Answer»

trying to get this to print the environment variable "PATH"
 
here is what is in the freebasic compiler
 
#include "crt/stdlib.bi"
Sub main()
 Print getenv("PATH")
End Sub
main()
 
i have also tried to change getenv to environ (i found a few posts for this. fbwiki).
 
tried this as well:
print *getenv("PATH")
 
what am i doing wrong?Both these work fine for me (FreeBASIC version 0.22.0 for win32)

Code: [Select]#include "crt/stdlib.bi"
Sub main()
 Print environ("PATH")
End Sub
main()

Code: [Select]#include "crt/stdlib.bi"
Print *getenv("PATH")
 



maybe something is not configure quite right then as i did the following:
#include "crt/stdlib.bi"
Sub main()
 Print environ("PATH")
End Sub
main()
 
when i run the program, nothing but a blank lf/cf is returned..
 
i checked the freebasic folder and the crt is found under ..\FreeBASIC\inc\crt and i verified that the stdlib.bi exists there and in the ..\inc\crt\win32 folder as well.
 
is it possible that it is not including the .bi file? is there a way to test that it is loading properly?
 
thanks
 
Quote from: Salmon Trout on December 14, 2011, 11:11:53 AM

I just changed getenv to environ and it worked fine.
1. Does the PATH environment string actually have a value? Sometimes people fool around and nuke it.

2. What does this code do?

Code: [Select]#include "crt/stdlib.bi"
Sub main()
 Print "Homedrive: " & environ("HOMEDRIVE")
 Print "OS:        " & environ("OS")
 Print "PROMPT:    " & environ("PROMPT")
End Sub
main()

You should see something like this

Code: [Select]Homedrive: C:
OS:        Windows_NT
PROMPT:    $p$g
3. If the lib was not found you would get an ERROR message from the compiler and no exe would be produced. Try changing stlib to stdlibb and see what happens!

4. Are you actually looking at the compiler output?



No, i was using FEdit and had inadvertently chosen Windows GUI instead of Windows Console,
 
all is good now.
 
So SalmonTrout, i am now gettig the correct path. many thanks for your astutness 
 
Quote from: Salmon Trout on December 14, 2011, 12:08:14 PM
4. Are you actually looking at the compiler output?
so after some more tweaking, i have things sort of being ported over to the new lang.. again im primarily setting and displaying environment variables.
 
ONE thing that i have not figured out is this:
 
#include "crt/stdlib.bi"
Dim return1 As String, testdir As String
return1 = Environ("return")
testdir = Environ("%cd%")
Print "return is "; return1
Print "testdir is "; testdir
 
return1 is valid and prints out the value
testdir prints out the statement and no value, so somehow the percent signs seem to give the environ command an issue
 
how can i pass the %cd% to the environ function, i have tried quoting, single quoting, with and without %, nothing has worked so far.
 
any thoughts
 Not sure if you need to use th curdir or exepath commands. Not exactly sure which path you are looking for.curdir is the winner....
 
and the answer is... so some more issues with this not working as expected, so i need to learn more...
 
here is what i currently have in the FEDIT console window:
 
#include "crt/stdlib.bi"
Dim return1 As String, currdir As String, newpath As String
return1 = Environ("return")                                 'display the current value of return
currdir = curdir                                                 'set the current directory to currdir
Print "return is "; return1                                    'print the current value of return1 (not slot)
Print "current dir is "; currdir                               'print the current working directory
newpath="return=" + currdir                               'make new path string for setenviron
SetEnviron newpath                                          'setenviron to newpath
Print Environ("return")                                       'print the new value of "return"
 
during runtime, this does exactly what it is supposed to do, set the envvar "return" to the curdir. and it prints out exactly what it is supposed to say, which is the folder where im compiling the program at. all is good to this point.
 
however once it exits the program, the value for "return" is nil or not set.
 
thoughts?The Environment Block of a process is a copy of the environment block of the parent process, or a new, default Environment Block (depending how you spawn the child process).

In this case, the Environment Block your FreeBasic Application receives is a copy of the Environment Block as it exists in the spawning CMD console.

However, because of this, changes will not persist back to the parent processes environment.

With Older MS-DOS programs this wasn't an issue since... it didn't exist. The concept of multiple processes was a foreign one, and there really was only one environment block for each command interpreter that was globally accessible to any applications run within.


Instead you have to write a value to the registry. (HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment).

This does that (can be confirmed be via RegEdit) but changes don't take effect until a reboot... (Even though I Broadcast the required WM_SETTINGSCHANGE. Oh well.

Code: [Select]#include once "windows.bi"







'Windows Master Registry Block is stored in the registry,

'at HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment" as REG_SZ's



Declare Function SetEnvironment(VarName As String, VarValue As String) As Boolean



Const WM_SETTINGSCHANGE = &H1A

Function SetEnvironment(VarName As String,VarValue As String) As Boolean

    Dim UseRoot as HKEY,usekey As String

    Dim hk as HKEY

    Dim returnvalue As Boolean

    returnvalue=False

    UseRoot=HKEY_LOCAL_MACHINE

    UseKey= "SYSTEM\CurrentControlSet\Control\Session Manager\Environment\"

    'Open the key...

    If RegOpenKeyEx(UseRoot,UseKey,0,KEY_ALL_ACCESS,hk) = ERROR_SUCCESS Then

       'hk is a pointer to the key, use it with RegSetValueEx()...

        SetLastError(0)

        if RegSetValueEx(hk,StrPtr(VarName),0,REG_SZ,StrPtr(VarValue),Len(VarValue)+1)<>ERROR_SUCCESS Then

            Print "RegSetValueEx Failed." + "GetLastError returns " + Str(GetLastError())

        End If



        RegCloseKey(hk)

    Else

       

        Print "RegOpenKeyEx Failed. GetLastError() returns " + Str(GetLastError())

    End If

    PostMessage HWND_BROADCAST,WM_SETTINGSCHANGE,0,StrPtr("Environment")

    return returnvalue

End Function





SetEnvironment("Testing","howdy")

I keep wondering why the OP doesn't just use a cmd script ("batch file") to do the things that he wants, and he never answers. This makes me wonder if he is trying to do some kind of script-kiddie thing. Since he steadfastly refuses to tell us, the help he is going to get will not be focused.
i dont' know how much clearer than to post my code to make you happy    (i did post the routine that is not working). Im not a scriptkiddie nor a kid at all, im a director of a marketing firm and have a need to take my batch files (that run on an sql server) and make them faster (and now with more capabilities, having moved to freebasic.)
 
i have been doing things in the batch file with no issue's but wanted to make things faster, so am i allowed by you    to move my batch file into a higher level language? is there some tribunal    that i need to attend prior to GETTING help from you??? is it the fact that my routine is so simple that you can't wrap your head around it and wonder if i have an ulterior motive???    Did I not tip at the gate the proper amount??? Should I move to another forum to get help??? Who's in charge, I want to talk to the management about this!!! Outrageous! Preposterous! Bullocks!!! 
 
Quote from: Salmon Trout on December 15, 2011, 11:02:53 AM
I keep wondering why the OP doesn't just use a cmd script ("batch file") to do the things that he wants, and he never answers. This makes me wonder if he is trying to do some kind of script-kiddie thing. Since he steadfastly refuses to tell us, the help he is going to get will not be focused.
These two statements kind of contradict themselves.
Quote from: skorpio07 on December 13, 2011, 03:04:21 PM
not to hide, but the source file is getting a bit large so as a way of making it run faster.

Quote from: skorpio07 on December 15, 2011, 12:04:08 PM
is it the fact that my routine is so simple that you can't wrap your head around it
Quote from: skorpio07 on December 15, 2011, 12:04:08 PM
i dont' know how much clearer than to post my code to make you happy    (i did post the routine that is not working). Im not a scriptkiddie nor a kid at all, im a director of a marketing firm and have a need to take my batch files (that run on an sql server) and make them faster (and now with more capabilities, having moved to freebasic.)
 
i have been doing things in the batch file with no issue's but wanted to make things faster, so am i allowed by you    to move my batch file into a higher level language? is there some tribunal    that i need to attend prior to getting help from you??? is it the fact that my routine is so simple that you can't wrap your head around it and wonder if i have an ulterior motive???    Did I not tip at the gate the proper amount??? Should I move to another forum to get help??? Who's in charge, I want to talk to the management about this!!! Outrageous! Preposterous! Bullocks!!! 

Oh dear!

Thank you BC for your very detailed response. I appreciate the help that your giving alot. couple of quick questions.
 
in my batch file, when it gets to the part of the code that sets the environment variables, i had to run this:
 
if not "%return%"==""  setx return %cd%  &  set return=%cd%
 
that way i set the new global (via setx for FUTURE windows) envvar and then set the local var for the current (parent) window and it works just fine as a batch file. but if i want to move up to freebasic, how to program the same functionality in it.
 
i knew that there were options to create the regkeys but didnt think i needed that till now, maybe a better solution..
 
but if i use the regkey solution, is there a way to have it promote itself back to the parent window? would need the envvar to "stick" upon program completion.
 
again, many thanks for your detailed explanations, i will learn from this.
 
Quote from: BC_Programmer on December 15, 2011, 10:52:13 AM
The Environment Block of a process is a copy of the environment block of the parent process, or a new, default Environment Block (depending how you spawn the child process).


Discussion

No Comment Found