delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2006/04/30/00:11:28

X-Spam-Check-By: sourceware.org
Message-ID: <445438DF.7DAC5BBA@dessent.net>
Date: Sat, 29 Apr 2006 21:11:11 -0700
From: Brian Dessent <brian AT dessent DOT net>
X-Mailer: Mozilla 4.79 [en] (Windows NT 5.0; U)
MIME-Version: 1.0
To: cygwin AT cygwin DOT com
Subject: Re: Truncated Environment Variables? - using Cygwin + GetEnvironmentStrings() WIN32 API
References: <44542B34 DOT 9000907 AT arkasoft DOT com>
X-IsSubscribed: yes
Reply-To: cygwin AT cygwin DOT com
Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm
List-Subscribe: <mailto:cygwin-subscribe AT cygwin DOT com>
List-Archive: <http://sourceware.org/ml/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-help AT cygwin DOT com>, <http://sourceware.org/ml/#faqs>
Sender: cygwin-owner AT cygwin DOT com
Mail-Followup-To: cygwin AT cygwin DOT com
Delivered-To: mailing list cygwin AT cygwin DOT com

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/

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019