BUG: getenv() Fails when Called in Ctor for QuickWin App

Last reviewed: July 22, 1997
Article ID: Q116213
1.00 1.50 MS-DOS kbprg kbbuglist

The information in this article applies to:

  • The C Run-time (CRT), included with: Microsoft Visual C++ for Windows, versions 1.0 and 1.5

SYMPTOMS

Calling the getenv() function inside a constructor for a global object may not work for a QuickWin Application. Using the same code for an MS-DOS application works properly.

RESOLUTION

A possible workaround is to move the call to getenv() out of the constructor and into a member function. Call this member after stepping inside main(). (See comments in the sample code below.)

STATUS

Microsoft has confirmed this to be a bug in the products listed at the beginning of this article. We are researching this bug and will post new information here in the Microsoft Knowledge Base as it becomes available.

MORE INFORMATION

The sample code below demonstrates how the same code works for an MS-DOS application but fails in a QuickWin Application.

Sample Code

/* Compile options needed for MS-DOS: cl /AM /D_DOS
   Compile options needed for QuickWin: standard
*/

   #include <iostream.h>
   #include <stdlib.h>

   class Enviro
   {
   public:
     Enviro();
     void PrintEnviro()
     { cout << s_ << endl; }

     // void MyGetEnv()          // uncomment as workaround
     // { s_ = getenv("PATH"); }

     static int RefCount;
   private:
     char *s_;
   };

   Enviro::Enviro()
   {
     RefCount++;
     s_ = getenv("PATH");       // enviro1 fails here
     if (!s_)                    // enviro2 works here
       s_ = "Not Found";
   }

   int Enviro::RefCount=0;

   static Enviro enviro1;       // enviro1 ctor is called before main()

   void main(int argc, char *argv[], char *envp[])
   {
      //enviro1.MyGetEnv();  // uncomment as workaround

     Enviro enviro2;          // enviro2 ctor is called after main()

     cout << "print: enviro2" << endl;
     enviro2.PrintEnviro();

     cout << "print: enviro1" << endl;
     enviro1.PrintEnviro();
   }


Additional reference words: 1.00 1.50 runtime
KBCategory: kbprg kbbuglist
KBSubcategory: CRTIss
Keywords : kb16bitonly


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.