Guest User

Untitled

a guest
May 21st, 2013
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.69 KB | None | 0 0
  1. /*     */ package rg;
  2. /*     */
  3. /*     */ import java.lang.reflect.InvocationTargetException;
  4. /*     */ import java.lang.reflect.Method;
  5. /*     */ import java.util.ArrayList;
  6. /*     */ import java.util.HashMap;
  7. /*     */ import java.util.List;
  8. /*     */ import java.util.Map;
  9. /*     */ import java.util.prefs.Preferences;
  10. /*     */
  11. /*     */ public class WinRegistry
  12. /*     */ {
  13. /*     */   public static final int HKEY_CURRENT_USER = -2147483647;
  14. /*     */   public static final int HKEY_LOCAL_MACHINE = -2147483646;
  15. /*     */   public static final int REG_SUCCESS = 0;
  16. /*     */   public static final int REG_NOTFOUND = 2;
  17. /*     */   public static final int REG_ACCESSDENIED = 5;
  18. /*     */   public static final int KEY_WOW64_32KEY = 512;
  19. /*     */   public static final int KEY_WOW64_64KEY = 256;
  20. /*     */   private static final int KEY_ALL_ACCESS = 983103;
  21. /*     */   private static final int KEY_READ = 131097;
  22. /*  35 */   private static Preferences userRoot = Preferences.userRoot();
  23. /*  36 */   private static Preferences systemRoot = Preferences.systemRoot();
  24. /*  37 */   private static Class<? extends Preferences> userClass = userRoot.getClass();
  25. /*  38 */   private static Method regOpenKey = null;
  26. /*  39 */   private static Method regCloseKey = null;
  27. /*  40 */   private static Method regQueryValueEx = null;
  28. /*  41 */   private static Method regEnumValue = null;
  29. /*  42 */   private static Method regQueryInfoKey = null;
  30. /*  43 */   private static Method regEnumKeyEx = null;
  31. /*  44 */   private static Method regCreateKeyEx = null;
  32. /*  45 */   private static Method regSetValueEx = null;
  33. /*  46 */   private static Method regDeleteKey = null;
  34. /*  47 */   private static Method regDeleteValue = null;
  35. /*     */
  36. /*     */   public static String readString(int hkey, String key, String valueName, int wow64)
  37. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  38. /*     */   {
  39. /*  96 */     if (hkey == -2147483646) {
  40. /*  97 */       return readString(systemRoot, hkey, key, valueName, wow64);
  41. /*     */     }
  42. /*  99 */     if (hkey == -2147483647) {
  43. /* 100 */       return readString(userRoot, hkey, key, valueName, wow64);
  44. /*     */     }
  45. /*     */
  46. /* 103 */     throw new IllegalArgumentException("hkey=" + hkey);
  47. /*     */   }
  48. /*     */
  49. /*     */   public static Map<String, String> readStringValues(int hkey, String key, int wow64)
  50. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  51. /*     */   {
  52. /* 122 */     if (hkey == -2147483646) {
  53. /* 123 */       return readStringValues(systemRoot, hkey, key, wow64);
  54. /*     */     }
  55. /* 125 */     if (hkey == -2147483647) {
  56. /* 126 */       return readStringValues(userRoot, hkey, key, wow64);
  57. /*     */     }
  58. /*     */
  59. /* 129 */     throw new IllegalArgumentException("hkey=" + hkey);
  60. /*     */   }
  61. /*     */
  62. /*     */   public static List<String> readStringSubKeys(int hkey, String key, int wow64)
  63. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  64. /*     */   {
  65. /* 148 */     if (hkey == -2147483646) {
  66. /* 149 */       return readStringSubKeys(systemRoot, hkey, key, wow64);
  67. /*     */     }
  68. /* 151 */     if (hkey == -2147483647) {
  69. /* 152 */       return readStringSubKeys(userRoot, hkey, key, wow64);
  70. /*     */     }
  71. /*     */
  72. /* 155 */     throw new IllegalArgumentException("hkey=" + hkey);
  73. /*     */   }
  74. /*     */
  75. /*     */   public static void createKey(int hkey, String key)
  76. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  77. /*     */   {
  78. /* 171 */     if (hkey == -2147483646) {
  79. /* 172 */       int[] ret = createKey(systemRoot, hkey, key);
  80. /* 173 */       regCloseKey.invoke(systemRoot, new Object[] { new Integer(ret[0]) });
  81. /*     */     }
  82. /* 175 */     else if (hkey == -2147483647) {
  83. /* 176 */       int[] ret = createKey(userRoot, hkey, key);
  84. /* 177 */       regCloseKey.invoke(userRoot, new Object[] { new Integer(ret[0]) });
  85. /*     */     }
  86. /*     */     else {
  87. /* 180 */       throw new IllegalArgumentException("hkey=" + hkey);
  88. /*     */     }
  89. /*     */     int[] ret;
  90. /* 182 */     if (ret[1] != 0)
  91. /* 183 */       throw new IllegalArgumentException("rc=" + ret[1] + "  key=" + key);
  92. /*     */   }
  93. /*     */
  94. /*     */   public static void writeStringValue(int hkey, String key, String valueName, String value, int wow64)
  95. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  96. /*     */   {
  97. /* 204 */     if (hkey == -2147483646) {
  98. /* 205 */       writeStringValue(systemRoot, hkey, key, valueName, value, wow64);
  99. /*     */     }
  100. /* 207 */     else if (hkey == -2147483647) {
  101. /* 208 */       writeStringValue(userRoot, hkey, key, valueName, value, wow64);
  102. /*     */     }
  103. /*     */     else
  104. /* 211 */       throw new IllegalArgumentException("hkey=" + hkey);
  105. /*     */   }
  106. /*     */
  107. /*     */   public static void deleteKey(int hkey, String key)
  108. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  109. /*     */   {
  110. /* 226 */     int rc = -1;
  111. /* 227 */     if (hkey == -2147483646) {
  112. /* 228 */       rc = deleteKey(systemRoot, hkey, key);
  113. /*     */     }
  114. /* 230 */     else if (hkey == -2147483647) {
  115. /* 231 */       rc = deleteKey(userRoot, hkey, key);
  116. /*     */     }
  117. /* 233 */     if (rc != 0)
  118. /* 234 */       throw new IllegalArgumentException("rc=" + rc + "  key=" + key);
  119. /*     */   }
  120. /*     */
  121. /*     */   public static void deleteValue(int hkey, String key, String value, int wow64)
  122. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  123. /*     */   {
  124. /* 253 */     int rc = -1;
  125. /* 254 */     if (hkey == -2147483646) {
  126. /* 255 */       rc = deleteValue(systemRoot, hkey, key, value, wow64);
  127. /*     */     }
  128. /* 257 */     else if (hkey == -2147483647) {
  129. /* 258 */       rc = deleteValue(userRoot, hkey, key, value, wow64);
  130. /*     */     }
  131. /* 260 */     if (rc != 0)
  132. /* 261 */       throw new IllegalArgumentException("rc=" + rc + "  key=" + key + "  value=" + value);
  133. /*     */   }
  134. /*     */
  135. /*     */   private static int deleteValue(Preferences root, int hkey, String key, String value, int wow64)
  136. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  137. /*     */   {
  138. /* 269 */     int[] handles = (int[])regOpenKey.invoke(root, new Object[] { new Integer(hkey), toCstr(key), new Integer(0xF003F | wow64) });
  139. /*     */
  140. /* 272 */     if (handles[1] != 0) {
  141. /* 273 */       return handles[1];
  142. /*     */     }
  143. /* 275 */     int rc = ((Integer)regDeleteValue.invoke(root, new Object[] { new Integer(handles[0]), toCstr(value) })).intValue();
  144. /*     */
  145. /* 278 */     regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  146. /* 279 */     return rc;
  147. /*     */   }
  148. /*     */
  149. /*     */   private static int deleteKey(Preferences root, int hkey, String key)
  150. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  151. /*     */   {
  152. /* 286 */     int rc = ((Integer)regDeleteKey.invoke(root, new Object[] { new Integer(hkey), toCstr(key) })).intValue();
  153. /*     */
  154. /* 289 */     return rc;
  155. /*     */   }
  156. /*     */
  157. /*     */   private static String readString(Preferences root, int hkey, String key, String value, int wow64)
  158. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  159. /*     */   {
  160. /* 296 */     int[] handles = (int[])regOpenKey.invoke(root, new Object[] { new Integer(hkey), toCstr(key), new Integer(0x20019 | wow64) });
  161. /*     */
  162. /* 299 */     if (handles[1] != 0) {
  163. /* 300 */       return null;
  164. /*     */     }
  165. /* 302 */     byte[] valb = (byte[])regQueryValueEx.invoke(root, new Object[] { new Integer(handles[0]), toCstr(value) });
  166. /*     */
  167. /* 305 */     regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  168. /* 306 */     return valb != null ? new String(valb).trim() : null;
  169. /*     */   }
  170. /*     */
  171. /*     */   private static Map<String, String> readStringValues(Preferences root, int hkey, String key, int wow64)
  172. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  173. /*     */   {
  174. /* 313 */     HashMap results = new HashMap();
  175. /* 314 */     int[] handles = (int[])regOpenKey.invoke(root, new Object[] { new Integer(hkey), toCstr(key), new Integer(0x20019 | wow64) });
  176. /*     */
  177. /* 317 */     if (handles[1] != 0) {
  178. /* 318 */       return null;
  179. /*     */     }
  180. /* 320 */     int[] info = (int[])regQueryInfoKey.invoke(root, new Object[] { new Integer(handles[0]) });
  181. /*     */
  182. /* 324 */     int count = info[2];
  183. /* 325 */     int maxlen = info[3];
  184. /* 326 */     for (int index = 0; index < count; index++) {
  185. /* 327 */       byte[] name = (byte[])regEnumValue.invoke(root, new Object[] { new Integer(handles[0]), new Integer(index), new Integer(maxlen + 1) });
  186. /*     */
  187. /* 330 */       String value = readString(hkey, key, new String(name), wow64);
  188. /* 331 */       results.put(new String(name).trim(), value);
  189. /*     */     }
  190. /* 333 */     regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  191. /* 334 */     return results;
  192. /*     */   }
  193. /*     */
  194. /*     */   private static List<String> readStringSubKeys(Preferences root, int hkey, String key, int wow64)
  195. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  196. /*     */   {
  197. /* 341 */     List results = new ArrayList();
  198. /* 342 */     int[] handles = (int[])regOpenKey.invoke(root, new Object[] { new Integer(hkey), toCstr(key), new Integer(0x20019 | wow64) });
  199. /*     */
  200. /* 345 */     if (handles[1] != 0) {
  201. /* 346 */       return null;
  202. /*     */     }
  203. /* 348 */     int[] info = (int[])regQueryInfoKey.invoke(root, new Object[] { new Integer(handles[0]) });
  204. /*     */
  205. /* 352 */     int count = info[0];
  206. /* 353 */     int maxlen = info[3];
  207. /* 354 */     for (int index = 0; index < count; index++) {
  208. /* 355 */       byte[] name = (byte[])regEnumKeyEx.invoke(root, new Object[] { new Integer(handles[0]), new Integer(index), new Integer(maxlen + 1) });
  209. /*     */
  210. /* 358 */       results.add(new String(name).trim());
  211. /*     */     }
  212. /* 360 */     regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  213. /* 361 */     return results;
  214. /*     */   }
  215. /*     */
  216. /*     */   private static int[] createKey(Preferences root, int hkey, String key)
  217. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  218. /*     */   {
  219. /* 368 */     return (int[])regCreateKeyEx.invoke(root, new Object[] { new Integer(hkey), toCstr(key) });
  220. /*     */   }
  221. /*     */
  222. /*     */   private static void writeStringValue(Preferences root, int hkey, String key, String valueName, String value, int wow64)
  223. /*     */     throws IllegalArgumentException, IllegalAccessException, InvocationTargetException
  224. /*     */   {
  225. /* 377 */     int[] handles = (int[])regOpenKey.invoke(root, new Object[] { new Integer(hkey), toCstr(key), new Integer(0xF003F | wow64) });
  226. /*     */
  227. /* 380 */     regSetValueEx.invoke(root, new Object[] { new Integer(handles[0]), toCstr(valueName), toCstr(value) });
  228. /*     */
  229. /* 383 */     regCloseKey.invoke(root, new Object[] { new Integer(handles[0]) });
  230. /*     */   }
  231. /*     */
  232. /*     */   private static byte[] toCstr(String str)
  233. /*     */   {
  234. /* 389 */     byte[] result = new byte[str.length() + 1];
  235. /*     */
  236. /* 391 */     for (int i = 0; i < str.length(); i++) {
  237. /* 392 */       result[i] = ((byte)str.charAt(i));
  238. /*     */     }
  239. /* 394 */     result[str.length()] = 0;
  240. /* 395 */     return result;
  241. /*     */   }
  242. /*     */
  243. /*     */   static
  244. /*     */   {
  245. /*     */     try
  246. /*     */     {
  247. /*  51 */       regOpenKey = userClass.getDeclaredMethod("WindowsRegOpenKey", new Class[] { Integer.TYPE, [B.class, Integer.TYPE });
  248. /*  52 */       regOpenKey.setAccessible(true);
  249. /*  53 */       regCloseKey = userClass.getDeclaredMethod("WindowsRegCloseKey", new Class[] { Integer.TYPE });
  250. /*  54 */       regCloseKey.setAccessible(true);
  251. /*  55 */       regQueryValueEx = userClass.getDeclaredMethod("WindowsRegQueryValueEx", new Class[] { Integer.TYPE, [B.class });
  252. /*  56 */       regQueryValueEx.setAccessible(true);
  253. /*  57 */       regEnumValue = userClass.getDeclaredMethod("WindowsRegEnumValue", new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE });
  254. /*  58 */       regEnumValue.setAccessible(true);
  255. /*  59 */       regQueryInfoKey = userClass.getDeclaredMethod("WindowsRegQueryInfoKey1", new Class[] { Integer.TYPE });
  256. /*  60 */       regQueryInfoKey.setAccessible(true);
  257. /*  61 */       regEnumKeyEx = userClass.getDeclaredMethod("WindowsRegEnumKeyEx", new Class[] { Integer.TYPE, Integer.TYPE, Integer.TYPE });
  258. /*  62 */       regEnumKeyEx.setAccessible(true);
  259. /*  63 */       regCreateKeyEx = userClass.getDeclaredMethod("WindowsRegCreateKeyEx", new Class[] { Integer.TYPE, [B.class });
  260. /*  64 */       regCreateKeyEx.setAccessible(true);
  261. /*  65 */       regSetValueEx = userClass.getDeclaredMethod("WindowsRegSetValueEx", new Class[] { Integer.TYPE, [B.class, [B.class });
  262. /*  66 */       regSetValueEx.setAccessible(true);
  263. /*  67 */       regDeleteValue = userClass.getDeclaredMethod("WindowsRegDeleteValue", new Class[] { Integer.TYPE, [B.class });
  264. /*  68 */       regDeleteValue.setAccessible(true);
  265. /*  69 */       regDeleteKey = userClass.getDeclaredMethod("WindowsRegDeleteKey", new Class[] { Integer.TYPE, [B.class });
  266. /*  70 */       regDeleteKey.setAccessible(true);
  267. /*     */     }
  268. /*     */     catch (Exception e) {
  269. /*  73 */       e.printStackTrace();
  270. /*     */     }
  271. /*     */   }
  272. /*     */ }
  273.  
  274. /* Location:           C:\Users\Alexamdre\AppData\Local\Temp\Install.jar
  275.  * Qualified Name:     rg.WinRegistry
  276.  * JD-Core Version:    0.6.2
  277.  */
Advertisement
Add Comment
Please, Sign In to add comment