Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. how to logoff using Windows Service

how to logoff using Windows Service

Scheduled Pinned Locked Moved Visual Basic
helptutorial
7 Posts 4 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    BSRK
    wrote on last edited by
    #1

    i have to check for a particular condition, and if the user doesnt satisfy that condition, i need to logoff him forcibly. (OS: WindowsXP and Win2K) obviously i have to use a windows service, but i couldnt figure out the code to loghim off. can anyone please help

    D 2 Replies Last reply
    0
    • B BSRK

      i have to check for a particular condition, and if the user doesnt satisfy that condition, i need to logoff him forcibly. (OS: WindowsXP and Win2K) obviously i have to use a windows service, but i couldnt figure out the code to loghim off. can anyone please help

      D Offline
      D Offline
      Dave Sexton
      wrote on last edited by
      #2

      Though it doesn't force a logoff the code below will programatically lock the workstation using an API call

      Declare Function LockWorkStation Lib "user32.dll" () As Long

      Call LockWorkStation() after condition evaluation. I'll look around for a better example.

      1 Reply Last reply
      0
      • B BSRK

        i have to check for a particular condition, and if the user doesnt satisfy that condition, i need to logoff him forcibly. (OS: WindowsXP and Win2K) obviously i have to use a windows service, but i couldnt figure out the code to loghim off. can anyone please help

        D Offline
        D Offline
        Dave Sexton
        wrote on last edited by
        #3

        Here is an API call that forces a logoff BUT I'm still trying to work out a bug - it throws an exception during debug saying that I've unbalanced the stack, though this exception isn't thrown when I run the code from outside the IDE. So basically what I'm saying is that the code below ain't perfect but it's closer to what you asked for than the previous example. This function takes two arguments. First argument is one or more flags (the declared constants) instructing windows how to shutdown. Second argument is reserved (I don't know why yet), and is set to zero.

        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

        then when needed call

        ExitWindowsEx(EWX_FORCE Or EWX_LOGOFF, 0)

        I'm still trying to figure it out better. Sorry that the help I've provided isn't very informative or perfect. :sigh:

        D 1 Reply Last reply
        0
        • D Dave Sexton

          Here is an API call that forces a logoff BUT I'm still trying to work out a bug - it throws an exception during debug saying that I've unbalanced the stack, though this exception isn't thrown when I run the code from outside the IDE. So basically what I'm saying is that the code below ain't perfect but it's closer to what you asked for than the previous example. This function takes two arguments. First argument is one or more flags (the declared constants) instructing windows how to shutdown. Second argument is reserved (I don't know why yet), and is set to zero.

          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

          then when needed call

          ExitWindowsEx(EWX_FORCE Or EWX_LOGOFF, 0)

          I'm still trying to figure it out better. Sorry that the help I've provided isn't very informative or perfect. :sigh:

          D Offline
          D Offline
          Dave Kreskowiak
          wrote on last edited by
          #4

          You've unbalanced the stack because your using the wrong data types for the parameters and return values. I'll make an educated guess and say that you lifted some VB6 code and tried to use it under VB.NET without any modification. Under VB6, the Long type is a 32-bit signed integer, while under VB.NET, the Long type is a 64-bit signed integer. This is why the stack is unbalanced. If you're going to use VB6 code, you have to change all the Long's to Integers under VB.NET:

          Public Const EWX_LOGOFF As Integer = &H0
          Public Const EWX_POWEROFF As Integer = &H8
          Public Const EWX_REBOOT As Integer = &H2
          Public Const EWX_RESTARTAPPS As Integer = &H40
          Public Const EWX_SHUTDOWN As Integer = &H1

          Public Const EWX_FORCE As Integer = &H4
          Public Const EWX_FORCEIFHUNG As Integer = &H10

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

          Oh! You also don't need the 'Alias' clause if the name you gave the function matches the name in the library.

          Dave Kreskowiak Microsoft MVP - Visual Basic

          Y D 2 Replies Last reply
          0
          • D Dave Kreskowiak

            You've unbalanced the stack because your using the wrong data types for the parameters and return values. I'll make an educated guess and say that you lifted some VB6 code and tried to use it under VB.NET without any modification. Under VB6, the Long type is a 32-bit signed integer, while under VB.NET, the Long type is a 64-bit signed integer. This is why the stack is unbalanced. If you're going to use VB6 code, you have to change all the Long's to Integers under VB.NET:

            Public Const EWX_LOGOFF As Integer = &H0
            Public Const EWX_POWEROFF As Integer = &H8
            Public Const EWX_REBOOT As Integer = &H2
            Public Const EWX_RESTARTAPPS As Integer = &H40
            Public Const EWX_SHUTDOWN As Integer = &H1

            Public Const EWX_FORCE As Integer = &H4
            Public Const EWX_FORCEIFHUNG As Integer = &H10

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

            Oh! You also don't need the 'Alias' clause if the name you gave the function matches the name in the library.

            Dave Kreskowiak Microsoft MVP - Visual Basic

            Y Offline
            Y Offline
            Yona Low
            wrote on last edited by
            #5

            "Wow" looks neat, you got my 5

            1 Reply Last reply
            0
            • D Dave Kreskowiak

              You've unbalanced the stack because your using the wrong data types for the parameters and return values. I'll make an educated guess and say that you lifted some VB6 code and tried to use it under VB.NET without any modification. Under VB6, the Long type is a 32-bit signed integer, while under VB.NET, the Long type is a 64-bit signed integer. This is why the stack is unbalanced. If you're going to use VB6 code, you have to change all the Long's to Integers under VB.NET:

              Public Const EWX_LOGOFF As Integer = &H0
              Public Const EWX_POWEROFF As Integer = &H8
              Public Const EWX_REBOOT As Integer = &H2
              Public Const EWX_RESTARTAPPS As Integer = &H40
              Public Const EWX_SHUTDOWN As Integer = &H1

              Public Const EWX_FORCE As Integer = &H4
              Public Const EWX_FORCEIFHUNG As Integer = &H10

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

              Oh! You also don't need the 'Alias' clause if the name you gave the function matches the name in the library.

              Dave Kreskowiak Microsoft MVP - Visual Basic

              D Offline
              D Offline
              Dave Sexton
              wrote on last edited by
              #6

              Thanks for corrections, I seldom have need to use API calls.

              Dave Kreskowiak wrote:

              I'll make an educated guess and say that you lifted some VB6 code

              Close - API Viewer 2004 :)

              Dave Kreskowiak wrote:

              Under VB6, the Long type is a 32-bit signed integer, while under VB.NET, the Long type is a 64-bit signed integer.

              I thought Long was Long regardless, my mistake :doh: Great advice, thanks!

              B 1 Reply Last reply
              0
              • D Dave Sexton

                Thanks for corrections, I seldom have need to use API calls.

                Dave Kreskowiak wrote:

                I'll make an educated guess and say that you lifted some VB6 code

                Close - API Viewer 2004 :)

                Dave Kreskowiak wrote:

                Under VB6, the Long type is a 32-bit signed integer, while under VB.NET, the Long type is a 64-bit signed integer.

                I thought Long was Long regardless, my mistake :doh: Great advice, thanks!

                B Offline
                B Offline
                BSRK
                wrote on last edited by
                #7

                Dear friends, thanks for the timely help.

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups