This is the mail archive of the cygwin mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Truncated Environment Variables? - using Cygwin + GetEnvironmentStrings() WIN32 API


Kaveh Goudarzi wrote:

>         I'm working on an app which attempts to get
> the environment of running cygwin apps.  It does this
> by running the GetEnvironmentStrings() win32 api call
> in the context of the running cygwin app (code injection).
> However the result that comes back as a truncated version of
> the environment - only 4 values PATH, SYSTEMDRIVE, SYSTEMROOT,
> WINDIR.

This is the expected behavior.  Cygwin maintains its own environment
separate from the Windows environment.  This is (presumably) to reduce
the overhead of having to convert paths back and forth every time a
value is accessed.

The only variables kept in the Windows environment are those that are
essential to various win32 API calls, which as you have seen are things
like SYSTEMROOT.  Also, when Cygwin knows that it will be spawning a
non-Cygwin binary, it will sync the Windows environment so that it is
filled out.

This is not a problem for the vast majority of Cygwin apps because the
way to access the environment in a POSIX-like way is with getenv() and
putenv(), and these work as expected on the Cygwin environment.  Calling
the win32 API directly to manipulate the environment breaks the
encapsulation layer in that the proper way for a POSIX program to do
this is with getenv/putenv, and Cygwin targets the POSIX API.

So, the ideal way to handle this is to use getenv() instead.  As an
alternative you could call cygwin_internal (CW_SYNC_WINENV) before
calling any win32 APIs, which should fill out the Windows environment.

Brian

--
Unsubscribe info:      http://cygwin.com/ml/#unsubscribe-simple
Problem reports:       http://cygwin.com/problems.html
Documentation:         http://cygwin.com/docs.html
FAQ:                   http://cygwin.com/faq/


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]