Microsoft KB Archive/69725

{|
 * width="100%"|

XL: Reading the MS-DOS Environment String

 * }

-

The information in this article applies to:


 * Microsoft Excel for Windows, version 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(&quot;KERNEL&quot;,&quot;GetMS-DOSEnvironment&quot;,&quot;D&quot;) 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 &quot;D&quot; 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 &quot;~USERNAME=BOB&quot;, 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 &quot;~&quot; has a decimal value of 126. You can then search this string for particular environment variables.