Retrieving MS-DOS Environment Vars from a Windows DLL

This article was previously published under Q78542
This article has been archived. It is offered "as is" and will no longer be updated.
The C run-time library function getenv() does not work when calledfrom a Windows dynamic-link library (DLL). MS-DOS environment variablescan be found by searching the environment block. This article explainsa method to retrieve information from environment variables.

NOTE: In Visual C++ version 1.0, getenv() does work correctly as longas you do not link in LIBENTRY.OBJ (you need the default libraryversion that initializes the run time) and mark your DLL function as_export to correctly load DS.
In a Windows system, an application's environment block is notavailable to a DLL. For this reason, the getenv() C run-time libraryfunction does not work when called from a DLL.

To gain access to environment variables, an application must use theWindows GetDOSEnvironment() function, which returns a far pointer tothe environment block of the currently running task. Using thispointer, the application can search the environment block for thedesired variable. The format of the environment block is as follows:

  1. Each environment variable is followed by an equal sign (=), the contents of the variable, and a NULL terminator (\0).
  2. The last environment variable in the block is terminated with two NULL characters.
An environment variable set with the following MS-DOS command
   set envvar=c:\this\is\a\test				
is stored in memory as follows:
MS-DOS converts environment variables to uppercase letters; therefore,all searches for an environment variable must take this into account.

Below is source code to a function called DLLGetEnv() that acts as asubstitute for the getenv() function in a DLL:
/* forward declaration */ LPSTR FAR PASCAL DLLGetEnv ( LPSTR );/**********************************************************************  DLLGetEnv ( lpszVariableName )                                    **                                                                    **  Takes a LPSTR to the name of an environment variable and returns  **  the contents of that variable. Returns NULL if the environment    **  variable does not exist. The search for the environment variable  **  is case sensitive.                                                **                                                                    **********************************************************************/ LPSTR FAR PASCAL DLLGetEnv ( LPSTR lpszVariableName )  {  LPSTR lpEnvSearch;  LPSTR lpszVarSearch;  if ( !*lpszVariableName )  //  Check for a NULL pointer    return NULL;                        //  Get a pointer to the MS-DOS environment block  lpEnvSearch = GetDOSEnvironment ();  while ( *lpEnvSearch )  //  While there are strings to parse    {     /*           *  Make a copy of the pointer to the name of the           *  environment variable to search for.           */     lpszVarSearch = lpszVariableName;              //  Check to see if the variable names match    while ( *lpEnvSearch && * lpszVarSearch &&            *lpEnvSearch == *lpszVarSearch )      {      lpEnvSearch++;      lpszVarSearch++;      }        /*         *  If the names match, the lpEnvSearch pointer is on the "="         *  character and lpszVarSearch is on a null terminator.         *  Increment and return lpszEnvSearch, which will point to the         *  environment variable's contents.         *         *  If the names do not match, increment lpEnvSearch until it         *  reaches the end of the current variable string.         */     if ( *lpEnvSearch == '=' && *lpszVarSearch == '\0' )      return ( lpEnvSearch + 1 );    else      while ( *lpEnvSearch )        lpEnvSearch++;        /*         *  At this point the end of the environment variable's string         *  has been reached. Increment lpEnvSearch to move to the         *  next variable in the environment block. If it is NULL,         *  the end of the environment block has been reached.         */     lpEnvSearch++;    }  return NULL;  /*                 *  If this section of code is reached, the variable                 *  was not found.                 */   }				
3.00 3.10

Article ID: 78542 - Last Review: 10/07/2013 00:59:19 - Revision: 3.0

  • Microsoft Windows Software Development Kit 3.0
  • Microsoft Windows Software Development Kit 3.1
  • kbnosurvey kbarchive kb16bitonly KB78542