// GetEnvirBlock.java // /** * Displays entire environment variable block contents using J/Direct * Uses custom DLL migutilslib.lib function: * DWORD GetAllEnvars(LPTSTR varbuff, DWORD nSize) * * @author Michel Gallant 01/11/2002 */ public class GetEnvirBlock { /** * Main application entrypoint. * @param arg Command line arguments */ static public void main(String[] arg) { System.out.println("--------- Environment Variables -------------\n" + getEnvBlock()); } /** * Gets the entire environment variable block * @return The value of the named environment variable, or * null if no such variable exists */ static String getEnvBlock() { // Allocate string buffer for return value StringBuffer value = new StringBuffer(2000); // Try to retrieve environment variable block via J/Direct int chars = GetAllEnvars(value, value.capacity()+1); // terminating null character added to buff capacity by J/Direct // If it was not large enough, reallocate buffer and retry if (chars > (value.capacity()+1)) //if buffer too small, GetAllEnvars returns required buffer size including C null. { System.out.println("\n" + value.capacity() + " character StringBuffer not large enough! expanding to " + (chars-1) + " characters\n\n") ; value.ensureCapacity(chars-1); chars = GetAllEnvars(value, chars); } // System.out.println("Characters read (including added after lines) " + chars) ; return value.toString(); } /** @dll.import("MIGUTILSLIB", ansi) */ static native int GetAllEnvars(StringBuffer buf, int size); /** @dll.import("KERNEL32") */ static native int GetEnvironmentVariable(String envirname, StringBuffer buf, int size); }