Hello Experts

When the system user Log off the system , shutdown and so on How to know that in vbcode
Bcoz When user Log on i have vb application that automatically work, but when user logg off , shut down i have write code in application

Please Help me


place a shortcut of your vb application in windows startup folder. it will automatically launch your program whenever you will login to windows.

and for shutdown, there is no coding required. windows already has a logging off utility that logs off each user from the current session before your computer is going to be restarted or shut down and unloads/closes all running processes/programs from memory.

so make sure your program is running. you can ensure that by visiting the process list in windows task manager. now when a user logs off from his/her session windows will automatically unload your program and close it.

do like shouvik suggest if u want your program automatically when windows start.

instead of placing the shortcut if you wish to launch your program automatically though coding then you can try the following snippet :-


Option Explicit

Public Declare Function RegCreateKey Lib "advapi32.dll" Alias "RegCreateKeyA" _
(ByVal hKey As Long, ByVal lpSubKey As String, phkResult As Long) As Long

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

Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long

Public Const REG_SZ = 1
Public Const HKEY_LOCAL_MACHINE = &H80000002

Public Sub IncludeAtStartup(hKey As Long, strPath As String, strValue As String, _
strdata As String)

Dim keyhand As Long
Dim r As Long

r = RegCreateKey(hKey, strPath, keyhand)
r = RegSetValueEx(keyhand, strValue, 0, REG_SZ, ByVal strdata, Len(strdata))
r = RegCloseKey(keyhand)
End Sub


Dim strname as String

strname = App.EXEName & ".Exe"
            Call IncludeAtStartup(HKEY_LOCAL_MACHINE, "SOFTWARE\Microsoft\Windows\CurrentVersion\Run", _
            strname, App.Path & "\" & App.EXEName & ".exe")

hope this helps.


also u can write code to put your application on startup folder. so u no need to put your program manually on startup.
see this following code :

Private Sub GetStartUp()
    Dim mShell
    Dim txtFStartUp As String
    Set mShell = CreateObject("WScript.Shell")
    txtFStartUp = mShell.SpecialFolders("Startup")
    FileCopy App.Path & "\" & App.EXEName & _
    ".exe", txtFStartUp & "\YourAppName.exe"
End Sub

If you need to write custom code during the shutdown of the application and need to check to see if the user is logging off or if the machine is shutting down, use the forms QueryUnload event. Form_QueryUnload(cancel As Integer, unloadmode As Integer) UnloadMode will identify HOW the request to shutdown was received. You can set Cancel to True to halt the process, but in the event of a system shutdown, Windows may terminate the process.

The list of UnloadMode constants can be found on the Microsoft MSDN site.

Dear Experts,
I have developed the one vb project in my machine.It's Working fine. my problem is My form(alert) has been disaplayed when i run the exe but it was showing in background so i couldn't see in my form(alert) in foreground bcoz i have working another application at the time.hence how to display the form in foreground in runtime when i using the another apllication.

Thank you,

your intension is not clear from your explanation....plz explain it in more detail...