1,105,226 Community Members

Code for restart/shut down PC

Member Avatar
arasten
Newbie Poster
17 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I made a program that makes someone think his desktop cleaned up, its just for fun and make me learn a littel bit of codes because i'm still a beginner in programming i'm 16 years old in high school grade 11 so i wanna learn more codes because at school the teacher doesn't give us lots of codes because i'm in IT major so in grade 10 we took A+ and grade 11 we are taking programming, DB, and in grade 12 we'll study networking i know that 3 years aren't enough, but we are not studying the whole major because IT is a big major so every year we study something else to decide which major are we going to study in college and i liked the programming so i wanna learn more to prove my self...anyways here is the program download it and take a look on it. http://www.mediafire.com/?autjb3fznrg

please help me with the restart and shut down PC code and if you can teach me a code that disables the program from task manager that would be nice ^_^

and oh yeah i want the code to be after the msgbox i.e when the you click on start it shows you a msgbox and once you click on "ok" the computer restarts or shuts down...

JUST TO AWARE YOU, YOU WONT BE ABLE TO CLOSE THE PROGRAM BY RIGHT CLICK ON IT AND CLOSE OR CLICKING ON "x" ON THE TOP OF THE FORM...YOU'LL HAVE TO USE TASK MANAGER TO END TASK !

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
0
 

What OS are u using? XP, Vista or before?

Member Avatar
arasten
Newbie Poster
17 posts since Jan 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 1 [?]
Skill Endorsements: 0 [?]
 
0
 

I'm using XP

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
4
 

This code Using 'Shell':

Call Shell("Shutdown /s") 'to shutdown

Call Shell("Shutdwon /r") 'to restart

Call Shell("Shutdown /l") 'to logoff

Call Shell("Shutdown /a") 'to Abort

This Code Using 'Managed Code in .NET':

System.Diagnostics.Process.Start (Shutdown", "/s") 'to shutdown

System.Diagnostics.Process.Start("Shutdown", "/r") 'to restart

System.Diagnostics.Process.Start("Shutdown", "/l") 'to logoff

System.Diagnostics.Process.Start("Shutdown", "/a") 'to abort

Choose the one as u like..
The code in my post its a standard command...
You can add the function with more argument like described below:
to shutdown remote machine, add the argument ' /m MyRemoteMachineName' or
to make process delay, add the argument ' /t nn' with 'nn' is number of second eg: Call Shell("Shutdown /s /m upstaircomputer /t 30") 'it will shutdown the upstair computer in 30 second
if you want to know another argument, type shutdown /? in command prompt window

by - risky k (owner)

Member Avatar
Jx_Man
Senior Poster
3,543 posts since Nov 2007
Reputation Points: 987 [?]
Q&As Helped to Solve: 542 [?]
Skill Endorsements: 74 [?]
Featured
 
5
 

hi arasten...
sorry for my post before...its code for vb.net but for vb its not different
this is the code :

Shell "shutdown -r -t 00"   ' for restart
Shell "shutdown -s -t 00"  ' for Shutdown
Shell "shutdown -l -t 00"   ' for log off
Shell "shutdown -a -t 00"  ' for abort

but its only work in xp os..
ok. all for the best

Member Avatar
AndreRet
Industrious Poster
4,492 posts since Jan 2008
Reputation Points: 362 [?]
Q&As Helped to Solve: 499 [?]
Skill Endorsements: 24 [?]
 
1
 

THE FOLLOWING SHOULD CLARIFY ALL.....

If this works for you please reply so.....

Thanks.

'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
Question Answered as of 6 Years Ago by Jx_Man and AndreRet
Member Avatar
jhaymayclow
Newbie Poster
5 posts since Jun 2008
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This code Using 'Shell':

Call Shell("Shutdown /s") 'to shutdown

Call Shell("Shutdwon /r") 'to restart

Call Shell("Shutdown /l") 'to logoff

Call Shell("Shutdown /a") 'to Abort

This Code Using 'Managed Code in .NET':

System.Diagnostics.Process.Start (Shutdown", "/s") 'to shutdown

System.Diagnostics.Process.Start("Shutdown", "/r") 'to restart

System.Diagnostics.Process.Start("Shutdown", "/l") 'to logoff

System.Diagnostics.Process.Start("Shutdown", "/a") 'to abort

Choose the one as u like..
The code in my post its a standard command...
You can add the function with more argument like described below:
to shutdown remote machine, add the argument ' /m MyRemoteMachineName' or
to make process delay, add the argument ' /t nn' with 'nn' is number of second eg: Call Shell("Shutdown /s /m upstaircomputer /t 30") 'it will shutdown the upstair computer in 30 second
if you want to know another argument, type shutdown /? in command prompt window

by - risky k (owner)

(shell("shutdown -m \\PC04 -r"))

I type this code in VB2008 but nothing happen it just open a command promt look alike.
PC04 has not been shutted down .pls help me i wanna try this program.

Member Avatar
jin100
Newbie Poster
1 post since Oct 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

In Timer Event Make Time Interval just 1 and call any big .EXE file with shell it made user to restart his pc

Member Avatar
jbobby87
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Is there code to cancel it once it starts? maybe by using cmd?

Member Avatar
Matigo
Light Poster
44 posts since Jul 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Hi there , Just about to finish the app but i was wondering if you could help me with this please
I'm using the managed Codes and what i want to do is , i want to make a window or an alarm appear for example 15 seconds before let's say it will shutdown
could you help me please ?
thanks

mjkhonline
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
Unverified Member
 
0
 

how to force shut down??

Member Avatar
pritaeas
mod_pritaeas
11,285 posts since Jul 2006
Reputation Points: 1,420 [?]
Q&As Helped to Solve: 1,829 [?]
Skill Endorsements: 154 [?]
Moderator
Featured
Sponsor
 
0
 

how to force shut down??

The code is there.

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article