How Shutdown, Restart, and Log Off the Computer without APIs for VB 6.0 and .NET

Click Star to Rate Post
1 Star2 Stars3 Stars4 Stars5 Stars (33 votes, average: 4.61 out of 5)
 

IF you do not like using Microsoft API calls like ExitWindowsEx, but you still want to Shutdown, Restart, and/or Log Out a computer, you can use some simple shell based code. The Shell method works fine but requires the Windows Shutdown service to be active on the target computer to work. IT usually is running by default on most computers. Also using these codes won’t require your calling application to register with Windows for higher or “special” priviledges like you would if you used the ExitWindowsEx API.

  Update: I went ahead and added the VB 6.0 version of this code after the Visual Basic.NET version. The .NET code works with version Visual Basic 2008, VB 2010, and newer as well.

  Update #2: Thanks to Robbin for pointing out at the Process Class will NOT execute the Shutdown service to perform the commands. I was assuming it would and just added the required flags. So you can just use the same Shell method that VB 6.0 for .NET.


Shell "shutdown -s" 'Shutdown

Shell "shutdown -r" 'Restart

Shell "shutdown -l" 'Log Out

 

25 thoughts on “How Shutdown, Restart, and Log Off the Computer without APIs for VB 6.0 and .NET

  1. robbin

    Its not working on visual basic 9.0 or visual basic 2008 express.
    You have to use:
    shell (“shutdown -s”) Shutdown
    shell (“shutdown -r”) Restart
    shell (“shutdown -l”) Log Out

    Reply
  2. RapsMX

    I hope you don’t mind me posting what I use, it might come in handy;

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    ‘Restart Windows XP
    Shell(“shutdown -r -f -t 0”)
    Shell(“RUNDLL32 SHELL32.DLL,SHExitWindowsEx 1”)
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    ‘Log Off Windows XP
    Shell(“shutdown -l -f -t 0”)
    Shell(“RUNDLL32 SHELL32.DLL,SHExitWindowsEx 1”)
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    ‘Shutdown Windows XP
    Shell(“shutdown -s -f -t 0”)
    Shell(“RUNDLL32 SHELL32.DLL,SHExitWindowsEx 1”)
    End Sub

    Reply
  3. curly

    Shell(“shutdown -s -f -t 0”)
    what’s the use,command or stand for of the
    -f-t 0 ??????
    can someone explain it,..?

    Reply
  4. extra comment

    just a little bit of extra info those of you who are trying to use thi with a C++ software be VERY careful it has been known to backfire sending the corrupted file back to which can wipe your haardrive and make it impossible keep your computer on

    Reply
  5. krischen

    paano po mg log off ung compter n gmit lang ung vb code…gmgwa kc kme ng thisis..n kylangang mkontrol ng server ang client n pg nglapse na ang time ng user automatic n mg lo2g off pls help me…tnx

    Reply
  6. AbdiSan

    or use only System.Diagnostics.Process.Start(“Shutdown”, “/f””/r”) if “/f””/r” doesnt work then only use “/r” which makes a system restar 🙂 easy as that 🙂 bye have fun shutting down PCs

    Reply
  7. Rusty

    Hello. Is it possible to call shutdown, but instead of typing -t 20, have some variable for time, like:
    Shell(“shutdown -s -f -t X?).

    Reply
  8. Jason Post author

    Hi, Rusty, i’m not 100% sure what you mean. If you do want a timed shutdown, there is aother API call you can use. I also wanted to mention that I have a library called: csWindowsShutdown that has various methods of doing a shutdown, logoff, restart, display system must shutdown, must restart message and so on.

    This API call: InitiateSystemShutdownEx will allow you to set the time and reason for the shutdown and shows a Shutdown window counting down the time before it begins. You will need token privileges though. I have a full example using the library at the link below…

    http://www.vbcodesource.info/downloads/csWindowsShutdownExample.zip

    If you want the libraries source code its under the .NET Controls page.

    Jason

    Reply
  9. ouafae bouali

    hi;it’s easy to shuddown my pc using vb2008 but i don’t know how i can shutdown a pc a distence (client/server)that really make me creasy 🙁 may someone can help me plz

    Reply
  10. daniel

    Hello everyone!,
    I just want a little help. How can i shutdown and restart clients PC..Please give me some codes..For example, im using a server form then it is connected to other pc or the client..The server should restart or shutdown the client pc in one by one procedure..Thanks a lot! I hope some of you has an idea to code for it..ASAP

    Reply

Leave a Reply