home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / strings / Strings.java < prev   
Encoding:
Java Source  |  2000-05-04  |  4.9 KB  |  143 lines

  1. import com.ms.dll.DllLib;
  2.  
  3.  
  4. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  5.  *
  6.  * This example shows how to pass strings using the String class and
  7.  * receive strings using the StringBuffer class. It also demonstrates
  8.  * the use of the DllLib.ptrToStringAnsi method. This method is used
  9.  * to extract strings out of a series of catenated strings.
  10.  */
  11. public class Strings
  12. {
  13.  
  14.     public static void main(String args[])
  15.     {
  16.         // Print out some common environment variables.
  17.         System.out.println("PATH = " + getEnvironmentVariable("PATH"));
  18.         System.out.println("TMP  = " + getEnvironmentVariable("TMP"));
  19.         System.out.println("TEMP = " + getEnvironmentVariable("TEMP"));
  20.  
  21.  
  22.         // Now, list all defined environment variables.
  23.         System.out.println();
  24.         System.out.println("Printing out all environment strings:");
  25.         System.out.println("-------------------------------------\n");
  26.         String env[] = getEnvironmentStrings();
  27.         for (int i = 0; i < env.length; i++) {
  28.             System.out.println(env[i]);
  29.         }
  30.     }
  31.  
  32.  
  33.  
  34.  
  35.  
  36.     //-------------------------------------------------------------
  37.     // This method returns the value of the named environment variable,
  38.     // or null if the environment variable is not defined. Note how
  39.     // this method always sets the StringBuffer "value"'s capacity
  40.     // prior to passing it to GetEnvironmentVariable. This step is crucial
  41.     // because it ensures that enough buffer space is allocated to
  42.     // accommodate the largest possible output.
  43.     //-------------------------------------------------------------
  44.     static String getEnvironmentVariable(String name)
  45.     {
  46.         final int MAXLENGTH=80;
  47.  
  48.         StringBuffer value;    
  49.         int          numchars;
  50.  
  51.         value    = new StringBuffer(MAXLENGTH);
  52.         numchars = GetEnvironmentVariable(name, value, MAXLENGTH+1);
  53.         if (numchars == 0) {
  54.             return null;   // Environment variable not defined.
  55.         }
  56.         if (numchars > MAXLENGTH+1) {
  57.             // If we got here, our initial buffer was too small. Expand it
  58.             // and try again.
  59.             value.ensureCapacity(numchars-1);
  60.             GetEnvironmentVariable(name, value, numchars);
  61.         }
  62.         return value.toString();
  63.     }
  64.  
  65.  
  66.     //-------------------------------------------------------------
  67.     // This declaration gives us direct access to the GetEnvironmentVariable
  68.     // api. Note that the "name" parameter is declared as a String, but
  69.     // that "value" is declared as a StringBuffer. This is because "name"
  70.     // is used for input while "value" is used for output.
  71.     // 
  72.     //-------------------------------------------------------------
  73.     /** @dll.import("KERNEL32") */
  74.     static native int GetEnvironmentVariable(String       name,
  75.                                              StringBuffer value,
  76.                                              int          valueSize);
  77.  
  78.  
  79.  
  80.  
  81.  
  82.     //-------------------------------------------------------------
  83.     // This method returns the entire environment, as an array of
  84.     // strings where each string has the form "name=value".
  85.     //
  86.     //
  87.     //-------------------------------------------------------------
  88.     static String[] getEnvironmentStrings()
  89.     {
  90.         int env = GetEnvironmentStrings();
  91.         try {
  92.  
  93.             int walk  = env;
  94.             int count = 0;
  95.             int len;
  96.             String s;
  97.  
  98.             // GetEnvironmentStrings returns a single memory block which
  99.             // holds all the environment strings concatenated together: i.e.
  100.             //
  101.             //  A=1\0B=2\0C=3\0\0
  102.             //
  103.             // The block is terminated by an empty string.
  104.             //
  105.             // To turn this into a String array, we walk through
  106.             // the memory block and extract one string at a time.
  107.  
  108.             // First, make one pass to count the number of environment
  109.             // strings.
  110.             do {
  111.                 s = DllLib.ptrToStringAnsi(walk);
  112.                 walk += s.length() + 1;
  113.                 count++;
  114.             } while(s.length() != 0);
  115.             count--;
  116.  
  117.             // Now that we know the count, allocate a String array of
  118.             // that size.
  119.             String envStrings[] = new String[count];
  120.  
  121.             // Now, do a second pass to extract each string.
  122.             walk = env;
  123.             for (int i = 0; i < count; i++) {
  124.                 envStrings[i] = DllLib.ptrToStringAnsi(walk);
  125.                 walk += envStrings[i].length() + 1;
  126.             }
  127.             return envStrings;
  128.  
  129.         } finally {
  130.             FreeEnvironmentStrings(env);
  131.         }
  132.     }
  133.  
  134.     /** @dll.import("KERNEL32") */
  135.     static native int     GetEnvironmentStrings();
  136.     /** @dll.import("KERNEL32") */
  137.     static native boolean FreeEnvironmentStrings(int env);
  138. }
  139.  
  140.  
  141.  
  142.  
  143.