[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

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

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
apm825
Asked:
apm825
  • 2
  • 2
2 Solutions
 
David LeeCommented:
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
 
apm825Author Commented:
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
 
David LeeCommented:
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
 
avi247Commented:
'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
 
apm825Author Commented:
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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now