Retrieving MS-DOS Environment Vars from a Windows DLLLast reviewed: July 23, 1997Article ID: Q78542 |
3.00 3.10
WINDOWS
kbprg
The information in this article applies to:
SUMMARYThe C run-time library function getenv() does not work when called from a Windows dynamic-link library (DLL). MS-DOS environment variables can be found by searching the environment block. This article explains a method to retrieve information from environment variables. NOTE: In Visual C++ version 1.0, getenv() does work correctly as long as you do not link in LIBENTRY.OBJ (you need the default library version that initializes the run time) and mark your DLL function as _export to correctly load DS.
MORE INFORMATIONIn a Windows system, an application's environment block is not available to a DLL. For this reason, the getenv() C run-time library function does not work when called from a DLL. To gain access to environment variables, an application must use the Windows GetDOSEnvironment() function, which returns a far pointer to the environment block of the currently running task. Using this pointer, the application can search the environment block for the desired variable. The format of the environment block is as follows:
set envvar=c:\this\is\a\testis stored in memory as follows:
ENVVAR=c:\this\is\a\test\0MS-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 a substitute 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. */ } |
Additional reference words: 3.00 3.10
© 1998 Microsoft Corporation. All rights reserved. Terms of Use. |