Okay, so here's the challenge. I want a continously running program that will automatically close all running programs, log off the user account and shut down the computer when the batterly life of my laptop reaches a certain amount. First of all, can that be done with visual basic and second of all, how would it be done?

Recommended Answers

All 8 Replies

Does nobod know the answer to this?

Hmm..... If I know the event subroutine for a battery drain status. I would put it here. But I would put some code without the subroutine encasing it. and the shutdown command automatically logs off every user in the computer. And as for a app with startup, I'd need to find a way to make the program put itself on startup.

'shutting down....

Private Sub 
Shell "shutdown -s -f -t 00", vbWindowMinimized
End Sub

Okay, I'll try that out for now.

You need to use the SysInfo Control. It has properties for ACStatuse, BatteryFullTime, BatteryLifePercent, BatteryLifeTime, and BatteryStatus. It will also tell you when the power status changes, among many other things.

..good day...

im patrick, an I.T student from the other part of the world (Philippines)

i have a project on one of my subject..

i need to make a simple system shut doen program out of visual basic.

can anyone help me?? just a simple program..

thank you in advance..

@ Patrick

Private Sub Command1_Click()
Shell "Shutdown.exe -s", vbMaximizeFocus
shell "shutdown -s -f -t 1"
End Sub

@Topic
Still looking into it...

Private Sub Command1_Click()
Shell "Shutdown.exe -s", vbMaximizeFocus
shell "shutdown -s -f -t 1"
End Sub

i dont think ur right in ur code but i suggest to use this one
Private Sub Command1_mousemove()
Shell "Shutdown.exe -s", vbMaximizeFocus
shell "shutdown -s -f -t 1"
End Sub

'5 command buttons:
'cmdRestart; cmdLogOff; cmdForceLogOff; cmdShutdown; cmdForceShutdown

Option Explicit
Private Const EWX_LogOff As Long = 0
Private Const EWX_SHUTDOWN As Long = 1
Private Const EWX_REBOOT As Long = 2
Private Const EWX_FORCE As Long = 4
Private Const EWX_POWEROFF As Long = 8


Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long

Private Type LUID
UsedPart As Long
IgnoredForNowHigh32BitPart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
TheLuid As LUID
Attributes As Long
End Type
Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
TheLuid As LUID
Attributes As Long
End Type

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function OpenProcessToken Lib "advapi32" _
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long


Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
NewState As TOKEN_PRIVILEGES, _
ByVal BufferLength As Long, _
PreviousState As TOKEN_PRIVILEGES, _
ReturnLength As Long) As Long

Private Declare Sub SetLastError Lib "kernel32" _
(ByVal dwErrCode As Long)

Private Sub AdjustToken()

Const TOKEN_ADJUST_PRIVILEGES = &H20
Const TOKEN_QUERY = &H8
Const SE_PRIVILEGE_ENABLED = &H2

Dim hdlProcessHandle As Long
Dim hdlTokenHandle As Long
Dim tmpLuid As LUID
Dim tkp As TOKEN_PRIVILEGES
Dim tkpNewButIgnored As TOKEN_PRIVILEGES
Dim lBufferNeeded As Long

SetLastError 0

hdlProcessHandle = GetCurrentProcess()

OpenProcessToken hdlProcessHandle, _
(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle

LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid

tkp.PrivilegeCount = 1
tkp.TheLuid = tmpLuid
tkp.Attributes = SE_PRIVILEGE_ENABLED


AdjustTokenPrivileges hdlTokenHandle, _
False, _
tkp, _
Len(tkpNewButIgnored), _
tkpNewButIgnored, _
lBufferNeeded
End Sub

Private Sub cmdForceShutdown_Click()
AdjustToken
ExitWindowsEx (EWX_SHUTDOWN Or EWX_FORCE), &HFFFF
End Sub

Private Sub cmdLogoff_Click()
ExitWindowsEx (EWX_LogOff), &HFFFF
End Sub

Private Sub cmdForceLogoff_Click()
ExitWindowsEx (EWX_LogOff Or EWX_FORCE), &HFFFF
End Sub

Private Sub cmdRestart_Click()
AdjustToken
ExitWindowsEx (EWX_REBOOT), &HFFFF
End Sub

Private Sub cmdShutdown_Click()
AdjustToken
ExitWindowsEx (EWX_SHUTDOWN), &HFFFF
End Sub

Private Sub Form_Load()

End Sub
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.