home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / JDirect / lasterr / LastErr.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  3.0 KB  |  104 lines

  1. /* (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  2.  *
  3.  * This example shows how to obtain the SetLastError() error code from a
  4.  * Win32 api. It is not safe to call GetLastError() yourself because the
  5.  * virtual machine can make api calls itself that overwrite the error value.
  6.  * Instead, you must use the "SetLastError" keyword (shown on the
  7.  * declaration for FindNextFile) which will cause the virtual machine
  8.  * to capture the error code and save it in a per-thread data block.
  9.  * This error code can then be retrieved using the getLastWin32Error()
  10.  * method.
  11.  */
  12.  
  13. import com.ms.dll.DllLib;
  14. import com.ms.dll.Win32Exception;
  15.  
  16.  
  17. /** @dll.import("KERNEL32",auto) */
  18. public class LastErr
  19. {
  20.     public static void main(String args[])
  21.     {
  22.         WIN32_FIND_DATA fd = new WIN32_FIND_DATA();
  23.         String searchPattern;
  24.  
  25.         if (args.length < 1) {
  26.             searchPattern = "*.*";
  27.         } else {
  28.             searchPattern = args[0];
  29.         }
  30.  
  31.         System.out.println("Finding all files that match the pattern \"" +
  32.                            searchPattern +
  33.                            "\"");
  34.         System.out.println("----------------------------------------------");
  35.  
  36.         int hFindFile = FindFirstFile(searchPattern, fd);
  37.         if (hFindFile == -1) {
  38.             System.err.println("FindFirstFile failed.");
  39.             return;
  40.         }
  41.         try {
  42.             do {
  43.                 System.out.println(fd.cFileName);
  44.             } while (FindNextFile(hFindFile, fd));
  45.  
  46.             int errcode = DllLib.getLastWin32Error();
  47.             if (errcode != ERROR_NO_MORE_FILES) {
  48.                 String errDescription;
  49.                 errDescription = Win32Exception.getErrorDescription(errcode);
  50.                 System.err.println("Unexpected FindNextFile() failure: " +
  51.                                    errDescription);
  52.             }
  53.  
  54.  
  55.         } finally {
  56.             FindClose(hFindFile);
  57.         }
  58.  
  59.     }
  60.  
  61.  
  62.     private static native int     FindFirstFile(String          searchPattern,
  63.                                                 WIN32_FIND_DATA pfd);
  64.     /** @dll.import(setLastError) */
  65.     private static native boolean FindNextFile(int             hFindFile,
  66.                                                WIN32_FIND_DATA pfd);
  67.     private static native boolean FindClose(int hFindFile);
  68.  
  69.     private static final int ERROR_NO_MORE_FILES = 18;
  70.  
  71. }
  72.  
  73. /** @dll.struct(auto) */
  74. class WIN32_FIND_DATA
  75. {
  76.  
  77.     int      dwFileAttributes;
  78.     FILETIME ftCreation;
  79.     FILETIME ftLastAccessTime;
  80.     FILETIME ftLastWriteTime;
  81.     int      nFileSizeHigh;
  82.     int      nFileSizeLow;
  83.     int      dwReserved0;
  84.     int      dwReserved1;
  85.  
  86.     /** @dll.structmap([type=TCHAR[260]]) */
  87.     String   cFileName;
  88.  
  89.     /** @dll.structmap([type=TCHAR[14]]) */
  90.     String   cAlternateFileName;
  91.  
  92.  
  93.  
  94. }
  95.  
  96. /** @dll.struct(auto) */
  97. class FILETIME
  98. {
  99.     int      dwLowDateTime;
  100.     int      dwHighDateTime;
  101.  
  102. }
  103.  
  104.