View Javadoc

1   // KernelInterface.java, created Aug 14, 2004 1:00:18 AM by joewhaley
2   // Copyright (C) 2004 John Whaley <jwhaley@alum.mit.edu>
3   // Licensed under the terms of the CPL; see LICENSE.txt for details.
4   package edu.stanford.suif.keepresident;
5   
6   import java.util.HashMap;
7   import java.util.Map;
8   
9   /***
10   * KernelInterface
11   * 
12   * @author John Whaley
13   * @version $Id: KernelInterface.java,v 1.3 2004/08/18 00:15:02 jwhaley Exp $
14   */
15  public class KernelInterface {
16      
17      static {
18          System.loadLibrary("kernelinterface");
19          registerNatives();
20      }
21      
22      private static native void registerNatives();
23      
24      /***
25       * 
26       */
27      public KernelInterface() {
28          super();
29          nameToAddress = new HashMap();
30          libraryHandle = LoadLibrary("kernel32");
31      }
32      
33      public void free() {
34          boolean b = FreeLibrary(libraryHandle);
35          if (b) {
36              throw handleError();
37          }
38          nameToAddress = null;
39          libraryHandle = 0;
40          lastError = 0;
41      }
42      
43      private KernelException handleError() {
44          String s;
45          if (lastError == 0) {
46              s = "Unknown error";
47          } else {
48              int[] buffer = new int[1];
49              int lpBuffer = GetIntArrayElements(buffer);
50              int r = call_28(lookup("FormatMessageA"),
51                              0x1100,     // dwFlags
52                              0,          // lpSource
53                              lastError,  // dwMessageId
54                              0,          // dwLanguageId
55                              lpBuffer,   // lpBuffer
56                              128,        // nSize
57                              0);         // va_list*
58              ReleaseIntArrayElements(buffer, lpBuffer);
59              int p = buffer[0];
60              if (r == 0 || p == 0) {
61                  s = "Error code "+lastError;
62              } else {
63                  s = convertToString(p);
64                  call_4(lookup("LocalFree"), p);
65              }
66          }
67          return new KernelException(s);
68      }
69      
70      public String convertToString(int p) {
71          int len = strlen(p);
72          byte[] b = new byte[len];
73          int pb = GetByteArrayElements(b);
74          memcpy(pb, p, len);
75          ReleaseByteArrayElements(b, pb);
76          return new String(b);
77      }
78      
79      public int call(String procName) {
80          return call_0(lookup(procName));
81      }
82      
83      public int call(String procName, int a) {
84          return call_4(lookup(procName), a);
85      }
86      
87      public int call(String procName, int a, int b) {
88          return call_8(lookup(procName), a, b);
89      }
90      
91      public int call(String procName, int a, int b, int c) {
92          return call_12(lookup(procName), a, b, c);
93      }
94      
95      public int call_z(String procName) {
96          int r = call_0(lookup(procName));
97          if (r != 0)
98              throw handleError();
99          return r;
100     }
101     
102     public int call_z(String procName, int a) {
103         int r = call_4(lookup(procName), a);
104         if (r != 0)
105             throw handleError();
106         return r;
107     }
108     
109     public int call_z(String procName, int a, int b) {
110         int r = call_8(lookup(procName), a, b);
111         if (r != 0)
112             throw handleError();
113         return r;
114     }
115     
116     public int call_z(String procName, int a, int b, int c) {
117         int r = call_12(lookup(procName), a, b, c);
118         if (r != 0)
119             throw handleError();
120         return r;
121     }
122     
123     public int call_z(String procName, int a, int b, int c, int d) {
124         int r = call_16(lookup(procName), a, b, c, d);
125         if (r != 0)
126             throw handleError();
127         return r;
128     }
129     
130     public int call_z(String procName, int a, int b, int c, int d, int e) {
131         int r = call_20(lookup(procName), a, b, c, d, e);
132         if (r != 0)
133             throw handleError();
134         return r;
135     }
136     
137     public int call_z(String procName, int a, int b, int c, int d, int e, int f) {
138         int r = call_24(lookup(procName), a, b, c, d, e, f);
139         if (r != 0)
140             throw handleError();
141         return r;
142     }
143     
144     public int call_z(String procName, int a, int b, int c, int d, int e, int f, int g) {
145         int r = call_28(lookup(procName), a, b, c, d, e, f, g);
146         if (r != 0)
147             throw handleError();
148         return r;
149     }
150     
151     public int call_nz(String procName) {
152         int r = call_0(lookup(procName));
153         if (r == 0)
154             throw handleError();
155         return r;
156     }
157     
158     public int call_nz(String procName, int a) {
159         int r = call_4(lookup(procName), a);
160         if (r == 0)
161             throw handleError();
162         return r;
163     }
164     
165     public int call_nz(String procName, int a, int b) {
166         int r = call_8(lookup(procName), a, b);
167         if (r == 0)
168             throw handleError();
169         return r;
170     }
171     
172     public int call_nz(String procName, int a, int b, int c) {
173         int r = call_12(lookup(procName), a, b, c);
174         if (r == 0)
175             throw handleError();
176         return r;
177     }
178     
179     public int call_nz(String procName, int a, int b, int c, int d) {
180         int r = call_16(lookup(procName), a, b, c, d);
181         if (r == 0)
182             throw handleError();
183         return r;
184     }
185     
186     public int call_nz(String procName, int a, int b, int c, int d, int e) {
187         int r = call_20(lookup(procName), a, b, c, d, e);
188         if (r == 0)
189             throw handleError();
190         return r;
191     }
192     
193     public int call_nz(String procName, int a, int b, int c, int d, int e, int f) {
194         int r = call_24(lookup(procName), a, b, c, d, e, f);
195         if (r == 0)
196             throw handleError();
197         return r;
198     }
199     
200     public int call_nz(String procName, int a, int b, int c, int d, int e, int f, int g) {
201         int r = call_28(lookup(procName), a, b, c, d, e, f, g);
202         if (r == 0)
203             throw handleError();
204         return r;
205     }
206     
207     private int lookup(String name) {
208         Integer i = (Integer) nameToAddress.get(name);
209         if (i == null) {
210             int k = GetProcAddress(libraryHandle, name);
211             if (k == 0) throw new KernelException("Cannot get address for "+name);
212             i = new Integer(k);
213             nameToAddress.put(name, i);
214         }
215         return i.intValue();
216     }
217     
218     // Handle to kernel32 library.
219     private int libraryHandle;
220     // Result of GetLastError()
221     private int lastError;
222     // Cache mapping procedure names to addresses.
223     private Map nameToAddress;
224     
225     // JNI stuff.
226     public static native int GetIntArrayElements(int[] a);
227     public static native void ReleaseIntArrayElements(int[] a, int b);
228     public static native int GetByteArrayElements(byte[] a);
229     public static native void ReleaseByteArrayElements(byte[] a, int b);
230     
231     // C library stuff.
232     public static native int strlen(int p);
233     public static native void memcpy(int to, int from, int size);
234     
235     // Windows stuff.
236     public native int LoadLibrary(String name);
237     public native boolean FreeLibrary(int handle);
238     public native int GetProcAddress(int lib, String name);
239     
240     // Generic calls.
241     private native int call_0(int addr);
242     private native int call_4(int addr, int a);
243     private native int call_8(int addr, int a, int b);
244     private native int call_12(int addr, int a, int b, int c);
245     private native int call_16(int addr, int a, int b, int c, int d);
246     private native int call_20(int addr, int a, int b, int c, int d, int e);
247     private native int call_24(int addr, int a, int b, int c, int d, int e, int f);
248     private native int call_28(int addr, int a, int b, int c, int d, int e, int f, int g);
249     private native int call_32(int addr, int a, int b, int c, int d, int e, int f, int g, int h);
250     
251 }