Home / Software / Computer programming / Need proper syntax for getevn (or set/environ)
0 Members and 1 Guest are viewing this topic. « previous next »
Pages: [1] 2  All - (Bottom) Print
Author Topic: Need proper syntax for getevn (or set/environ)  (Read 408 times)
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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?
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,948

Computer: Specs
Experience: Beginner
OS: Unknown

1
« 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 » IP logged


Proud to be European
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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.
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,948

Computer: Specs
Experience: Beginner
OS: Unknown

1
« 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?



IP logged


Proud to be European
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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?
IP logged
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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
 
IP logged
Squashman
Hopeful



Thanked: 24
Posts: 340

Experience: Experienced
OS: Other



« 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.
IP logged
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« Reply #7 on: December 14, 2011, 03:08:10 PM »

curdir is the winner....
 
and the answer is... :D
IP logged
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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?
IP logged
BC_Programmer
Mastermind


Thanked: 697
Posts: 15,876

Computer: Specs
Experience: Beginner
OS: Windows 7


Pinkie Pie is best pony

BC-Programming.com 1 1
« 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")

IP logged

My Blog

BASeBlock 2.3.0 (NOW WITH MACGUFFINS!)
Salmon Trout
Sage



Thanked: 546
Posts: 7,948

Computer: Specs
Experience: Beginner
OS: Unknown

1
« 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.
IP logged


Proud to be European
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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.
IP logged
Squashman
Hopeful



Thanked: 24
Posts: 340

Experience: Experienced
OS: Other



« 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
IP logged
Salmon Trout
Sage



Thanked: 546
Posts: 7,948

Computer: Specs
Experience: Beginner
OS: Unknown

1
« 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!

IP logged


Proud to be European
skorpio07
Topic Starter
Rookie



Posts: 31

Computer: Specs
Experience: Guru
OS: Other


sniff sniff sniff

Fantasea Grafix
« 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).
IP logged
Pages: [1] 2  All - (Top) Print 
Home / Software / Computer programming / Need proper syntax for getevn (or set/environ) « previous next »
 


Login with username, password and session length

Old Forum Search | Forum Rules
Copyright © 2010 Computer Hope ® All rights reserved.
Powered by SMF 2.0 RC3 | SMF © 2006–2010, Simple Machines LLC
Page created in 0.162 seconds with 20 queries.