Re: GenEnvironmentVariable and CreateProcess failures under XP

From: Randy Birch (rgb_removethis_at_mvps.org)
Date: 04/23/04


Date: Fri, 23 Apr 2004 01:56:08 -0400

I'm not sure where you got your info on the meaning of the return values for
GetEnvironmentVariable ...
-----------

If the function succeeds, the return value is the number of TCHARs stored
into the buffer pointed to by lpBuffer, not including the terminating null
character.

If lpBuffer is not large enough to hold the data, the return value is the
buffer size, in TCHARs, required to hold the value string and its
terminating null character.

If the function fails, the return value is zero. If the specified
environment variable was not found in the environment block.

--------------

If you first call GetEnvironmentVariable with a 0-length string, the return
value will indicate the size you need to pad the string to retrieve the
data. A return value of '1' does not necessarily therefore = success.

My first suggestion to approach the debugging of these calls on NT-based
systems is to ensure that all strings used in the APIs are null-terminated.
NT is very picky about the formatting of strings, something 9x machines
allow a lot of grace on.

My second suggestion - since it's not clear in your message - would be to
ensure SetEnvironmentVariable is not being called prior to your executing
the external apps, especially if it is adding paths to the PATH environment
setting. There is a limit to the size this string can be, and repeated
calling may be adding your custom path multiple times, thereby exceeding
this limit.

Finally, it is important to realize that environment changes made by the
call(s) to SetEnvironmentVariable only affect the environment for your
application. It does not affect the "system environment" that other
applications see. As the MSDN remarks state ... "This function has no effect
on the system environment variables or the environment variables of other
processes." Perhaps you are relying on those applications to use your
newly-amended environment?

-- 
Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.
"LarryH" <BigLar@nospam.nospam> wrote in message 
news:B1533CE0-AC69-4AC8-A3DF-398662B8A9E0@microsoft.com...
: I am having truble figuring out how to debug a problem with VB6 program 
that shells other Win32 executables.  The program has been running for years 
and was just recompiled under Windows XP Pro SP1.  The problem is with calls 
to the Win32 API not returning results even though the exit code indicates 
success.  The program calls GenEnvironmentVariable to get the sytem PATH, 
SetEnvironmentVariable to prepend a value to the system PATH, then calls 
CreateProcess to launch Notepad or other Win32 executables.  I can launch 
Notepad 6 or 7 times in a row with no problem, then I start getting one of 
three unexpected results. The unexpected results are:
:
: (1) GetEnvironmentVariable fails to return anything, but still returns "1" 
to indicate success,
: (2) CreateProcess fails to launch Notepad (or whataver is called), but 
still returns "1" to indicate success,
: (3) Createprocess fails to launch Notepad (or whataver is called) and 
returns "0" to indicate failure.
:
: Once an unexpected result happens, it will happen on all subsequent 
attempts to launch that program until I shut the program down (when 
debugging, I have to shut VB 6 down).  Only the third result makes sense. 
The Event log is not updated by any of these errors.
:
: I cannot find any information on this problem in the MSDN library or 
through Internet searches, including some of the more popular VB sites.
:
: I have also been told that this problem ocurrs infrequently on Windows NT, 
but only happens once, not continuously.  On XP, of the two PCs we are 
testing on, one computer produces the unexpected results a lot more 
frequently that the other.  All of our PCs are locked down and all software 
is remotely installed.  Our workstation people looked at the computers and 
say the same patches have been applied to each.  I can replicate this 
problem at will.
:
: Is this a known problem?  What can I do about it?  If it is unfamiliar, 
what tools do I need to debug it? 


Relevant Pages

  • Re: A more structured approach
    ... For the purpose of a "string compare" routine that'll be ... you might care. ... some assembly language, you don't. ... But I would then pass that as a parameter to my "find environment ...
    (alt.lang.asm)
  • Complex XML Serialization
    ... I've been reading Keith Pijanowski's article - Enrich Your XML Serialization With Schema Providers In The .NET Framework - on how to do custom serialization of objects. ... private string _administratorPassword; ... //foreach (Environment environment in this.Environments) ...
    (microsoft.public.dotnet.xml)
  • Re: A more structured approach
    ... For the purpose of a "string compare" routine that'll be ... you might care. ... But I would then pass that as a parameter to my "find environment ...
    (alt.lang.asm)
  • Re: 15 Answers to Creationists Nonsense Claims
    ... if evolution were a computer program than the ... desired string would be passed to the program as an argument, ... The environment does not interact with the DNA directly, ... with the phenotype that the DNA constructs. ...
    (talk.origins)
  • Re: odd behavior on client installation
    ... And being an environment variable, ... keys, *AND* clean up the system environment ... expecting, leading to my purge of Oracle from the system being ... presence of environment settings being set from places I had never ...
    (comp.databases.oracle.server)