Solved

Running VB program at startup and shut down automatically

Posted on 2004-09-16
13
175 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
 
LVL 49

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

706 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

18 Experts available now in Live!

Get 1:1 Help Now