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

Kaveh Goudarzi kaveh@arkasoft.com
Tue May 2 14:18:00 GMT 2006


Hi Brian,

	Thanks for the code to figure out cygwin vs non
cygwin app.  I tried calling cygwin_internal(CW_SYNC_WINENV)
to sync the envs with windows but it doesn't seem to work
for me.  I still get the truncated version.

	I downloaded the src for cygwin and compiled my code
as below (cygwin is where I did the cvs pull).

gcc -o envs-test.exe env-test.c -I  cygwin/src/winsup/cygwin/include/sys

	I call cygwin_internal ( CW_SYNC_WINENV )  prior to
the call to GetEnvironmentStrings ... the strange thing is the
value that comes back ... looking at the code (cygwin/src/winsup/cygwin/external.cc)
I expected zero but I get another value (4294967295 ... uninitialized return?)

	Any ideas? (could the enum values have changed between the
version I pulled from cvs and the one I'm running (cygwin.h rev 1.63)?
(I attached cygcheck output this time).

	Also I noticed that the address of environ seems always to be
at 0x460090 ... is it safe to assume this to always be the case? This
must be at the bottom of the stack (top being the bit that grows + shrinks).
Can I assume this address to remain the same?  Because then I could pull
the values myself by just going to that address without having to rely on
cygwin to sync with windows or call any additional functions in my injected
code.

thanks again for all your help.

kind regards,

Kaveh.

Brian Dessent wrote:
> Kaveh Goudarzi wrote:
> 
>> 1. is it possible to somehow force cygwin to always sync
>> the env with windows prior to spawning a child process (any
>> child process)?
> 
> Well, you could always patch the DLL to not perform this optimization. 
> I'm not sure if it's possible to do this in any other way.
> 
>> 2. How does cygwin work out if it's spawning a cygwin or
>> non-cygwin binary?  (is it based on refs to cygwin1.dll?)
>> (is it the loader that does this?)
> 
> I think it looks at the PE headers of the file for a record referring to
> cygwin1.dll in the import section.  Or, if the path is mounted cygexec
> this check is skipped and all binaries are assumed to be Cygwin
> binaries, so you might be able to invert the sense of this somehow.
> 
> Also, there was some recent discussion about whether this optimization
> was worth the headaches (in the context of programs that included
> WinMain() failing when calling GetEnvironmentString()) and I think the
> result was that the optimization was dropped... so I don't know what the
> current state of this in CVS is.  I think cgf or Corinna (if she wasn't
> away on vacation right now) would have to clarify here.
> 
>> 3. Brian mentioned a call to cygwin_internal(CW_SYNC_WINENV)
>> In my scenario (see below) I'd have to work out if the target
>> process was a cygwin proc or not before I tried to invoke this
>> method ... is there a painless way of working out if a process
>> is cygwin or not e.g. one that would give me a response whether
>> the process I'm querying is a cygwin or a native win app?
> 
> #include <stdio.h>
> #include <windows.h>
> 
> int main()
> {
>   HMODULE hm = GetModuleHandle ("cygwin1.dll");
>   
>   if (hm && GetProcAddress (hm, "cygwin_internal"))
>     puts ("I appear to be a cygwin binary.");
>   else
>     puts ("Nope, no Cygwin here.");
> }
> 
> 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/
> 
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: env-test.c
URL: <http://cygwin.com/pipermail/cygwin/attachments/20060502/473e8ba7/attachment.c>
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: cygcheck.out
URL: <http://cygwin.com/pipermail/cygwin/attachments/20060502/473e8ba7/attachment.ksh>
-------------- next part --------------
--
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/


More information about the Cygwin mailing list