Shut down the computer

Windows XP
I am trying to shut down the computer after saving files and have tried to use this code which was supplied on another thread. The code works fine for loging off but I can't get it to shut down the computer. Am I missing something?


Module Module1
Declare Function ExitWindowsEx Lib "user32" Alias "ExitWindowsEx" (ByVal uFlags As _
Integer, ByVal dwReserved As Integer) As Integer

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

Public Sub ExitFromWindows(ByVal Choice As Choices)
ExitWindowsEx(Choice, 0)
End Sub

End Module

Thanks Malcolm Mack

Comments

  • Well you might not have shutdown priviledges.

    Or else, try calling the API twice in a row. I've seen it used occasionally.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • I am the computer administrator, is there something else I need to do to have shut down privalages?

    Malcolm
  • Ahhh... the heavenly archives of MSDN

    http://msdn2.microsoft.com/en-us/library/aa376868.aspx

    In particular, check the shutdown constants, and the remarks section.
    Mostly, the AdjustTokenPriveledge part (also: http://msdn2.microsoft.com/en-us/library/aa375202.aspx)

    EDIT: Complete example in C: http://msdn2.microsoft.com/en-us/library/aa376871.aspx

    Now note that this code will only work for XP and above.
    If you're also planning on using it for 98 and/or NT/2000 then you'll probably have to get a bit m
  • Thanks for the Link but it doen't help me much because it is C.
    I have tried to work it out but there also appears to be calls to functions in a library I haven't included.

    I do have microsoft.win32.

    Any VB code that does the same job as the C and any required libraries would be helpfull.

    regards Malcolm Mack

    : Ahhh... the heavenly archives of MSDN
    :
    : http://msdn2.microsoft.com/en-us/library/aa376868.aspx
    :
    : In particular, check the shutdown constants, and the remarks section.
    : Mostly, the AdjustTokenPriveledge part (also:
    : http://msdn2.microsoft.com/en-us/library/aa375202.aspx)
    :
    : EDIT: Complete example in C:
    : http://msdn2.microsoft.com/en-us/library/aa376871.aspx
    :
    : Now note that this code will only work for XP and above.
    : If you're also planning on using it for 98 and/or NT/2000 then
    : you'll probably have to get a bit m
  • I have a complete .NET control at my website under the Visual Basic.NET - Controls page that will restart, shutdown, logoff, initiate shutdown, and more that is very easy to use. Simply add a reference to the compiled class library and create a instance and your good to go.

    You could also use shell code to manipulate the Windows Shutdown Process to shutdown the computer. Example:

    Process.Start("shutdown -s -f -t 0") 'Will Shutdown the computer.

    Process.Start("shutdown -r -f -t 0") 'Will Reboot the computer.

    Process.Start("shutdown -l -f -t 0") 'Will Logoff the user.


    Hope this helps :)


    Jason



    : Windows XP
    : I am trying to shut down the computer after saving files and have
    : tried to use this code which was supplied on another thread. The
    : code works fine for loging off but I can't get it to shut down the
    : computer. Am I missing something?
    :
    :
    : Module Module1
    : Declare Function ExitWindowsEx Lib "user32" Alias
    : "ExitWindowsEx" (ByVal uFlags As _
    : Integer, ByVal dwReserved As Integer) As Integer
    :
    : Public Enum Choices
    : Logoff = 0
    : Shutdown = 1
    : Reboot = 2
    : ForceLogOff = 4
    : ForceReboot = 6
    : PowerDown = 8
    : ForcePowerDown = 12
    : End Enum
    :
    : Public Sub ExitFromWindows(ByVal Choice As Choices)
    : ExitWindowsEx(Choice, 0)
    : End Sub
    :
    : End Module
    :
    : Thanks Malcolm Mack

    --------------------------------------------

    Visual Basic Code Source (New Design!) - http://www.vbcodesource.org

    Visual Basic Code Source - http://www.vbcodesource.com
  • Thanks Jason that worked just fine.

    : I have a complete .NET control at my website under the Visual
    : Basic.NET - Controls page that will restart, shutdown, logoff,
    : initiate shutdown, and more that is very easy to use. Simply add a
    : reference to the compiled class library and create a instance and
    : your good to go.
    :
    : You could also use shell code to manipulate the Windows Shutdown
    : Process to shutdown the computer. Example:
    :
    : Process.Start("shutdown -s -f -t 0") 'Will Shutdown the computer.
    :
    : Process.Start("shutdown -r -f -t 0") 'Will Reboot the computer.
    :
    : Process.Start("shutdown -l -f -t 0") 'Will Logoff the user.
    :
    :
    : Hope this helps :)
    :
    :
    : Jason
    :
    :
    :
    : : Windows XP
    : : I am trying to shut down the computer after saving files and have
    : : tried to use this code which was supplied on another thread. The
    : : code works fine for loging off but I can't get it to shut down the
    : : computer. Am I missing something?
    : :
    : :
    : : Module Module1
    : : Declare Function ExitWindowsEx Lib "user32" Alias
    : : "ExitWindowsEx" (ByVal uFlags As _
    : : Integer, ByVal dwReserved As Integer) As Integer
    : :
    : : Public Enum Choices
    : : Logoff = 0
    : : Shutdown = 1
    : : Reboot = 2
    : : ForceLogOff = 4
    : : ForceReboot = 6
    : : PowerDown = 8
    : : ForcePowerDown = 12
    : : End Enum
    : :
    : : Public Sub ExitFromWindows(ByVal Choice As Choices)
    : : ExitWindowsEx(Choice, 0)
    : : End Sub
    : :
    : : End Module
    : :
    : : Thanks Malcolm Mack
    :
    : --------------------------------------------
    :
    : Visual Basic Code Source (New Design!) - http://www.vbcodesource.org
    :
    : Visual Basic Code Source - http://www.vbcodesource.com
  • : I have a complete .NET control at my website under the Visual
    : Basic.NET - Controls page that will restart, shutdown, logoff,
    : initiate shutdown, and more that is very easy to use. Simply add a
    : reference to the compiled class library and create a instance and
    : your good to go.

    I took a look at the control. Nice code!
    I almost had a working code, but the AdjustTokenPrivelege function kept giving me errors and I had not yet succeeded in fixing it.

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : : I have a complete .NET control at my website under the Visual
    : : Basic.NET - Controls page that will restart, shutdown, logoff,
    : : initiate shutdown, and more that is very easy to use. Simply add a
    : : reference to the compiled class library and create a instance and
    : : your good to go.
    :
    : I took a look at the control. Nice code!
    : I almost had a working code, but the AdjustTokenPrivelege function
    : kept giving me errors and I had not yet succeeded in fixing it.
    :
    : Best Regards,
    : Richard
    :
    : The way I see it... Well, it's all pretty blurry

    These Codes will work only on XP.
    :
    : Process.Start("shutdown -s -f -t 0") 'Will Shutdown the computer.
    :
    : Process.Start("shutdown -r -f -t 0") 'Will Reboot the computer.
    :
    : Process.Start("shutdown -l -f -t 0") 'Will Log off the user.
    :
    if u used this command, your program will restart or do what ever by sending it to DOS Prompt try this in DOS Mode (Windows)
    shutdown -s -f -t 0
    to understand about is try this on dos prompt.
    shutdown /?
    --------------------------------------------------------------
    if u use
    Shutdown -a
    shutdown or restart process will abort.

    the way you are going to restart, is not the best practice.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories