ExitWindowsEx function not working.

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

    #1

    ExitWindowsEx function not working.

    Using VB2003 on Windows XP Pro

    Why doesn't this work?

    Public Class Form1
    Inherits System.Windows. Forms.Form

    Private Declare Auto Function ExitWindowsEx Lib "user32.dll " (ByVal
    uFlags As Int32, ByVal dwreserved As Int32) As Int32

    Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button3.Click
    Dim retval As Int32
    retval = ExitWindowsEx(2 + 4, 0)
    End Sub
    End Class
  • cj

    #2
    Re: ExitWindowsEx function not working.

    I am an administrator if that matters. Also this does work.

    Public Class Form1
    Inherits System.Windows. Forms.Form

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
    System.EventArg s) Handles Button1.Click
    'Shutdown
    Dim t As Single
    Dim WMIService, Computer As Object
    WMIService =
    GetObject("Winm gmts:{impersona tionLevel=imper sonate,(Debug,S hutdown)}")
    For Each Computer In
    WMIService.Inst ancesOf("Win32_ OperatingSystem ")
    t = Computer.Win32S hutdown(2 + 4, 0)
    Next
    End Sub
    End Class

    cj wrote:[color=blue]
    > Using VB2003 on Windows XP Pro
    >
    > Why doesn't this work?
    >
    > Public Class Form1
    > Inherits System.Windows. Forms.Form
    >
    > Private Declare Auto Function ExitWindowsEx Lib "user32.dll " (ByVal
    > uFlags As Int32, ByVal dwreserved As Int32) As Int32
    >
    > Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
    > System.EventArg s) Handles Button3.Click
    > Dim retval As Int32
    > retval = ExitWindowsEx(2 + 4, 0)
    > End Sub
    > End Class[/color]

    Comment

    • Patrice

      #3
      Re: ExitWindowsEx function not working.

      Try perhaps :


      It could be that you need also some code to grant this privilege to the
      process...

      --
      Patrice

      "cj" <cj@nospam.nosp am> a écrit dans le message de news:
      %23SW%232cjjGHA .2200@TK2MSFTNG P05.phx.gbl...[color=blue]
      >I am an administrator if that matters. Also this does work.
      >
      > Public Class Form1
      > Inherits System.Windows. Forms.Form
      >
      > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
      > System.EventArg s) Handles Button1.Click
      > 'Shutdown
      > Dim t As Single
      > Dim WMIService, Computer As Object
      > WMIService =
      > GetObject("Winm gmts:{impersona tionLevel=imper sonate,(Debug,S hutdown)}")
      > For Each Computer In
      > WMIService.Inst ancesOf("Win32_ OperatingSystem ")
      > t = Computer.Win32S hutdown(2 + 4, 0)
      > Next
      > End Sub
      > End Class
      >
      > cj wrote:[color=green]
      >> Using VB2003 on Windows XP Pro
      >>
      >> Why doesn't this work?
      >>
      >> Public Class Form1
      >> Inherits System.Windows. Forms.Form
      >>
      >> Private Declare Auto Function ExitWindowsEx Lib "user32.dll " (ByVal
      >> uFlags As Int32, ByVal dwreserved As Int32) As Int32
      >>
      >> Private Sub Button3_Click(B yVal sender As System.Object, ByVal e As
      >> System.EventArg s) Handles Button3.Click
      >> Dim retval As Int32
      >> retval = ExitWindowsEx(2 + 4, 0)
      >> End Sub
      >> End Class[/color][/color]


      Comment

      • Jeffrey Tan[MSFT]

        #4
        Re: ExitWindowsEx function not working.

        Hi cj,

        Thanks for your post!

        Regarding Win32 programming, you should always check the API return value
        to determine if the API calling fails. Once the API fails, we can use
        Marshal.GetLast Win32Error() method to get the Win32 error code. With
        searching this error code in errlook.exe, you can get the description text
        of the error.
        Note: errlook.exe is a tool followed with VS2005/VS.net2003. You can type
        errlook.exe in "Visual Studio 2005 Command Prompt" to run the tool.

        You should modify the code snippet as below:
        Private Declare Auto Function ExitWindowsEx Lib "user32.dll " (ByVal uFlags
        As Int32, ByVal dwreserved As Int32) As Int32
        Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
        System.EventArg s) Handles Button1.Click
        Dim retval As Int32
        retval = ExitWindowsEx(2 + 4, 0)
        If (retval = 0) Then

        MessageBox.Show (System.Runtime .InteropService s.Marshal.GetLa stWin32Error(). T
        oString())
        End If
        End Sub

        With running, you will get error code 1314, which standards for "A required
        privilege is not held by the client." in errlook.exe. With MSDN, you will
        see that to shut down or restart the system, the calling process must use
        the AdjustTokenPriv ileges function to enable the SE_SHUTDOWN_NAM E
        privilege. This is why our calling will fail.
        Note: most of the privilege is disabled for the user account by default.
        You have to use AdjustTokenPriv ileges to enable it explicitly.

        The KB provided by Patrice has demonstrating the correct way of calling
        ExitWindowsEx. You may give it a try.

        Hope this helps!

        Best regards,
        Jeffrey Tan
        Microsoft Online Community Support
        =============== =============== =============== =====
        When responding to posts, please "Reply to Group" via your newsreader so
        that others may learn and benefit from your issue.
        =============== =============== =============== =====
        This posting is provided "AS IS" with no warranties, and confers no rights.

        Comment

        • cj

          #5
          Re: ExitWindowsEx function not working.

          Jeffrey, do you know where I can find a VB.net 2003 example of
          rebooting a pc with ExitWindowsEx? The MS page is working with VB6 and
          I've already spent too long trying to get it to work in VB.net 2003.
          The simpler the example the better.

          Jeffrey Tan[MSFT] wrote:[color=blue]
          > Hi cj,
          >
          > Thanks for your post!
          >
          > Regarding Win32 programming, you should always check the API return value
          > to determine if the API calling fails. Once the API fails, we can use
          > Marshal.GetLast Win32Error() method to get the Win32 error code. With
          > searching this error code in errlook.exe, you can get the description text
          > of the error.
          > Note: errlook.exe is a tool followed with VS2005/VS.net2003. You can type
          > errlook.exe in "Visual Studio 2005 Command Prompt" to run the tool.
          >
          > You should modify the code snippet as below:
          > Private Declare Auto Function ExitWindowsEx Lib "user32.dll " (ByVal uFlags
          > As Int32, ByVal dwreserved As Int32) As Int32
          > Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As
          > System.EventArg s) Handles Button1.Click
          > Dim retval As Int32
          > retval = ExitWindowsEx(2 + 4, 0)
          > If (retval = 0) Then
          >
          > MessageBox.Show (System.Runtime .InteropService s.Marshal.GetLa stWin32Error(). T
          > oString())
          > End If
          > End Sub
          >
          > With running, you will get error code 1314, which standards for "A required
          > privilege is not held by the client." in errlook.exe. With MSDN, you will
          > see that to shut down or restart the system, the calling process must use
          > the AdjustTokenPriv ileges function to enable the SE_SHUTDOWN_NAM E
          > privilege. This is why our calling will fail.
          > Note: most of the privilege is disabled for the user account by default.
          > You have to use AdjustTokenPriv ileges to enable it explicitly.
          >
          > The KB provided by Patrice has demonstrating the correct way of calling
          > ExitWindowsEx. You may give it a try.
          >
          > Hope this helps!
          >
          > Best regards,
          > Jeffrey Tan
          > Microsoft Online Community Support
          > =============== =============== =============== =====
          > When responding to posts, please "Reply to Group" via your newsreader so
          > that others may learn and benefit from your issue.
          > =============== =============== =============== =====
          > This posting is provided "AS IS" with no warranties, and confers no rights.
          >[/color]

          Comment

          • Jeffrey Tan[MSFT]

            #6
            Re: ExitWindowsEx function not working.

            Hi CJ,

            Thanks for your feedback!

            I can find the 2 VB.net sample below:
            "Shut Down, Restart, and Log Off From Code "
            http://blogs.msdn.com/brad_mccabe/ar...02/383542.aspx


            Additionally, http://www.pinvoke.net provides a lot of Win32 API
            declaration regarding C#/VB.net, it is a good reference for doing p/invoke
            in .Net. Also, any further interop and p/invoke questions can be posted in
            microsoft.publi c.dotnet.framew ork.interop newsgroup.

            Hope this helps!

            Best regards,
            Jeffrey Tan
            Microsoft Online Community Support
            =============== =============== =============== =====
            When responding to posts, please "Reply to Group" via your newsreader so
            that others may learn and benefit from your issue.
            =============== =============== =============== =====
            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            Working...