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
Solved

Running VB program at startup and shut down automatically

Posted on 2004-09-16
13
182 Views
Last Modified: 2013-12-25
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.
0
Comment
Question by:kvnagaraj2000
13 Comments
 
LVL 8

Assisted Solution

by:mladenovicz
mladenovicz earned 300 total points
ID: 12072822
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_SHUTDOWN = 1
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
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12072833
Ooops. This
ret& = ExitWindowsEx(EWX_FORCE Or SHUTDOWN, 0)
should be
 ret& = ExitWindowsEx(EWX_FORCE Or EWX_SHUTDOWN, 0)
0
 
LVL 8

Accepted Solution

by:
mladenovicz earned 300 total points
ID: 12072880
To run app at the startup, copy shortcut to your app to the Startup folder in Start menu. Or you can write to registry

'module:

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.
Public Type SECURITY_ATTRIBUTES
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




'code

On Error Resume Next
Dim hregkey As Long
Dim secattr As SECURITY_ATTRIBUTES
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
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)
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
LVL 51

Assisted Solution

by:Ryan Chong
Ryan Chong earned 100 total points
ID: 12073359
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"
    Else
        MsgBox "This is within 1 hour"
    End If
End Sub

cheers
0
 

Author Comment

by:kvnagaraj2000
ID: 12074082
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?
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12074155
Check ShowInTaskbar property of form
0
 

Expert Comment

by:klm-soft
ID: 12076313
Hi,

Also you can do that:

App.TaskVisible = False

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

Michael
0
 
LVL 9

Assisted Solution

by:Dang123
Dang123 earned 100 total points
ID: 12077695
kvnagaraj2000,
    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
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/Q176/0/85.asp&NoWebContent=1

Add Application to System Tray and Respond to Mouse Events
http://www.thescarms.com/vbasic/systemtray.asp

Using the System Tray
http://www.geocities.com/siliconvalley/campus/9449/sys_tray.htm

Dang123
0
 

Author Comment

by:kvnagaraj2000
ID: 12100234
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?
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12100289
What error do you get? What line does produce error?
0
 

Author Comment

by:kvnagaraj2000
ID: 12109825
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.  
0
 
LVL 8

Expert Comment

by:mladenovicz
ID: 12110179
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.
0
 
LVL 26

Expert Comment

by:Rejojohny
ID: 12154803
>>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 ?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

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…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
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…

861 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