how to logoff using Windows Service
-
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
-
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
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. -
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
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 = 4Declare Function ExitWindowsEx Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved _
As Long) As Longthen 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:
-
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 = 4Declare Function ExitWindowsEx Lib "user32" Alias _
"ExitWindowsEx" (ByVal uFlags As Long, ByVal dwReserved _
As Long) As Longthen 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:
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 = &H1Public Const EWX_FORCE As Integer = &H4
Public Const EWX_FORCEIFHUNG As Integer = &H10Declare Function ExitWindowsEx Lib "user32" _
(ByVal uFlags As Integer, ByVal dwReserved As Integer) As IntegerOh! 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
-
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 = &H1Public Const EWX_FORCE As Integer = &H4
Public Const EWX_FORCEIFHUNG As Integer = &H10Declare Function ExitWindowsEx Lib "user32" _
(ByVal uFlags As Integer, ByVal dwReserved As Integer) As IntegerOh! 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
-
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 = &H1Public Const EWX_FORCE As Integer = &H4
Public Const EWX_FORCEIFHUNG As Integer = &H10Declare Function ExitWindowsEx Lib "user32" _
(ByVal uFlags As Integer, ByVal dwReserved As Integer) As IntegerOh! 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
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!
-
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!