Jump to content

Win32 Server How To Set Environment Variables?

- - - - -

  • Please log in to reply
14 replies to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US

Quote

The server will set environmental variables available to the shell of which runs the CGI program, Apache could use mod_env for example.
(not sure how to cite this, but it's by Alexander, from the thread: http://forum.codecal...dress-http.html)

I got this reply from another thread, when I asked how the server tells the CGI program what the remote IP address is; how would I set the environment variables, if I was developing the server?

#2
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
In assembly language? Dude, I think this is the wrong subforum for that. You should probably go to the C/C++ or PHP forums, I'd imagine.
sudo rm -rf /

#3
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
The thing with that is that I use the MASM32 assembler for the server. Would the answer differ much, if I asked the question in the C/C++ forum (because I know Win32 programming is very similar with both, assembly and C/C++)?

#4
Gunner

Gunner

    Learning Programmer

  • Members
  • PipPipPip
  • 45 posts

RhetoricalRuvim said:

The thing with that is that I use the MASM32 assembler for the server. Would the answer differ much, if I asked the question in the C/C++ forum (because I know Win32 programming is very similar with both, assembly and C/C++)?

If you are writing a server, then just create the logic to store the vars your server needs/creates, AFAIK windows does not have a "remote IP" environment var (Maybe windows server does)... so when the remote connects to your server app, get the ip and save to a config file or a database...

What does C/C++ have to do with anything? Cause if you can do Win32 in C, then you sure as hell can do it in Assembly (might not be as easy but you can do it) C++ is a different story...

What you can do is call SetEnvironmentVariable and try creating RemoteIP for the user logged in... if you want it system wide, have admin rights and create a RemoteIP in HKEY_LOCAL_MACHINE\System\CurrentControlSet\Control\Session Manager\Environment then do something like:

.data
szEnviron BYTE "Environment", 0
.code
invoke SendMessage, HWND_BROADCAST, WM_SETTINGCHANGE, 0, offset szEnviron

to notify the apps and system you changed/created a setting

#5
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
There's this one thing I still don't understand. If the server gets a request for, let's say, test.pl, and then test.pl would send a request to that same server requesting for test2.pl, but the environment variables are already set for test.pl; how would the server handle that? I mean, can there be more than one of those environment variables set at a time?

#6
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
Each CGI process gets its own copy of the environment variables when it's started up. That way, it can't mess with other processes and each can have different data.
sudo rm -rf /

#7
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
So since the CreateProcess function uses the environment variables from the current process for the new process, if the fourth from the last parameter is null, then should I just set the environment variables within the server, start the CGI process, and then delete the environment variables?

#8
dargueta

dargueta

    Writes binary right handed and hex left handed

  • Moderators
  • 4,705 posts
  • Programming Language:C, Java, C++, PHP, Python, Perl, Assembly, Bash, Others
  • Learning:JavaScript
I guess, but I don't know why you'd do that.
sudo rm -rf /

#9
jakash3

jakash3

    Newbie

  • Members
  • PipPip
  • 21 posts
Just call CreateProcess, it has an option to set the environment of the process.
MSDN CreateProcess function: CreateProcess Function (Windows)
The environment is a null terminated array of C-style strings. Most of the arguments for this function can be NULL for default.
push 0       ;lpProcessInformation
push 0       ;lpStartupInfo
push 0       ;lpCurrentdirectory
push env     ;lpEnvironment
push 0       ;dwCreationFlags
push 0       ;bInheritHandles
push 0       ;lpThreadAttributes
push 0       ;lpProcessAttributes
push args    ;lpCommandLine
push fname   ;lpApplicationName
call [CreateProcessA]

fname db "foo.exe",0
args db "my program arguments",0
env db "var1=blah",0,"var2=foo",0,"bar=foobar",0,0
Also, programs can get and set their own environment variables using GetEnvironmentVariable and SetEnvironmentVariable function respectively.


#10
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
So the environment parameter is a pointer to that array?

#11
jakash3

jakash3

    Newbie

  • Members
  • PipPip
  • 21 posts

RhetoricalRuvim said:

So the environment parameter is a pointer to that array?
Yes.
Each variable in the block looks like:
name=value\0
The environment pointer could point to data that looks like this (in C string format):
PATH=C:\\WINDOWS;C:\\WINDOWS\\system32\0DATA=foobar qwerty asdf\0HOST=192.168.0.1\0\0
The double null termination denotes the end of the block.

#12
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,252 posts
  • Location:C:\Countries\US
Now I'm more confused. Is it a pointer to an array of pointers to strings, or is it a pointer to a string containing multiple strings?

So is it this?:
env_var_block: 

db "SERVER_SOFTWARE=whatevertheservernameis", 0 

db "REMOTE_ADDR=127.0.0.1", 0 

db 0 

Or is it like this?:
env_var_block: 

dd offset var1 

dd offset var2 

dd offset var3 

;; ..... and so on .....  

dd 0 


var1 db "SERVER_SOFTWARE=theservername", 0 

var2 db "REMOTE_ADDR=127.0.0.1", 0 

var3 db "SOME_OTHER_VAR=someothervalue", 0 

;; ..... and so on .....  





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users