当前位置:编程学习 > JAVA >>

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子项

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

cto.create(); 

 

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

cto.createSubkey("51cto");  

 

2.删除cto子项

try {

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

   cto.delete();

catch(RegistryException re) {

   re.printStackTrace();

}

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

RegistryKey adobe = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\Adobe");   

if(adobe .hasSubkeys()) {   

   Iterator i =adobe .subkeys();   

   while(i.hasNext()) {   

      RegistryKey x = (RegistryKey)i.next();   

      System.out.println(x.toString());   

   } // while   

}

 

 

运行结果:

 

4.枚举某子项的所有值(v.toString是获得子项的值,值包括名称,类型,数据。若获得子项对应的值数据用v.getData().toString())

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, "Software\\360desktop");   

if(r.hasValues()) {   

   Iterator i = r.values();   

   while(i.hasNext()) {   

      RegistryValue v = (RegistryValue)i.next();   

      System.out.println(v.toString());   

   } // while   

}

 

 

 

运行结果:

 

5.读指定子项的值

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER,  "Software\\360desktop");   

if(r.hasValue("appinstall1")) {   

   RegistryValue v = r.getValue("appinstall1");   

   System.out.println(v.toString());//   

运行结果:

 

6.设置注册表中项的值

RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, 

        "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"); 

 

RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1);

 

r.setValue(v);

 

下面实现文章上文提到的修改注册表禁用记事本的操作:

 

package test;

import ca.beq.util.win32.registry.RegistryKey;

import ca.beq.util.win32.registry.RegistryValue;

import ca.beq.util.win32.registry.RootKey;

import ca.beq.util.win32.registry.ValueType;

 

public class JregistryTest {

 

    public static void main(String[] args) {

        RegistryKey r = new RegistryKey(RootKey.HKEY_CURRENT_USER, 

                "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\Explorer"); 

        //创建一个DWORD类型的值,值数据为1

        RegistryValue v = new RegistryValue("DisallowRun", ValueType.REG_DWORD, 1);

        //Explorer项中添加值v

        r.setValue(v);

        //在Explorer下创建子项disallowRun

        RegistryKey disallowRun = r.createSubkey("DisallowRun");

        

        disallowRun.setValue(new RegistryValue("1",ValueType.REG_SZ, "notepad.exe"));

    }

}

 

参照了很多贴子,代码都是自己编的,不够整洁,但功能实现了。

如果想进一步了解的同学,建议下载相应的javadoc看看,api里的类不多,很容易掌握

补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,