View mode: basic / threaded / horizontal-split · Log in · Help
April 22, 2005
How to get whole env.
Simple:

extern(C){
   char **environ;
}

does not work;
-- 
Dawid Ciężarkiewicz | arael
jid: arael@fov.pl
April 23, 2005
Re: How to get whole env.
Dawid Ciê¿arkiewicz <arael@fov.pl> wrote:

[...]
> does not work;

Try along the lines of
http://www.digitalmars.com/drn-bin/wwwnews?D/26842

-manfred
April 23, 2005
Re: How to get whole env.
In article <d4btvq$ac5$1@digitaldaemon.com>, Dawid
=?ISO-8859-2?Q?Ci=EA=BFarkiewicz?= says...
>
>Simple:
>
>extern(C){
>    char **environ;
>}
>
>does not work;
>-- 
>Dawid Ciê¿arkiewicz | arael
>jid: arael@fov.pl

Dawid, I'm not sure which OS you're on from looking at your message, but in
WinXP it can be done with Win32 API calls. Below is an example I found on MSDN
Online and modified it to work in D.

# // getenv.d
# // Example from MSDN 
# // http://msdn.microsoft.com/library/default.asp?url=/library/
#           en-us/dllproc/base/getenvironmentstrings.asp
#
# private import std.stdio;
# private import std.c.windows.windows;
# 
# // function retrieves the environment variables for the current process.
# extern( Windows ) LPVOID GetEnvironmentStringsA();
#  
# // function frees a block of environment strings.
# extern( Windows ) 
#    BOOL FreeEnvironmentStringsA( /* IN */ LPTSTR lpszEnvironmentBlock);
# 
# // Function retrieves the calling thread's last-error code value
# extern( Windows ) uint GetLastError();
# 
# int main()
# {
#     LPSTR  lpszVariable; 
#     LPVOID lpvEnv; 
#  
#     // Get a pointer to the environment block. 
#     lpvEnv = GetEnvironmentStringsA();
# 
#     // If the returned pointer is NULL, exit.
#     if (lpvEnv == null)
#     {
#         writefln("GetEnvironmentStrings failed (%d)", GetLastError()); 
#         return 0;
#     }
#  
#     // Variable strings are separated by NULL byte, and the block is 
#     // terminated by a NULL byte. 
#     for (lpszVariable = cast(LPTSTR)lpvEnv; *lpszVariable; lpszVariable++) 
#     { 
#        while (*lpszVariable) 
#           putchar(*lpszVariable++);
#  
#        putchar('\n'); 
#     }
# 
#     FreeEnvironmentStringsA(cast(LPTSTR)lpvEnv);
# 
#     return 0;
# }

Output:
------------------
C:\dmd>dmd getenv.d
C:\dmd\bin\..\..\dm\bin\link.exe getenv,,,user32+kernel32/noi;

C:\dmd>getenv
=::=::\
=C:=C:\dmd
=ExitCode=00000000
ALLUSERSPROFILE=C:\Documents and Settings\All Users
APPDATA=C:\Documents and Settings\SPOTTE~1\Application Data
CommonProgramFiles=C:\Program Files\Common Files
COMPUTERNAME=HeavyMetal
ComSpec=C:\WINDOWS\system32\cmd.exe
EDPATH=C:\watcom\EDDAT
FINCLUDE=C:\watcom\SRC\FORTRAN
GMAXLOC=C:\gmax\
HOMEDRIVE=C:
HOMEPATH=\Documents and Settings\SpottedTiger
INCLUDE=C:\watcom\H;C:\watcom\H\NT
LIB=C:\Program Files\Microsoft Visual Studio\VC98\mfc\lib;
C:\Program Files\Microsoft Visual Studio\VC98\lib;
C:\Program Files\Microsoft Visual Studio .NET\FrameworkSDK\Lib\
MSDevDir=C:\Program Files\Microsoft Visual Studio\Common\MSDev98
NUMBER_OF_PROCESSORS=1
OS=Windows_NT
Path=C:\DM;C:\DMD;C:\UT2004\system;C:\watcom\BINNT;C:\watcom\BINW;
C:\Program Files\Borland\BDS\1.0\Bin;
PATHEXT=.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
PROCESSOR_ARCHITECTURE=x86
PROCESSOR_IDENTIFIER=x86 Family 15 Model 1 Stepping 2, GenuineIntel
PROCESSOR_LEVEL=15
PROCESSOR_REVISION=0102
ProgramFiles=C:\Program Files
PROMPT=$P$G
SESSIONNAME=Console
SystemDrive=C:
SystemRoot=C:\WINDOWS
TEMP=C:\DOCUME~1\SPOTTE~1\LOCALS~1\Temp
TMP=C:\DOCUME~1\SPOTTE~1\LOCALS~1\Temp
tvdumpflags=10
USERPROFILE=C:\Documents and Settings\SpottedTiger
VJSHARPTOOLS=C:\Program Files\Microsoft Visual J# .NET\Framework\Bin;
C:\WINDOWS\Microsoft Visual JSharp .NET\Framework\v1.0.4205;
VSCOMNTOOLS="C:\Program Files\Microsoft Visual Studio .NET\Common7\Tools\"
WATCOM=C:\watcom
windir=C:\WINDOWS

C:\dmd>

David L.

-------------------------------------------------------------------
"Dare to reach for the Stars...Dare to Dream, Build, and Achieve!"
-------------------------------------------------------------------

MKoD: http://spottedtiger.tripod.com/D_Language/D_Main_XP.html
April 27, 2005
Re: How to get whole env.
Manfred Nowak wrote:

> Dawid Ciężarkiewicz <arael@fov.pl> wrote:
> 
> [...]
>> does not work;
> 
> Try along the lines of
> http://www.digitalmars.com/drn-bin/wwwnews?D/26842

Isn't this a bug/lack of functionality that I have to manually change Phobos
source to do this?
-- 
Dawid Ciężarkiewicz | arael
jid: arael@fov.pl
April 27, 2005
Re: How to get whole env.
David L. Davis wrote:
> Dawid, I'm not sure which OS you're on from looking at your message, but
> in WinXP it can be done with Win32 API calls. Below is an example I found
> on MSDN Online and modified it to work in D.

I'm using linux. Thanks you anyway.
-- 
Dawid Ciężarkiewicz | arael
jid: arael@fov.pl
Top | Discussion index | About this forum | D home