Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Reboot Dialog 500 bones for a code sample

Posted on 2006-05-24
12
Medium Priority
?
311 Views
Last Modified: 2006-11-18
To all my expert friends,

I need a code snippet for vb.net that reboots the client machine.

Mydasx
0
Comment
Question by:mydasx
  • 4
  • 4
  • 2
  • +1
12 Comments
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16754206
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16754313
0
 
LVL 5

Author Comment

by:mydasx
ID: 16754947
neither of these are very straight forward.  For 500 bones i'm looking for a code snippet that calls reboot, i dont want a form, i dont want to parse through a bunch of other code.  What's the api call for reboot (non debug) what considerations do i need to make for different windows versions... etc.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 10

Expert Comment

by:bchoor
ID: 16755095
If you're using Windows XP

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

' to reboot
System.Diagnostics.Process.Start("Shutdown", "-r")

' to force shutdown
System.Diagnostics.Process.Start("Shutdown", "-s -f")

to get more arguments for the windows shutdown command, in command prompt type: shutdown /?

HTH
~BC
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16755145
this is the code to reboot

        Dim t As Single
        Dim objWMIService, objComputer As Object
        'Now get some privileges
        objWMIService = GetObject("Winmgmts:{impersonationLevel=impersonate,(Debug,Shutdown)}")
        For Each objComputer In objWMIService.InstancesOf("Win32_OperatingSystem")
            t = objComputer.Win32Shutdown(2 + 4, 0)
            If t <> 0 Then
                MsgBox("Error occurred!!!")
            Else
            End If
        Next
0
 
LVL 10

Expert Comment

by:bchoor
ID: 16755186
0
 
LVL 5

Author Comment

by:mydasx
ID: 16755224
David:  I get a message that option strict doesnt like late binding how do i work around that...  i cannot turn option strict off...
0
 
LVL 7

Expert Comment

by:davidrichardson
ID: 16755465
0
 
LVL 10

Accepted Solution

by:
heintalus earned 2000 total points
ID: 16755494
David, thats an old way of using WMI, in DotNet you should be using the System.Management namespace classes.

Here's an example, taken from my FAQ on WMI at VBCity  
http://www.vbcity.com/forums/faq.asp?fid=30&cat=General&#TID90733

Another useful article on WMI in Dotnet is
http://www.devcity.net/Articles/144/1/article.aspx

Public Enum Win32ShutdownFlag
        [Logoff] = 0
        [Shutdown] = 1
        [Reboot] = 2
        [ForceLogOff] = 4
        [ForceReboot] = 6
        [PowerDown] = 8
        [ForcePowerDown] = 12
End Enum

Private Sub RebootPC(ByVal flg As Win32ShutdownFlag)
        Dim options As New ConnectionOptions
        options.Impersonation = ImpersonationLevel.Impersonate
        options.EnablePrivileges = True
        options.Username = _AdminUser
        options.Password = _AdminPassword
        Dim myScope As New ManagementScope("\\" & _MachineName & "\root\cimv2", options)
        Dim oQuery As New SelectQuery("SELECT * FROM win32_operatingsystem")
        Dim oResults As New ManagementObjectSearcher(myScope, oQuery)
        Dim oItem As ManagementObject
        Dim oProperty As PropertyData
        Dim myParams() As Object = {flg, 0}

        For Each oItem In oResults.Get()
            oItem.InvokeMethod("Win32Shutdown", myParams)
        Next
End Sub

HTH
Andy
0
 
LVL 5

Author Comment

by:mydasx
ID: 16755859
andy,  will this require that i know an admin user and its pass, as this is for a dialog post install of an msi where i am needing reboot after install.  thus i dont know the users credentials.  Windows has built in users for this case right?
0
 
LVL 10

Expert Comment

by:heintalus
ID: 16756025
If it's from an msi then there's a flag in there that you can set to tell the install that a reboot is required & you don't have to do it from code.  It's so long since I've built an msi that I can't remember exactly what it is, but believe it's a variable within the msi that you have to set.

The code I posted above is for rebooting a remote machine where you'd have to send in credentials of an administrator.  If however you were a domain admin trying to reboot a remote machine then you wouldn't need to send in the credentials so could leave out the ConnectionOptions.  The same would apply if you were doing it on the local machine & you had admin rights.

However I suggest you look at the flag within the msi as thats the easiest way of doing it.

HTH
Andy

0
 
LVL 5

Author Comment

by:mydasx
ID: 16756044
thanx dude.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question