3

Shutting down a pc in vb.net is easy:

Process.Start("shutdown", "-s -t 00")

unless the user has locked the pc in which case the above fails.

How do I get around this in vb.net? How do I shutdown a locked PC?

The program will be running locally.

Altycoder
  • 270
  • 3
  • 15

6 Answers6

2
System.Diagnostics.Process.Start("shutdown", "-s -f -t 00")

This will force a shutdown In 00ms silently. The code you have to invoke each process is redundant, use the code above. Just do a System.Imports.IO at the top and you are good to go.

Moon Cheesez
  • 2,489
  • 3
  • 24
  • 38
Chaosis
  • 21
  • 2
  • So you propose, that the force flag suffices, ok maybe have not such a system at hand, but could you provide a vlid code snippet instead of stating "Just do a System.Imports.IO at the top" and maybe shortly describe in other words what you mean ´with "pile of code above" as the qnaswers change ordering dynamically and it might be not so friendly received by those having provided multiple lines of code. Thanks. – Dilettant Jun 05 '16 at 07:33
1

You could P/Invoke ExitWindowsEx

There is an example in C# there, but I'm sure you can convert it.

Preet Sangha
  • 64,563
  • 18
  • 145
  • 216
  • Wow, there must be a simpler way surely! – Altycoder Sep 13 '10 at 13:10
  • It's pretty simple - adjust the process level token, and then call exit windows. All it does is give the program the right to shut down. you can give that to the process dynamically. The example is a neat little class you can use. – Preet Sangha Sep 13 '10 at 13:22
1

I think you're looking for the '-f' flag to force a shutdown.

Quote from a MS KB article: When the computer is locked, you can shut down the computer, if you run the Shutdown.exe command together with the -f option.

Hans Olsson
  • 54,199
  • 15
  • 94
  • 116
  • The -f flag is definitely not the way to go as it's designed to force all programs to close ungracefully so on reboot there's likely to be problems. For example on my PC it causes issues withe MS Security Essentials service on reboot. – Altycoder Sep 13 '10 at 13:02
1

For posterity:

Dim ms As ManagementScope = New ManagementScope("\\LocalHost")
    ms.Options.EnablePrivileges = True

    Dim oq As ObjectQuery = New ObjectQuery("SELECT * FROM Win32_OperatingSystem")
    Dim query1 As ManagementObjectSearcher = New ManagementObjectSearcher(ms, oq)
    Dim queryCollection1 As ManagementObjectCollection = query1.Get()

    For Each mo As ManagementObject In queryCollection1
        Dim ss As String() = {"5"}
        mo.InvokeMethod("Win32Shutdown", ss)
    Next

Google "Win32Shutdown" for more details of the flags available (ss above). 5 is a forced shutdown for when the pc is locked but it's more graceful than shutdown /f and doesn't appear to cause any problems with programs or services on restart.

Altycoder
  • 270
  • 3
  • 15
  • PS: Please mark this as "accepted solution" (using the check mark next to your answer), so that this question no longer shows up as "unanswered". – Heinzi Sep 14 '10 at 13:28
0

Have a look at this article on CodeProject which illustrates forcing a computer to shutdown remotely to give you an idea on how to do it.

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
0

Using the System.Management namespace is more elegant than starting an external tool. Here is a code example in C#, which should be fairly easy to convert:

http://www.dreamincode.net/forums/topic/33948-how-to-shut-down-your-computer-in-c%23/

Heinzi
  • 167,459
  • 57
  • 363
  • 519
  • Thanks for the pointer to System.Management. Found this link which helps: http://bytes.com/topic/visual-basic-net/answers/356576-how-shut-down-computer-using-vb-net – Altycoder Sep 13 '10 at 13:24