XL: Reading the MS-DOS Environment String

Last reviewed: February 21, 1996
Article ID: Q69725
The information in this article applies to:
  • Excel for Windows, versions 2.x, 3.0, 4.0, 5.0

IMPORTANT: The CALL() and REGISTER() functions are provided for advanced users only. If you use these functions incorrectly you could accidentally cause errors in your system's operation.

SUMMARY

It is possible to retrieve a set of characters from the MS-DOS environment string from Microsoft Excel if you are running under Microsoft Windows 3.0. This is done using the CALL and REGISTER functions.

MORE INFORMATION

The DOS environment is an area of memory that MS-DOS sets aside to store a series of ASCII strings. Each string in the environment is terminated by a byte of zero. The final string is terminated by two bytes of zero.

The following Microsoft Excel macro function will return the first N number of characters (starting with the second character) from the DOS environment string, including the zero bytes separating strings. This occurs as long as the first character in your environment string has the ASCII decimal value of N.

   A1      Get_Environment
   A2      =REGISTER("KERNEL","GetMS-DOSEnvironment","D")
   A3      =CALL(A2)
   A4      =RETURN()

Note: The CALL function can also be used on a standard worksheet in Microsoft Excel version 3.0.

Using a return parameter of "D" in the REGISTER function tells Microsoft Excel to interpret the value returned from the DLL function call as a string with the first byte containing the number of bytes to follow in the string. In this case, the first byte in the string returned will be the first ASCII character in your environment string. If this character has a decimal value of N, the following N characters will be returned.

Example

If the first string in your environment is "~USERNAME=BOB", cell A3 returns 126 characters of your environment string, starting with

   USERNAME=BOB{null}

where {null} indicates the zero byte terminating the string. This is because the ASCII tilde character "~" has a decimal value of 126. You can then search this string for particular environment variables.

REFERENCES

"Online Help," version 5.0 "Function Reference," version 4.0, pages 42-43, 350-352, 475-480 "Function Reference," version 3.0, pages 24, 193-194, 253-258 "Functions and Macros," version 2.1x, pages 255-256 and 337-340


KBCategory: kbusage
KBSubcategory:

Additional reference words: 2.1 2.10 3.0 3.00 4.0 4.00 5.0 5.00 environment
variable


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: February 21, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.