java操作註冊表實現禁用指定程序

首先,介紹一下 用註冊表編輯器實現禁用指定軟件的操作:

禁止用戶運行記事本(notepad.exe)和計算器(cal.Exe):

 

首先在註冊表項HKEY_CURRENT_USER\Software\Microsoft \Windows\CurrentVersion\Policies\Explorer中,新建一個雙字節值項DisallowRun,修改其值爲1,以允許我們定義禁止允許的程序,然後新建一個註冊表項HKEY_CURRENT_USER\Software\Microsoft\ Windows\Current Version\Policies\Explorer\DisallowRun,在其下新建兩個字符串值項。第一個值項的名稱爲1,值爲notepad.exe,第二個值項爲2,值爲calc.exe。如果想禁止更多的程序,可以依次建立名稱爲3、4等順序往下排列的值項。修改註冊表後立即生效。這時想通過"開始"菜單運行記事本和計算器程序,系統會提示不能進行此操作。

  注意:用戶在Windows NT/2000/XP的命令解釋器(CMD.exe)窗口中,仍然可以通過輸入"notepad.exe"運行記事本。這是因爲DisallowRun禁止的只是通過資源管理器Explorer運行的程序,記事本不是通過Explorer啓動的,所以就無法禁止了。如果不希望用戶可以通過命令解釋器運行程序,應該在DisallowRun中將命令解釋器(CMD.exe)禁止。另外,此方式還有一個不安全之處,就是如果用戶將記事本程序"notepad.exe"更改名稱,如改成"note.exe",用戶就可以運行它了。

二. 用jRegistry 來操作註冊表

 jRegistry它是用JNI來封裝WINDOWS註冊表API,方便了java開發者訪問windows註冊表。首先介紹一下jRegistryKey.jar和jRegistryKey.dll,這兩個文件是使用jRegistry來操作註冊表所必需的文件:一個是jar包,是一個包括了java類的文件;一個是動態鏈接庫文件jRegistryKey.dll,提供了訪問註冊表所需的本地代碼(即C/C++)。

1.將jRegistryKey.jar導入eclipse工程中。(這個就不詳細介紹了,不會的google去)

2.將jRegistryKey.dll文件copy到D:\WINDOWS\system32下(我的操作系統裝在D盤)

代碼分析:

1.在RootKey.HKEY_CURRENT_USER\Software下創建cto子項

  1. RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\cto");    
  2. cto.create();  

  1. RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\cto");    
  2. cto.createSubkey("51cto");   

2.刪除cto子項

  1. try { 
  2.    RegistryKey cto = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\cto"); 
  3.    cto.delete(); 
  4. }  
  5. catch(RegistryException re) { 
  6.    re.printStackTrace(); 

3.查看RootKey.HKEY_CURRENT_USER/Software/Adobe下的子項

  1. RegistryKey adobe = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\Adobe");    
  2. if(adobe .hasSubkeys()) {    
  3.    Iterator i =adobe .subkeys();    
  4.    while(i.hasNext()) {    
  5.       RegistryKey x = (RegistryKey)i.next();    
  6.       System.out.println(x.toString());    
  7.    } // while    

運行結果:

4.枚舉某子項的所有值(v.toString是獲得子項的值,值包括名稱,類型,數據。若獲得子項對應的值數據用v.getData().toString())

  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\360desktop");    
  2. if(r.hasValues()) {    
  3.    Iterator i = r.values();    
  4.    while(i.hasNext()) {    
  5.       RegistryValue v = (RegistryValue)i.next();    
  6.       System.out.println(v.toString());    
  7.    } // while    

 

 

運行結果:

5.讀指定子項的值

  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\360desktop");    
  2. if(r.hasValue("appinstall1")) {    
  3.    RegistryValue v = r.getValue("appinstall1");    
  4.    System.out.println(v.toString());//    
  5. }  

運行結果:

6.設置註冊表中項的值

  1. RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  
  2.         "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");  
  3.  
  4. RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1); 
  5.  
  6. r.setValue(v); 

下面實現文章上文提到的修改註冊表禁用記事本的操作:

 

  1. package test; 
  2. import ca.beq.util.win32.registry.RegistryKey; 
  3. import ca.beq.util.win32.registry.RegistryValue; 
  4. import ca.beq.util.win32.registry.RootKey; 
  5. import ca.beq.util.win32.registry.ValueType; 
  6.  
  7. public class JregistryTest { 
  8.  
  9.     public static void main(String[] args) { 
  10.         RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  
  11.                 "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer");  
  12.         //創建一個DWORD類型的值,值數據爲1 
  13.         RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1); 
  14.         //Explorer項中添加值v 
  15.         r.setValue(v); 
  16.         //在Explorer下創建子項disallowRun 
  17.         RegistryKey disallowRun = r.createSubkey("DisallowRun"); 
  18.          
  19.         disallowRun.setValue(new RegistryValue("1",ValueType.REG_SZ, "notepad.exe")); 
  20.     } 

參照了很多貼子,代碼都是自己編的,不夠整潔,但功能實現了。

如果想進一步瞭解的同學,建議下載相應的javadoc看看,api裏的類不多,很容易掌握

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章