Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Need proper syntax for getevn (or set/environ)  (Read 7957 times)

0 Members and 1 Guest are viewing this topic.

skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Need proper syntax for getevn (or set/environ)
« on: December 14, 2011, 10:58:39 AM »
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?

Salmon Trout

  • Guest
Re: Need proper syntax for getevn (or set/environ)
« Reply #1 on: December 14, 2011, 11:11:53 AM »
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")
 




« Last Edit: December 14, 2011, 11:36:14 AM by Salmon Trout »

skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #2 on: December 14, 2011, 11:56:38 AM »
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
 
I just changed getenv to environ and it worked fine.

Salmon Trout

  • Guest
Re: Need proper syntax for getevn (or set/environ)
« Reply #3 on: December 14, 2011, 12:08:14 PM »
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?




skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #4 on: December 14, 2011, 12:40:02 PM »
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  :)
 
4. Are you actually looking at the compiler output?

skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #5 on: December 14, 2011, 02:30:49 PM »
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
 

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #6 on: December 14, 2011, 02:55:52 PM »
Not sure if you need to use th curdir or exepath commands. Not exactly sure which path you are looking for.

skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #7 on: December 14, 2011, 03:08:10 PM »
curdir is the winner....
 
and the answer is... :D

skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #8 on: December 15, 2011, 09:38:24 AM »
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?

BC_Programmer


    Mastermind
  • Typing is no substitute for thinking.
  • Thanked: 1140
    • Yes
    • Yes
    • BC-Programming.com
  • Certifications: List
  • Computer: Specs
  • Experience: Beginner
  • OS: Windows 11
Re: Need proper syntax for getevn (or set/environ)
« Reply #9 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).

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 was trying to dereference Null Pointers before it was cool.

Salmon Trout

  • Guest
Re: Need proper syntax for getevn (or set/environ)
« Reply #10 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.

skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #11 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  :o  to move my batch file into a higher level language???? is there some tribunal  :-X  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!!!  ;D
 
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.

Squashman



    Specialist
  • Thanked: 134
  • Experience: Experienced
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #12 on: December 15, 2011, 12:17:24 PM »
These two statements kind of contradict themselves.
not to hide, but the source file is getting a bit large so as a way of making it run faster.

is it the fact that my routine is so simple that you can't wrap your head around it

Salmon Trout

  • Guest
Re: Need proper syntax for getevn (or set/environ)
« Reply #13 on: December 15, 2011, 12:21:28 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  :o  to move my batch file into a higher level language???? is there some tribunal  :-X  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!!!  ;D

Oh dear!


skorpio07

    Topic Starter


    Rookie
  • sniff sniff sniff
    • Fantasea Grafix
  • Computer: Specs
  • Experience: Guru
  • OS: Other
Re: Need proper syntax for getevn (or set/environ)
« Reply #14 on: December 15, 2011, 12:22:21 PM »
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.
 
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).