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

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This example shows several different ways of dealing with pointers
  4.  * in Java. This sample simply prints out the version string of
  5.  * KERNEL32.DLL using the Win32 version apis. This seemingly straightforward
  6.  * task requires parsing data through a double-indirected pointer.
  7.  *
  8.  * A one-element int[] array is used to simulate a pointer to a return buffer,
  9.  * in the "pValBuffer" argument to VerQueryValue.
  10.  *
  11.  * A byte[] array is used to simulate a generic buffer in the "pVerBuffer"
  12.  * arguments to getFileVersionInfo and VerQueryValue.
  13.  *
  14.  * An "int" is used to simulate the LPVOID pointer received from
  15.  * VerQueryValue.
  16.  *
  17.  * DllLib.ptrToStruct() is used to parse the data addressed by
  18.  * an LPVOID pointer.
  19.  *
  20.  * DllLib.ptrToString() is used to extract a string addressed by
  21.  * an LPVOID pointer.
  22.  *
  23.  * (Note: This is intended to be an example of how to deal with particularly
  24.  * difficult DLL functions. The specific task of getting version information
  25.  * for a file can be done much more easily using the FileVersionInformation
  26.  * class in com.ms.util.)
  27.  */
  28.  
  29.  
  30. import com.ms.dll.*;
  31.  
  32. /** @dll.import(auto) */
  33. public class Pointers
  34. {
  35.   
  36.   public static void main(String args[])
  37.   {
  38.     String sVersion;
  39.     
  40.     sVersion = FileInfo.getVersion(getSystemDirectory() + "\\KERNEL32.DLL");
  41.     
  42.     if(sVersion.length() > 0)
  43.     { System.out.println("KERNEL32.DLL version is " + sVersion);
  44.     }
  45.     else
  46.     { System.out.println("Couldn't get version information for KERNEL32.DLL");
  47.     }
  48.   }
  49.  
  50.  
  51.  
  52.   /** @dll.import("KERNEL32") */
  53.   private static native int GetSystemDirectory(StringBuffer sysPath, int cch);
  54.  
  55.   private static String getSystemDirectory()
  56.   {
  57.       int MAXPATH=260;
  58.       StringBuffer sysPath = new StringBuffer(MAXPATH-1);
  59.       int cb = GetSystemDirectory(sysPath, MAXPATH);
  60.       if (cb == 0 || cb > MAXPATH) {
  61.           throw new RuntimeException("GetSystemDirectory() failed.");
  62.       }
  63.       return sysPath.toString();
  64.   }
  65.  
  66. }
  67.  
  68.  
  69.  
  70. //==========================================================================//
  71. // This structure allows us to extract a value from a pointer returned by
  72. // the Win32 VerQueryValue routine.
  73.  
  74. /** @dll.struct() */
  75. class PP_INTEGER 
  76. { public int iVal;
  77. }
  78.  
  79.  
  80. //==========================================================================//
  81.  
  82. /** @dll.import(auto) */
  83. class FileInfo
  84. {
  85.  
  86.   /** @dll.import("VERSION") */
  87.   private static native int GetFileVersionInfoSize(String sFileName,
  88.                                                    int    pHandle[]);
  89.  
  90.   /** @dll.import("VERSION") */
  91.   private static native boolean GetFileVersionInfo(String sFileName,
  92.                                                    int    iHandle,
  93.                                                    int    iSize,
  94.                                                    byte   pVerBuffer[]);
  95.  
  96.   /** @dll.import("VERSION") */
  97.   private static native boolean VerQueryValue(byte   pVerBuffer[],
  98.                                               String sSubBlock,
  99.                                               int    pValBuffer[],
  100.                                               int    piSize[]);
  101.  
  102.  
  103.  
  104. //--------------------------------------------------------------------------//
  105.  
  106.   public static String getVersion(String sFileName)
  107.   {    
  108.     PP_INTEGER ppInt;
  109.     boolean bStat;
  110.     int pValBuffer[] = {0};
  111.     int pHandle[] = {0};
  112.     int piSize[] = {0};
  113.     int iSize;
  114.  
  115.     // See if the module has version info.  If not, return an empty string.
  116.       iSize = GetFileVersionInfoSize(sFileName, pHandle);
  117.       if(iSize < 1) return "";
  118.  
  119.     // Create enough space to hold the version information data block.
  120.     byte[] pVerBuffer = new byte[iSize + 32];
  121.  
  122.     // Try to get the version info.  If we can't, return an empty string.
  123.       bStat = GetFileVersionInfo(sFileName, pHandle[0], iSize, pVerBuffer);
  124.     if(bStat == false) return "";
  125.  
  126.     // Get the language entry array data for the target file.
  127.     // If we can't get it, return an empty string.
  128.     bStat = VerQueryValue(pVerBuffer,
  129.                           "\\VarFileInfo\\Translation",
  130.                           pValBuffer,
  131.                           piSize);
  132.     if(bStat == false || piSize[0] < 1) return "";
  133.  
  134.     // Use the default (first) language entry in the array to 
  135.     // generate the query string for the file version. 
  136.     ppInt = (PP_INTEGER)DllLib.ptrToStruct(PP_INTEGER.class, pValBuffer[0]);
  137.     String sLCID = intToHexString(ppInt.iVal);
  138.     String sFileVersion = "\\StringFileInfo\\" +
  139.                           sLCID.substring(4) +
  140.                           sLCID.substring(0, 4) +
  141.                           "\\FileVersion";
  142.     
  143.     // Query for version string. Return an empty string if we don't get it.
  144.     bStat = VerQueryValue(pVerBuffer, sFileVersion, pValBuffer, piSize);
  145.     if(bStat == false || piSize[0] < 1) return "";
  146.     
  147.     // Return the version string.
  148.     return DllLib.ptrToString(pValBuffer[0]);
  149.   }
  150.  
  151. //-----------------------------------------------------------------------//
  152. // This routine generates an eight-character hexadecimal string that
  153. // represents the integer value passed in.  The string is padded with
  154. // leading zeros as needed to make it eight characters long.
  155.  
  156.   static String intToHexString(int iVal)
  157.   {
  158.     String sTemp = new String("00000000" + Integer.toHexString(iVal));
  159.     sTemp = sTemp.toUpperCase();
  160.     return sTemp.substring(sTemp.length() - 8);    
  161.   }
  162.  
  163. }  // End of public class FileInfo.
  164.  
  165. //==========================================================================//
  166. //                               - EOF -                                    // 
  167. //==========================================================================//
  168.  
  169.  
  170.  
  171.  
  172.  
  173.