Self-made Windows 7 registry key value Modification Service (service)

Source: Internet
Author: User

First, let's explain why we need to write such a service. Because the computer needs to be used in the Company domain, it will inevitably inherit the Group Policy configuration in the domain. 95% of computers in the domain are Windows XP systems, and some group policies are redundant and troublesome for Windows 7 systems.

Problem 1: Clear the Virtual Memory Policy

Clear Virtual Memory Pagefile can reduce the spread of computer viruses in the domain to a certain extent. But the problem is that if you start this policy, it will also reduce the Windows Shutdown speed (3 ~ 5 minutes), the shutdown speed is extremely fast (13 ~ 16 seconds) Windows 7 is a heavy blow.

To modify this group policy, you only need[HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Memory Management]InClearpagefileatshutdownSetZero X 00000000You can.

Problem 2: system update policy

In the domain, a group policy can be used to allow computers to perform patch updates through WSUS. Although it is set for the XP system, it completely destroys the update mechanism of the Windows 7 system, for more information, see Windows Update error: 80244019. To solve this problem, you only need[HKEY_LOCAL_MACHINE \ SOFTWARE \ Policies \ Microsoft \ Windows \ windowsupdate]Completely Delete.

Solution

It can be seen that the above two problems can be solved by modifying the Registry. Of course, some people may say that they can also modify the domain group policy to apply the Group Policy on a host that is not a domain user, however, this method is not recommended. At first, I wrote a reg file and ran it to update the registry, but it was always troublesome to do so. Therefore, you need to create a service to run it in the background and modify the Registry content.

 
Windows Registry Editor Version 5.00 [-HKEY_LOCAL_MACHINE \ SOFTWARE \ Policies \ Microsoft \ Windows \ windowsupdate] [HKEY_LOCAL_MACHINE \ SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Memory Management]"Clearpagefileatshutdown"= DWORD: 00000000

Create a Windows Service Project, create a regvalueset class, and write the followingCodeContent. In the class, the changekeyvalue () method is used to modify the registry. Registry. localmachine. deletesubkeytree (updatepath); the system will delete the system update group policy information, regsetvalueex (hkey, keyname, 0, registryvaluekind. DWORD, keyval, 4); The clearpagefileatshutdown key value is changed to 0.

At first, I tried to use the regpolicychangekeyvalue method to monitor whether the key values under SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Memory Management were modified by the domain group policy. If yes, the regsetvalueex method will be executed, however, this method does not seem to work in the service (it will remain in the listening state when the service is started ). The test shows that you only need to perform the changekeyvalue () operation on/off, so you can change the code to the following method.

 Using System; Using System. runtime. interopservices; Using Microsoft. Win32; Namespace Regmonitor { Class  Regvalueset { Private Static  Uintptr HKEY_LOCAL_MACHINE = New Uintptr (0x80000002u ); Private Static  Uintptr Hkey; Private const int Keyrights = 0xf003f; // Key_all_access (0xf003f) 
 
  
  
   Private const Uint32Infinite = 0 xffffffff; 
   
 
         
 
  
  
   Private const Uint32Wait_failed = 0 xffffffff; 
   
 
          Public static void Changekeyvalue (){ String Clrpath =@ "SYSTEM \ CurrentControlSet \ Control \ Session Manager \ Memory Management" ; String Updatepath = @ "SOFTWARE \ Policies \ Microsoft \ Windows \ windowsupdate" ; String Keyname = "Clearpagefileatshutdown" ; // Delete Windows Update settings  Int Vopen = regopenkeyex (HKEY_LOCAL_MACHINE, updatepath, 0, keyrights, Out Hkey ); If (Vopen = 0 ){Registry . Localmachine. deletesubkeytree (updatepath );} // Change clear page file value Regopenkeyex (HKEY_LOCAL_MACHINE, clrpath, 0, keyrights, Out Hkey );
 
  
  
   IntptrHevent = createevent ( Intptr. Zero, True, False, Null); 
   
 
             
 
  
  
  
    Regpolicychangekeyvalue (hkey, True, 4, hevent, True); 
   
 
             
 
  
  
   While(Waitforsingleobject (hevent, infinite )! = Wait_failed) 
   
 
             
 
  
  
  
    { 
   
 
                 Registrykey Key = Registry . Localmachine. opensubkey (clrpath ); Int Val = ( Int ) Key. getvalue (keyname ); If (Val! = 0 ){ Intptr Keyval = Marshal . Allochglobal (4 ); Marshal . Writeint32 (keyval, 0, 0); regsetvalueex (hkey, keyname, 0, Registryvaluekind . DWORD, keyval, 4); key. Close () ;}regclosekey (hkey );
 
  
  
  
    } 
   
 
 }[ Dllimport ( "Advapi32.dll" , Charset = Charset . Auto, setlasterror = True )] Public static extern int Regopenkeyex ( Uintptr Hkey, String Subkey, Int Uloptions, Int Samdesired,Out  Uintptr Hkresult );[ Dllimport ( "Advapi32.dll" , Setlasterror = True )] Public static extern int Regsetvalueex ( Uintptr Hkey ,[ Financialas ( Unmanagedtype . Lpstr)] String Lpvaluename, Int Reserved,Registryvaluekind Dwtype, Intptr Lpdata, Int Cbdata );[ Dllimport ( "Advapi32.dll" , Setlasterror = True )] Public static extern int Regclosekey ( Uintptr Hkey );
 
  
  
  
    [ Dllimport( "Kernel32.dll")] 
   
 
         
 
  
  
   Public static extern IntptrCreateevent ( 
   
 
             
 
 
  
   IntptrLpeventattributes, 
   
 
             
 
  
  
   BoolBmanualreset, 
   
 
             
 
  
  
   BoolBinitialstate, 
   
 
             
 
  
  
   StringLpname ); 
   
 
         
 
  
  
  
    [ Dllimport( "Advapi32.dll", Setlasterror = True)] 
   
 
         
 
  
  
   Public static extern intRegpolicychangekeyvalue ( 
   
 
             
 
  
  
   UintptrHkey, 
   
 
             
 
  
  
   BoolWatchsubtree, 
   
 
             
 
  
  
   IntDwpolicyfilter, 
   
 
             
 
  
  
   IntptrHevent, 
   
 
             
 
 
  
   BoolFasynchronous ); 
   
 
         
 
  
  
  
    [ Dllimport( "Kernel32.dll", Setlasterror = True)] 
   
 
         
 
  
  
   Static extern Uint32Waitforsingleobject ( 
   
 
             
 
  
  
   IntptrHhandle, 
   
 
             
 
  
  
   Uint32Dwmilliseconds ); 
   
 
 }}

Finally, set canshutdown and canstop to true in the service attribute, and set the service name to registrymonitor. Add the changekeyvalue () method to onstart, onstop, and onshutdown, as shown in the following code.

UsingSystem. serviceprocess;NamespaceRegmonitor {Public partial classService1:Servicebase{PublicService1 () {initializecomponent ();}Protected override voidOnstart (String[] ARGs ){Regvalueset. Changekeyvalue ();}Protected override voidOnshutdown (){Regvalueset. Changekeyvalue ();}Protected override voidOnstop (){Regvalueset. Changekeyvalue ();}}}

Compile the project, install the registrymonitor Service (installutil), and start the registrymonitor service in services. MSC. So far, you no longer have to worry about the inheritance of group policies.

References

1. regopenkeyex Function

The http://msdn.microsoft.com/en-us/library/ms724897 (vs.85). aspx

2. regsetvalueex Function

The http://msdn.microsoft.com/en-us/library/ms724923 (vs.85). aspx

3. registrykey Methods

Http://msdn.microsoft.com/en-US/library/microsoft.win32.registrykey_methods (V = vs.80). aspx

Source Program Download

Related Article

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.