Solved

I need to know how to turn on and configure Automatic Windwos Update for Windows

Posted on 2004-09-27
5
150 Views
Last Modified: 2011-09-20
My vb6 code needs to do the following:
1. determine if Auto update is turned on or not
2. be able to turn it on programmatically.

Looking for information regarding, but would prefer some sample code.

I am already familiar with the following links:
--http://www.sanx.org/tipShow.asp?articleRef=115
--http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wua_sdk/wua/portal_client.asp

...If you want to look at those articles and give me code that does the 2 things listed above, that would suffice.

thanks!

0
Comment
Question by:apm825
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:David Lee
Comment Utility
This should do it.

Dim wshShell As New wshShell
wshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\NoAutoUpdate", 0, "REG_DWORD"
wshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\AUOptions", 3, "REG_DWORD"
wshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\ScheduledInstallDay", 0, "REG_DWORD"
wshShell.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU\ScheduledInstallTime", 3, "REG_DWORD"
set wshShell = Nothing

This link takes you to a page that spells out what the individual values do.
http://www.winguides.com/registry/display.php/1176/
0
 

Author Comment

by:apm825
Comment Utility
I'm not sure if I'll be able to do early binding, do you know the full prog id?

If I can, what is the reference library name?
0
 
LVL 76

Accepted Solution

by:
David Lee earned 300 total points
Comment Utility
If you want to use late binding then use this instead:

    Dim wshShell As Object
    Set wshShell = CreateObject("Wscript.Shell")

Set a reference in your project to "Windows Script Host Object Model"
0
 
LVL 4

Assisted Solution

by:avi247
avi247 earned 200 total points
Comment Utility
'Ref: http://www.sanx.org/tipShow.asp?articleRef=115
'HKEY_LOCAL_MACHINE\Software\Policies\Microsoft\Windows\WindowsUpdate\AU

'# Data Type: DWORD
'Value Name: NoAutoUpdate
'Value: 0 or 1
'
'    * 0: Automatic Updates are enabled (default)
'    * 1: Automatic Updates are disabled
'

'# Data Type: DWORD
'Value Name: AUOptions
'Value: 1 through 4
'
'    * 1: Automatic Updates are disabled
'    * 2: Notify of download and installation
'    * 3: Automatically download and notify of installation
'    * 4: Automatically download and scheduled installation
'
Option Explicit

  Private Declare Function RegCloseKey Lib "advapi32.dll" (ByVal lngRootKey As Long) As Long
 
  Private Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
            (ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 
  Private Declare Function RegOpenKey Lib "advapi32.dll" Alias "RegOpenKeyA" _
            (ByVal lngRootKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long
 
  Private Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _
            (ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, _
             lpType As Long, lpData As Any, lpcbData As Long) As Long
 
  Private Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" _
            (ByVal lngRootKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, _
             ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long

Private Const HKEY_LOCAL_MACHINE As Long = &H80000002
Private Const REG_DWORD As Long = 4

Private Sub Form_Load()
Dim lngKeyRoot As Long
Dim strKeyPath As String
Dim strSubKey As String
Dim lngKeyHandle As Long
Dim lngReturn As Long
Dim lngValue As Long
 
 lngKeyRoot = HKEY_LOCAL_MACHINE
 strKeyPath = "Software\Policies\Microsoft\Windows\WindowsUpdate\AU"
 strSubKey = "NoAutoUpdate"
 lngKeyHandle = 0
  If DoesKeyExist(lngKeyRoot, strKeyPath) Then
    lngReturn = RegOpenKey(lngKeyRoot, strKeyPath, lngKeyHandle)
    lngReturn = RegQueryValueEx(lngKeyHandle, strSubKey, 0&, REG_DWORD, lngValue, 4&)
    If lngValue = 0 Then ' its enabled
        ' do whatever you want
        GoTo CloseReg:
    Else
        ' turn it on
        lngReturn = RegSetValueEx(lngKeyHandle, strSubKey, 0&, REG_DWORD, 0, 4&)
    End If
 Else
    ' create a key and enable AU
    lngReturn = RegCreateKey(lngKeyRoot, strKeyPath, lngKeyHandle)
   
    'set a value
    lngReturn = RegSetValueEx(lngKeyHandle, strSubKey, 0&, REG_DWORD, 1, 4&)
 End If
 
 'set up Auto Update Configuration Keys e.g.AUOPTIONS
  strSubKey = "AUOptions"
  lngReturn = RegSetValueEx(lngKeyHandle, strSubKey, 0&, REG_DWORD, 2, 4&)
 
 
CloseReg:
 'close the registry
 lngReturn = RegCloseKey(lngKeyHandle)
End Sub


Private Function DoesKeyExist(ByVal KeyRoot As Long, ByVal KeyPath As String) As Boolean
  Dim lngKey As Long
  Dim blnKeyExists As Boolean
  Dim lngReturn As Long
 
  lngReturn = RegOpenKey(KeyRoot, KeyPath, lngKey)
  If lngKey = 0 Then 'key does not exist
      DoesKeyExist = False
  Else
      DoesKeyExist = True
  End If
 
   
End Function

0
 

Author Comment

by:apm825
Comment Utility
I wanted to award avi247 some of the points since I may have to use straight win32 api instead of WSH in case the client does not have it installed or enabled.

Thank you very much. Both answers were great and will help me very much!!

Now my problem is the registry keys that article was talking about isn't exactly what I needed. If you're interested, I've created another question under Window OS: http://www.experts-exchange.com/Operating_Systems/WinXP/Q_21147297.html - worth 500pts
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

7 Experts available now in Live!

Get 1:1 Help Now