Results 1 to 9 of 9

Thread: How to restart or turn off the computer?

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2002
    Location
    Toronto, Canada
    Posts
    50

    Question How to restart or turn off the computer?

    Hi,

    I would like to know how to restart or turn off the computer from the VB application.

    Thanks,
    Andrew
    Best regards, Andrew

  2. #2
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    either vbforums' server farts too much or you press the post button too many times
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  3. #3
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    VB Code:
    1. ' For logging off - shutdown - restarting
    2. Private Const EWX_LOGOFF = 0
    3. Private Const EWX_SHUTDOWN = 1
    4. Private Const EWX_REBOOT = 2
    5. Private Const EWX_FORCE = 4
    6. Private Const TOKEN_ADJUST_PRIVILEGES = &H20
    7. Private Const TOKEN_QUERY = &H8
    8. Private Const SE_PRIVILEGE_ENABLED = &H2
    9. Private Const ANYSIZE_ARRAY = 1
    10. Private Const VER_PLATFORM_WIN32_NT = 2
    11. Type OSVERSIONINFO
    12.     dwOSVersionInfoSize As Long
    13.     dwMajorVersion As Long
    14.     dwMinorVersion As Long
    15.     dwBuildNumber As Long
    16.     dwPlatformId As Long
    17.     szCSDVersion As String * 128
    18. End Type
    19. Type LUID
    20.     LowPart As Long
    21.     HighPart As Long
    22. End Type
    23. Type LUID_AND_ATTRIBUTES
    24.     pLuid As LUID
    25.     Attributes As Long
    26. End Type
    27. Type TOKEN_PRIVILEGES
    28.     PrivilegeCount As Long
    29.     Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
    30. End Type
    31. Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
    32. Private Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
    33. Private Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
    34. 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
    35. Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long
    36. Private Declare Function GetVersionEx Lib "kernel32" Alias "GetVersionExA" (ByRef lpVersionInformation As OSVERSIONINFO) As Long
    37. 'Detect if the program is running under Windows NT
    38. Public Function IsWinNT() As Boolean
    39.     Dim myOS As OSVERSIONINFO
    40.     myOS.dwOSVersionInfoSize = Len(myOS)
    41.     GetVersionEx myOS
    42.     IsWinNT = (myOS.dwPlatformId = VER_PLATFORM_WIN32_NT)
    43. End Function
    44. 'set the shut down privilege for the current application
    45. Private Sub EnableShutDown()
    46.     Dim hProc As Long
    47.     Dim hToken As Long
    48.     Dim mLUID As LUID
    49.     Dim mPriv As TOKEN_PRIVILEGES
    50.     Dim mNewPriv As TOKEN_PRIVILEGES
    51.     hProc = GetCurrentProcess()
    52.     OpenProcessToken hProc, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hToken
    53.     LookupPrivilegeValue "", "SeShutdownPrivilege", mLUID
    54.     mPriv.PrivilegeCount = 1
    55.     mPriv.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
    56.     mPriv.Privileges(0).pLuid = mLUID
    57.     ' enable shutdown privilege for the current application
    58.     AdjustTokenPrivileges hToken, False, mPriv, 4 + (12 * mPriv.PrivilegeCount), mNewPriv, 4 + (12 * mNewPriv.PrivilegeCount)
    59. End Sub
    60. ' Shut Down NT
    61. Public Sub ShutDownNT(Force As Boolean)
    62.     Dim ret As Long
    63.     Dim Flags As Long
    64.     Flags = EWX_SHUTDOWN
    65.     If Force Then Flags = Flags + EWX_FORCE
    66.     If IsWinNT Then EnableShutDown
    67.     ExitWindowsEx Flags, 0
    68. End Sub
    69. 'Restart NT
    70. Public Sub RebootNT(Force As Boolean)
    71.     Dim ret As Long
    72.     Dim Flags As Long
    73.     Flags = EWX_REBOOT
    74.     If Force Then Flags = Flags + EWX_FORCE
    75.     If IsWinNT Then EnableShutDown
    76.     ExitWindowsEx Flags, 0
    77. End Sub
    78. 'Log off the current user
    79. Public Sub LogOffNT(Force As Boolean)
    80.     Dim ret As Long
    81.     Dim Flags As Long
    82.     Flags = EWX_LOGOFF
    83.     If Force Then Flags = Flags + EWX_FORCE
    84.     ExitWindowsEx Flags, 0
    85. End Sub

    Umm, put this code in a module
    HTH
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  4. #4
    PowerPoster Pc_Madness's Avatar
    Join Date
    Dec 2001
    Location
    Melbourne, Australia
    Posts
    2,765
    Don't post this thread 4 times!
    Don't Rate my posts.

  5. #5
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs up Try this...

    Private Declare Function SHShutDownDialog Lib "shell32" _
    Alias "#60" (ByVal something As Long) As Long

    Private Sub Command1_Click()
    SHShutDownDialog 0
    End Sub

  6. #6
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428
    oh well, the code that I posted (dunno who did I steal if from ) works good, because it work both for win 9x and NT and it also does all those force crapp.....
    I dont think it's a good idea to show the shutdown dialog
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  7. #7
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Hmmm...

    I think I had the code you post it here b4 but

    first it's too long
    second it's not compitable to all OS from my experience
    third my boss wants every thing as windows standard

    Cheers...

  8. #8
    l33t! MrPolite's Avatar
    Join Date
    Sep 2001
    Posts
    4,428

    Re: Hmmm...

    Originally posted by wrack
    I think I had the code you post it here b4 but

    first it's too long
    second it's not compitable to all OS from my experience
    third my boss wants every thing as windows standard

    Cheers...
    The good thing is that it IS compatible to all OS ( I THINK )
    rate my posts if they help ya!
    Extract thumbnail without reading the whole image file: (C# - VB)
    Apply texture to bitmaps: (C# - VB)
    Extended console library: (VB)
    Save JPEG with a certain quality (image compression): (C# - VB )
    VB.NET to C# conversion tips!!

  9. #9
    Hyperactive Member gamezfreakuk's Avatar
    Join Date
    Mar 2002
    Location
    Nottingham, UK
    Posts
    302
    Err, there is a simpler way, but it dont work on al Operating Systems only win NT, 95 and 98 i think

    In a module:

    Code:
    Public Const EWX_LOGOFF = 0 
    Public Const EWX_SHUTDOWN = 1 
    Public Const EWX_REBOOT = 2 
    Public Const EWX_FORCE = 4
    Declare Function ExitWindowsEx Lib "user32" Alias _
    "ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved _
    As Long) As Long
    and put this wherever you wish to execute the code (for a restart)

    Code:
    t& = ExitWindowsEx(EWX_FORCE Or EWX_REBOOT, 0)
    for a shutdown use this

    Code:
    t& = ExitWindowsEx(EWX_FORCE Or EWX_SHUTDOWN, 1)
    there u have it!
    Gamezfreak
    Visual Basic 6 Enterprise Edition
    Borland C++ Builder 6 Enterprise Edition

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width