computer shutdown

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten

    #1

    computer shutdown

    hi

    does someone knows how to shut down you're compueter with vb 6.0?

    thanks Maarten


  • mickey

    #2
    Re: computer shutdown

    On Wed, 13 Oct 2004 09:56:50 +0200, "Maarten" <gurkjaan@hotma il.com>
    you typed some letters in random order:
    [color=blue]
    >hi
    >
    >does someone knows how to shut down you're compueter with vb 6.0?
    >
    >thanks Maarten
    >[/color]
    I'm not shure it works for OS > win98

    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
    Long, ByVal dwReserved As Long) As Long

    Call ExitWindowsEx(1 , 0)

    Groetjenz,

    Mickey
    --
    #### gewoan skrieve su ast ut seist ####

    Comment

    • Auric__

      #3
      Re: computer shutdown

      On Wed, 13 Oct 2004 23:18:03 +0200, mickey <mickeyATgruize lgruis.nl>
      wrote:
      [color=blue]
      >On Wed, 13 Oct 2004 09:56:50 +0200, "Maarten" <gurkjaan@hotma il.com>
      >you typed some letters in random order:
      >[color=green]
      >>hi
      >>
      >>does someone knows how to shut down you're compueter with vb 6.0?
      >>
      >>thanks Maarten
      >>[/color]
      >I'm not shure it works for OS > win98
      >
      >Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
      >Long, ByVal dwReserved As Long) As Long
      >
      >Call ExitWindowsEx(1 , 0)[/color]

      For NT systems it's a bit more complex - you have to give the thread
      permission to shut down, otherwise all you can do is logoff.

      Here's what I use (call quitWindows to use):

      ' found somewhere on the www (can't remember where)
      ' so don't give me credit

      Private Declare Function GetCurrentProce ss Lib "kernel32" () As Long
      Private Declare Function OpenProcessToke n Lib "advapi32" ( _
      ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
      TokenHandle As Long) As Long
      Private Declare Function LookupPrivilege Value Lib "advapi32" _
      Alias "LookupPrivileg eValueA" (ByVal lpSystemName As String, _
      ByVal lpName As String, lpLuid As LUID) As Long
      Private Declare Function AdjustTokenPriv ileges Lib "advapi32" ( _
      ByVal TokenHandle As Long, ByVal DisableAllPrivi leges As Long, _
      NewState As TOKEN_PRIVILEGE S, ByVal BufferLength As Long, _
      PreviousState As TOKEN_PRIVILEGE S, ReturnLength As Long) As Long
      Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
      Long, ByVal dwReserved As Long) As Long
      Private Declare Function GetVersionEx Lib "kernel32" Alias _
      "GetVersion ExA" (ByRef lpVersionInform ation As OSVERSIONINFO) _
      As Long

      Public Const EWX_LOGOFF = 0
      Public Const EWX_SHUTDOWN = 1
      Public Const EWX_REBOOT = 2
      Public Const EWX_FORCE = 4
      Public Const EWX_POWEROFF = 8
      Public Const EWX_FORCEIFHUNG = 16

      Private Const TOKEN_QUERY = 8
      Private Const TOKEN_ADJUST_PR IVILEGES = 32

      Private Const SE_PRIVILEGE_EN ABLED = 2

      Private Const ANYSIZE_ARRAY = 1
      Private Const VER_PLATFORM_WI N32_NT = 2

      Type OSVERSIONINFO
      dwOSVersionInfo Size As Long
      dwMajorVersion As Long
      dwMinorVersion As Long
      dwBuildNumber As Long
      dwPlatformId As Long
      szCSDVersion As String * 128
      End Type

      Public Type LUID
      LowPart As Long
      HighPart As Long
      End Type

      Public Type LUID_AND_ATTRIB UTES
      pLuid As LUID
      Attributes As Long
      End Type

      Public Type TOKEN_PRIVILEGE S
      PrivilegeCount As Long
      Privileges(ANYS IZE_ARRAY) As LUID_AND_ATTRIB UTES
      End Type

      Public Sub quitWindows(whi ch As Long)
      Dim n As Long

      If IsWinNT Then EnableShutDown
      n = ExitWindowsEx(w hich, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
      If n Then
      Dim x As String * 256
      FormatMessage FORMAT_MESSAGE_ FROM_SYSTEM, 0, Err.LastDllErro r, _
      0, x, 256, 0
      Else
      Unload frmBackground
      End If
      End Sub

      Public Function IsWinNT() As Boolean
      ' Detect if the program is running under Windows NT
      Dim myOS As OSVERSIONINFO

      myOS.dwOSVersio nInfoSize = Len(myOS)
      GetVersionEx myOS
      IsWinNT = (myOS.dwPlatfor mId = VER_PLATFORM_WI N32_NT)
      End Function

      Private Sub EnableShutDown( )
      ' Set the shut down privilege for the current application
      Dim hProc As Long, hToken As Long, mLUID As LUID
      Dim mPriv As TOKEN_PRIVILEGE S, mNewPriv As TOKEN_PRIVILEGE S

      hProc = GetCurrentProce ss()
      OpenProcessToke n hProc, TOKEN_ADJUST_PR IVILEGES + TOKEN_QUERY, _
      hToken
      LookupPrivilege Value "", "SeShutdownPriv ilege", mLUID
      mPriv.Privilege Count = 1
      mPriv.Privilege s(0).Attributes = SE_PRIVILEGE_EN ABLED
      mPriv.Privilege s(0).pLuid = mLUID

      ' enable shutdown privilege for the current application
      AdjustTokenPriv ileges hToken, False, mPriv, 4 + (12 * _
      mPriv.Privilege Count), mNewPriv, 4 + (12 * _
      mNewPriv.Privil egeCount)
      End Sub

      --
      auric underscore underscore at hotmail dot com
      *****
      The absence of war does not automatically mean peace.

      Comment

      • Maarten

        #4
        Re: computer shutdown

        thanks for the reply

        Public Sub quitWindows(whi ch As Long)
        Dim n As Long

        If IsWinNT Then EnableShutDown
        n = ExitWindowsEx(w hich, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
        If n Then
        Dim x As String * 256
        FormatMessage FORMAT_MESSAGE_ FROM_SYSTEM, 0, Err.LastDllErro r, _
        0, x, 256, 0
        Else
        Unload frmBackground
        End If
        End Sub

        in this part i get ann error

        FormatMessage sub or function not derfined?

        I'm using a computer with xp to program, but i test and run my software on
        a computer with win 98. wil it work for both?

        thanks Maarten

        "Auric__" <not.my.real@em ail.address> wrote in message
        news:h5erm09c6n b5od0328uum5u5e rvvejb5pn@4ax.c om...[color=blue]
        > On Wed, 13 Oct 2004 23:18:03 +0200, mickey <mickeyATgruize lgruis.nl>
        > wrote:
        >[color=green]
        > >On Wed, 13 Oct 2004 09:56:50 +0200, "Maarten" <gurkjaan@hotma il.com>
        > >you typed some letters in random order:
        > >[color=darkred]
        > >>hi
        > >>
        > >>does someone knows how to shut down you're compueter with vb 6.0?
        > >>
        > >>thanks Maarten
        > >>[/color]
        > >I'm not shure it works for OS > win98
        > >
        > >Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As
        > >Long, ByVal dwReserved As Long) As Long
        > >
        > >Call ExitWindowsEx(1 , 0)[/color]
        >
        > For NT systems it's a bit more complex - you have to give the thread
        > permission to shut down, otherwise all you can do is logoff.
        >
        > Here's what I use (call quitWindows to use):
        >
        > ' found somewhere on the www (can't remember where)
        > ' so don't give me credit
        >
        > Private Declare Function GetCurrentProce ss Lib "kernel32" () As Long
        > Private Declare Function OpenProcessToke n Lib "advapi32" ( _
        > ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
        > TokenHandle As Long) As Long
        > Private Declare Function LookupPrivilege Value Lib "advapi32" _
        > Alias "LookupPrivileg eValueA" (ByVal lpSystemName As String, _
        > ByVal lpName As String, lpLuid As LUID) As Long
        > Private Declare Function AdjustTokenPriv ileges Lib "advapi32" ( _
        > ByVal TokenHandle As Long, ByVal DisableAllPrivi leges As Long, _
        > NewState As TOKEN_PRIVILEGE S, ByVal BufferLength As Long, _
        > PreviousState As TOKEN_PRIVILEGE S, ReturnLength As Long) As Long
        > Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
        > Long, ByVal dwReserved As Long) As Long
        > Private Declare Function GetVersionEx Lib "kernel32" Alias _
        > "GetVersion ExA" (ByRef lpVersionInform ation As OSVERSIONINFO) _
        > As Long
        >
        > Public Const EWX_LOGOFF = 0
        > Public Const EWX_SHUTDOWN = 1
        > Public Const EWX_REBOOT = 2
        > Public Const EWX_FORCE = 4
        > Public Const EWX_POWEROFF = 8
        > Public Const EWX_FORCEIFHUNG = 16
        >
        > Private Const TOKEN_QUERY = 8
        > Private Const TOKEN_ADJUST_PR IVILEGES = 32
        >
        > Private Const SE_PRIVILEGE_EN ABLED = 2
        >
        > Private Const ANYSIZE_ARRAY = 1
        > Private Const VER_PLATFORM_WI N32_NT = 2
        >
        > Type OSVERSIONINFO
        > dwOSVersionInfo Size As Long
        > dwMajorVersion As Long
        > dwMinorVersion As Long
        > dwBuildNumber As Long
        > dwPlatformId As Long
        > szCSDVersion As String * 128
        > End Type
        >
        > Public Type LUID
        > LowPart As Long
        > HighPart As Long
        > End Type
        >
        > Public Type LUID_AND_ATTRIB UTES
        > pLuid As LUID
        > Attributes As Long
        > End Type
        >
        > Public Type TOKEN_PRIVILEGE S
        > PrivilegeCount As Long
        > Privileges(ANYS IZE_ARRAY) As LUID_AND_ATTRIB UTES
        > End Type
        >
        > Public Sub quitWindows(whi ch As Long)
        > Dim n As Long
        >
        > If IsWinNT Then EnableShutDown
        > n = ExitWindowsEx(w hich, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
        > If n Then
        > Dim x As String * 256
        > FormatMessage FORMAT_MESSAGE_ FROM_SYSTEM, 0, Err.LastDllErro r, _
        > 0, x, 256, 0
        > Else
        > Unload frmBackground
        > End If
        > End Sub
        >
        > Public Function IsWinNT() As Boolean
        > ' Detect if the program is running under Windows NT
        > Dim myOS As OSVERSIONINFO
        >
        > myOS.dwOSVersio nInfoSize = Len(myOS)
        > GetVersionEx myOS
        > IsWinNT = (myOS.dwPlatfor mId = VER_PLATFORM_WI N32_NT)
        > End Function
        >
        > Private Sub EnableShutDown( )
        > ' Set the shut down privilege for the current application
        > Dim hProc As Long, hToken As Long, mLUID As LUID
        > Dim mPriv As TOKEN_PRIVILEGE S, mNewPriv As TOKEN_PRIVILEGE S
        >
        > hProc = GetCurrentProce ss()
        > OpenProcessToke n hProc, TOKEN_ADJUST_PR IVILEGES + TOKEN_QUERY, _
        > hToken
        > LookupPrivilege Value "", "SeShutdownPriv ilege", mLUID
        > mPriv.Privilege Count = 1
        > mPriv.Privilege s(0).Attributes = SE_PRIVILEGE_EN ABLED
        > mPriv.Privilege s(0).pLuid = mLUID
        >
        > ' enable shutdown privilege for the current application
        > AdjustTokenPriv ileges hToken, False, mPriv, 4 + (12 * _
        > mPriv.Privilege Count), mNewPriv, 4 + (12 * _
        > mNewPriv.Privil egeCount)
        > End Sub
        >
        > --
        > auric underscore underscore at hotmail dot com
        > *****
        > The absence of war does not automatically mean peace.[/color]


        Comment

        • Auric__

          #5
          Re: computer shutdown

          On Thu, 14 Oct 2004 11:24:19 +0200, "Maarten" <gurkjaan@hotma il.com>
          wrote:
          [color=blue]
          >thanks for the reply
          >
          >Public Sub quitWindows(whi ch As Long)
          > Dim n As Long
          >
          > If IsWinNT Then EnableShutDown
          > n = ExitWindowsEx(w hich, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
          > If n Then
          > Dim x As String * 256
          > FormatMessage FORMAT_MESSAGE_ FROM_SYSTEM, 0, Err.LastDllErro r, _
          > 0, x, 256, 0
          > Else
          > Unload frmBackground
          > End If
          >End Sub
          >
          >in this part i get ann error
          >
          >FormatMessag e sub or function not derfined?[/color]

          Sorry, FormatMessage is another API call; in the project I pulled that
          file from I declared it public (I use it more than once) in another
          module. I use it specifically to get the error message so I can log it
          (if ExitWindowsEx returns a non-zero value, there was an error). You can
          safely remove that entire if-then-else structure, or you can add a call
          to your logging routine immediately after the call to FormatMessage.
          Here's what you need to add to use it:

          Public Declare Function FormatMessage Lib "kernel32" Alias _
          "FormatMessageA " (ByVal dwFlags As Long, lpSource As Any, _
          ByVal dwMessageId As Long, ByVal dwLanguageId As Long, ByVal _
          lpBuffer As String, ByVal nSize As Long, Arguments As Long) _
          As Long

          Public Const FORMAT_MESSAGE_ FROM_SYSTEM = &H1000
          [color=blue]
          >I'm using a computer with xp to program, but i test and run my software on
          >a computer with win 98. wil it work for both?[/color]

          That's the whole point of quitWindows - it starts off by finding out
          whether or not you're using an NT-based system (using the IsWinNT
          function), and if so it kicks up its permissions so it can do the
          shutdown. If it's a 9x system, well, the whole concept of thread
          security is non-existent there, so *any* process can shutdown Windows
          just by making the appropriate API calls.

          Another thing that I see I forgot is that the value you pass to
          quitWindows (which as long) needs to be one of the EWX_* values:
          EWX_LOGOFF
          EWX_SHUTDOWN
          EWX_REBOOT
          EWX_POWEROFF

          You can OR them with EWX_FORCE or EWX_FORCEIFHUNG to force closing apps
          that don't respond:
          quitWindows EWX_REBOOT Or EWX_FORCEIFHUNG

          There might also be an EWX_STANDBY, but since I don't use it (and don't
          have the API viewer installed) I can't check.
          --
          auric underscore underscore at hotmail dot com
          *****
          IRC: real-time Usenet.

          Comment

          • Maarten

            #6
            Re: computer shutdown

            very nice thank jou verry much. ik worked fine

            Maarten

            "Maarten" <gurkjaan@hotma il.com> wrote in message
            news:416cdfc5$0 $859$ba620e4c@n ews.skynet.be.. .[color=blue]
            > hi
            >
            > does someone knows how to shut down you're compueter with vb 6.0?
            >
            > thanks Maarten
            >
            >[/color]


            Comment

            Working...