1 20 21 package org.jdesktop.jdic.desktop.internal.impl; 22 23 24 29 public class WinAPIWrapper { 30 static { 31 System.loadLibrary("jdic"); 32 33 Runtime.getRuntime().addShutdownHook(new Thread () { 34 public void run() {shutDown();} 35 }); 36 } 37 38 41 public final static int HKEY_CLASSES_ROOT = 0x80000000; 42 public final static int HKEY_CURRENT_USER = 0x80000001; 43 public final static int HKEY_LOCAL_MACHINE = 0x80000002; 44 45 46 public static final int ERROR_SUCCESS = 0; 47 48 49 public static final int MAX_KEY_LENGTH = 255; 50 51 52 private static final int OPENED_KEY_HANDLE = 0; 53 private static final int ERROR_CODE = 1; 54 private static final int SUBKEYS_NUMBER = 0; 55 56 57 public static final int KEY_READ = 0x20019; 58 59 65 private static native int[] RegOpenKey(int hKey, byte[] subKey, 66 int securityMask); 67 68 71 private static native int RegCloseKey(int hKey); 72 73 76 private static native byte[] RegQueryValueEx(int hKey, byte[] valueName); 77 78 81 private static native byte[] AssocQueryString(byte[] fileExt, byte[] verb); 82 83 86 private static native byte[] ExpandEnvironmentStrings(byte[] envBytes); 87 88 94 private static native String resolveLinkFile(byte[] filePath); 95 96 102 private static native int shellExecute(byte[] filePath, byte[] verb); 103 104 114 private static synchronized native void openMapiMailer(String [] toArray, String [] ccArray, 115 String [] bccArray, String subject, String body, String [] attachArray); 116 117 120 protected static native void shutDown(); 121 122 129 private static native boolean nativeBrowseURLInIE(String urlStr, String target); 130 131 132 135 private static byte[] stringToByteArray(String str) { 136 if (str == null) { 137 return null; 138 } 139 140 byte[] srcByte = str.getBytes(); 141 int srcLength = srcByte.length; 142 byte[] result = new byte[srcLength + 1]; 143 144 System.arraycopy(srcByte, 0, result, 0, srcLength); 145 result[srcLength] = 0; 146 147 return result; 148 } 149 150 153 private static String byteArrayToString(byte[] array) { 154 if (array != null) { 155 String temString = new String (array); 156 157 if (temString != null) { 158 return temString.substring(0, temString.length() - 1); 159 } 160 } 161 return null; 162 } 163 164 167 private WinAPIWrapper() {} 168 169 177 public static String WinRegQueryValueEx(int hKey, String subKey, String valueName) { 178 byte[] lpSubKey = stringToByteArray(subKey); 179 int[] openResult = RegOpenKey(hKey, lpSubKey, KEY_READ); 180 181 if (openResult == null) { 182 return null; 183 } 184 185 if (openResult[ERROR_CODE] != ERROR_SUCCESS) { 186 return null; 187 } else { 188 byte[] valueBytes; 189 byte[] lpValueName = stringToByteArray(valueName); 190 191 valueBytes = 192 RegQueryValueEx(openResult[OPENED_KEY_HANDLE], lpValueName); 193 RegCloseKey(openResult[OPENED_KEY_HANDLE]); 194 195 if (valueBytes != null) { 196 if ((valueBytes.length == 1) && (valueBytes[0] == 0) && (valueName.equals("")) ){ 197 return null; 198 } else { 199 return byteArrayToString(valueBytes); 200 } 201 } else { 202 return null; 203 } 204 } 205 } 206 207 214 public static String WinAssocQueryString(String fileOrProtocal, String verb) { 215 byte[] fileOrProtocalBytes = stringToByteArray(fileOrProtocal); 216 byte[] verbBytes = stringToByteArray(verb); 217 218 byte[] queryResult = AssocQueryString(fileOrProtocalBytes, verbBytes); 219 if (queryResult != null) { 220 if ((queryResult.length == 1) && (queryResult[0] == 0) && (queryResult.equals("")) ){ 221 return null; 222 } else { 223 return byteArrayToString(queryResult); 224 } 225 } else { 226 return null; 227 } 228 } 229 230 236 public static String WinResolveLinkFile(String filePath) { 237 byte[] filePathBytes = stringToByteArray(filePath); 238 return resolveLinkFile(filePathBytes); 239 } 240 241 247 public static boolean WinShellExecute(String filePath, String verb) { 248 byte[] filePathBytes = stringToByteArray(filePath); 249 byte[] verbBytes = stringToByteArray(verb); 250 int exeResult = shellExecute(filePathBytes, verbBytes); 251 if (exeResult > 32) { 252 return true; 253 } else { 254 return false; 255 } 256 } 257 258 public static boolean WinBrowseURLInIE(String urlStr, String target) { 259 return nativeBrowseURLInIE(urlStr, target); 260 } 261 262 272 public static synchronized void WinOpenMapiMailer(String [] toArray, String [] ccArray, 273 String [] bccArray, String subject, String body, String [] attachArray) { 274 openMapiMailer(toArray, ccArray, bccArray, subject, body, attachArray); 275 } 276 } 277 278 279 | Popular Tags |