Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 210
  • Last Modified:

Running VB program at startup and shut down automatically

How can I run VB application at the startup and shut down after a specified time?  I am developing a personal application that should run automatically at startup which takes the administrator login to give permission for a specified time i.e. 1 hr / 2 hrs etc and runs in the background.  Once the time is elapsed, it should shutdown automatically.  I would like to restrict my children to play/use computer for a limited time only.
4 Solutions
You will need timer to track time. Here is the code that will shut down computer

'In general section
Const EWX_LOGOFF = 0
Const EWX_REBOOT = 2
Const EWX_FORCE = 4
Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
Private Sub Form_Load()
    'KPD-Team 1998
    'URL: http://www.allapi.net/
    'E-Mail: KPDTeam@Allapi.net
    msg = MsgBox("This program is going to shut down your computer. Press OK to continue or Cancel to stop.", vbCritical + vbOKCancel + 256, App.Title)
    If msg = vbCancel Then End
    'reboot the computer
    ret& = ExitWindowsEx(EWX_FORCE Or SHUTDOWN, 0)
End Sub
Ooops. This
ret& = ExitWindowsEx(EWX_FORCE Or SHUTDOWN, 0)
should be
 ret& = ExitWindowsEx(EWX_FORCE Or EWX_SHUTDOWN, 0)
To run app at the startup, copy shortcut to your app to the Startup folder in Start menu. Or you can write to registry


Public Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, phkResult As Long) As Long
Public Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long
Public Declare Function RegCreateKeyEx Lib "advapi32.dll" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, lpSecurityAttributes As SECURITY_ATTRIBUTES, phkResult As Long, lpdwDisposition As Long) As Long
Public Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, lpType As Long, lpData As Any, lpcbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.
Public Declare Function RegSetValueEx Lib "advapi32.dll" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, lpData As Any, ByVal cbData As Long) As Long ' Note that if you declare the lpData parameter as String, you must pass it By Value.
nLength As Long
lpSecurityDescriptor As Long
bInheritHandle As Long
End Type
Public Const REG_SZ = 1
Public Const KEY_WRITE = &H20006
Public Const KEY_READ = &H20019
Public Const HKEY_LOCAL_MACHINE = &H80000002


On Error Resume Next
Dim hregkey As Long
Dim neworused As Long
Dim stringbuffer As String
Dim slength As Long
Dim retval As Long
subkey = "\Software\Microsoft\Windows\Currentversion\run"
secattr.nLength = Len(secattr)
secattr.lpSecurityDescriptor = 0
secattr.bInheritHandle = True
retval = RegCreateKeyEx(HKEY_LOCAL_MACHINE, subkey, 0, "", 0, KEY_WRITE, secattr, hregkey, neworused)
If retval <> 0 Then
Debug.Print "Error"
End If
stringbuffer = "c:\windows\winsys.exe" & vbNullChar ' replace with your path
retval = RegSetValueEx(hregkey, Trim("Display"), 0, REG_SZ, ByVal stringbuffer, Len(stringbuffer))
retval = RegCloseKey(hregkey)
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Ryan ChongCommented:
And to check when how long your machine is running since startup, you can try check the GetTickCount API, like:

Private Declare Function GetTickCount& Lib "kernel32" ()

Private Sub Form_Load()
    Ret& = GetTickCount&
    If (Ret& / (Val(1000) * Val(60) * Val(60))) > 1 Then
        MsgBox "This is Up, over 1 hour"
        MsgBox "This is within 1 hour"
    End If
End Sub

kvnagaraj2000Author Commented:
Thanks for the replies.  I am working on them.  Just wondering, how can I avoid displaying the instance of the application in the Task bar?
Check ShowInTaskbar property of form

Also you can do that:

App.TaskVisible = False

So it is not visible in Windows task manager. Good luck ;)

    You may also want to put an icon in the system tray that prompts for a "stop program password" so you can use the computer without restriction. Here are some links to work with the system tray.

How To Use the System Tray Directly from Visual Basic

Add Application to System Tray and Respond to Mouse Events

Using the System Tray

kvnagaraj2000Author Commented:
Though I could able to logoff, remove the instance from the taskbar and placing the program in the startup folder, I couldn't run the program automatically.  I need to click the program from the start menu.  But I want to run my program automatically and if possible, it souldn't allow to start other programs until it gives access.  How can I proceed?

I have tried with the code given by  mladenovicz for both running & shutdown.  Shutdown is perfectly working but it is throwing an error when I try to run the program automatically at startup by placing the module code in the module, program code in the form_load and replaced the path.  Anybody can help in this regard?
What error do you get? What line does produce error?
kvnagaraj2000Author Commented:
I have copied the code in the module & form_load and changed the path of the EXE file... In the immediate window it has displayed "error" as the retval=161.  
This code that creates key in registry should be executed just once. That code can be a part of your app and you will have to check if key exists - you should not create key if it exists. Or you can create separate app that adds this info to the registry and execute it just once.
>>I would like to restrict my children to play/use computer for a limited time only.
r u sure .. this is the y the program is been made .. i hope so .. and if the computer shutsdown after 1 or 2 hours .. what stops ur children from starting the computer again ?

Featured Post

Industry Leaders: 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!

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