Java operation registry to disable specified programs

Source: Internet
Author: User

First, we will introduce how to use registry editor to disable the specified software:

The user cannot run the notebook (notepad.exe) and calculator (cal. Exe ):

 

First, in the Registry item HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ CurrentVersion \ Policies \ Explorer, create a dual-byte value item DisallowRun and change its value to 1, to allow us to define prohibited programs, and then create a new registry key HKEY_CURRENT_USER \ Software \ Microsoft \ Windows \ Current Version \ Policies \ Explorer \ DisallowRun, under which two new string value items are created. The first value is 1 and the value is notepad.exe. the second value is 2 and the value is calc.exe. If you want to disable more programs, you can create values listed in the order of 3 and 4. It takes effect immediately after you modify the registry. If you want to run the notepad and calculator programs through the "Start" menu, the system will prompt that you cannot perform this operation.

Note: In the Windows NT/2000/XP command interpreter (cmd.exe) window, you can still run notepad by entering "notepad.exe. This is because DisallowRun only prohibits programs that run through resource manager Explorer. notepad is not started through Explorer, so it cannot be disabled. If you do not want to use the command interpreter to run the program, disable the command interpreter (cmd.exe) in disallowrun. In addition, this method is insecure, that is, if you change the name of the Notepad program "notepad.exe", such as "note.exe", you can run it.

2. Use jRegistry to operate the Registry

JRegistry uses JNI to encapsulate WINDOWS Registry APIs, facilitating java developers to Access windows registry. First, we will introduce jRegistryKey. jar and jRegistryKey. dll, these two files are required to use jRegistry to operate the Registry: A jar package, a file including the java class; a dynamic link library file jRegistryKey. dll, which provides the local code (C/C ++) required to access the registry ).

1. Import jRegistryKey. jar to the eclipse project. (I will not go into details here, but I will not go to google)

2. copy the jRegistryKey. dll file to D: \ WINDOWS \ system32 (my operating system is mounted on disk D)

Code Analysis:

1. Create a cto subitem under RootKey. HKEY_CURRENT_USER \ Software

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. Delete the cto subitem

Try {

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

Cto. delete ();

}

Catch (RegistryException re ){

Re. printStackTrace ();

}

3. view the sub-item under 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

}

 

 

Running result:

 

4. enumerate all values of a subitem (v. toString is the value of the subitem, including the name, type, and data. If the value data corresponding to the subitem is obtained, use v. getData (). toString ())

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

If (r. hasValues ()){

Iterator I = r. values ();

While (I. hasNext ()){

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

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

} // While

}

 

 

 

Running result:

 

5. Read the value of a specified subitem

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

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

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

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

}

Running result:

 

6. Set the value of the entry in the registry.

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 );

 

The following describes how to modify the registry and disable notepad:

 

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 ");

// Create a DWORD value with the value 1

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

// Add a value to the Explorer

R. setValue (v );

// Create the subitem disallowRun under Explorer

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

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

}

}

 

With reference to many posts, the code is self-compiled and not neat enough, but the function is implemented.

If you want to learn more, we suggest you download the corresponding javadoc to see if there are not many classes in the api, which is easy to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.