?
Solved

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

Posted on 2004-09-27
5
Medium Priority
?
159 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
5 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 12165539
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
ID: 12165788
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 1200 total points
ID: 12165852
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 800 total points
ID: 12165918
'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
ID: 12166083
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…
Suggested Courses
Course of the Month10 days, 23 hours left to enroll

770 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