How to switch from users A to user B in visual basic?
User A should not log off.
Please help.
Printable View
How to switch from users A to user B in visual basic?
User A should not log off.
Please help.
[EDIT]
This link may have some info?
http://support.microsoft.com/?kbid=841291
I use this code that i found on the page of the link you gave to me, but it does nothing :(
Please help!!!
Code:Imports System.Runtime.InteropServices
Public Class Form1
'Constant declarations that you can use for message processing follow:
'
Private Const _WIN32_WINNT As Int32 = &H501
Private Const NOTIFY_FOR_THIS_SESSION As Int32 = &H0
Private Const WM_WTSSESSION_CHANGE As Int32 = &H2B1
Private Const WTS_CONSOLE_CONNECT As Int32 = &H1
Private Const WTS_CONSOLE_DISCONNECT As Int32 = &H2
Private Const WTS_SESSION_LOCK As Int32 = &H7
Private Const WTS_SESSION_UNLOCK As Int32 = &H8
Private Const WM_DESTROY As Int32 = &H2
Private Const WM_ACTIVATEAPP As Int32 = &H1C
'Declaration of API functions that you can use for message processing follow:
'
'The WTSUnRegisterSessionNotification function unregisters the specified
'window so that the specified window receives no more session-change notifications.
<DllImport("Wtsapi32.dll")> _
Private Shared Function WTSUnRegisterSessionNotification( _
ByVal hWnd As IntPtr) As Boolean
End Function
'The WTSRegisterSessionNotification function registers the specified
'window to receive session-change notifications.
<DllImport("Wtsapi32.dll")> _
Private Shared Function WTSRegisterSessionNotification( _
ByVal hWnd As IntPtr, ByVal dwFlags As Int32) As Boolean
End Function
'The PostQuitMessage function indicates to the system that a thread
'has made a request to quit. The PostQuitMessage function is typically used in
'response to a WM_DESTROY message.
<DllImport("user32.dll")> _
Private Shared Sub PostQuitMessage( _
ByVal nExitCode As Int32)
End Sub
'The OpenIcon function restores a minimized (iconic) window to its
'previous size and previous position. The OpenIcon function then activates the window.
<DllImport("user32.dll")> _
Private Shared Function OpenIcon(ByVal hwnd As IntPtr) As Boolean
End Function
'The SetForegroundWindow function puts the thread that created the
'specified window in the foreground and then activates the window.
<DllImport("user32.dll")> _
Private Shared Function SetForegroundWindow(ByVal hwnd As IntPtr) As Boolean
End Function
'
'The following processes Windows messages:
'
<System.Security.Permissions.PermissionSetAttribute _
(System.Security.Permissions.SecurityAction.Demand, Name:="FullTrust")> _
Protected Overrides Sub WndProc(ByRef m As Message) '
Select Case (m.Msg)
'
Case WM_WTSSESSION_CHANGE
Select Case (m.WParam.ToInt32)
Case WTS_CONSOLE_CONNECT
MessageBox.Show("WTS_CONSOLE_CONNECT", "WM_SESSION_CHANGE", MessageBoxButtons.OK)
Case WTS_CONSOLE_DISCONNECT
MessageBox.Show("WTS_CONSOLE_DISCONNECT", "WM_SESSION_CHANGE", MessageBoxButtons.OK)
Case WTS_SESSION_LOCK
MessageBox.Show("WTS_SESSION_LOCK", "WM_SESSION_CHANGE", MessageBoxButtons.OK)
Case WTS_SESSION_UNLOCK
MessageBox.Show("WTS_SESSION_UNLOCK", "WM_SESSION_CHANGE", MessageBoxButtons.OK)
Case Else
MessageBox.Show("test", "test", MessageBoxButtons.OK)
End Select
'
End Select
'
MyBase.WndProc(m)
'
End Sub
'
Protected Overrides Sub OnHandleCreated(ByVal e As System.EventArgs)
'
'The WTSRegisterSessionNotification function registers the specified
'window to receive session-change notifications.
'
WTSRegisterSessionNotification(Me.Handle, NOTIFY_FOR_THIS_SESSION)
'
MyBase.OnHandleCreated(e)
'
End Sub
'
Protected Overrides Sub OnHandleDestroyed(ByVal e As System.EventArgs)
'
WTSUnRegisterSessionNotification(Me.Handle)
PostQuitMessage(0)
'
End Sub
'
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
'
Dim blnChk As Boolean
'
'Checking for the existence of an
'application that is already running.
'
If UBound(Diagnostics.Process.GetProcessesByName _
(Diagnostics.Process.GetCurrentProcess.ProcessName)) > 0 Then
'
'Checking if there is already
'an instance that is running in this user's session.
'
blnChk = ActivatePrevInstance("FastSwitchingForm")
'
If (blnChk = False) Then
MsgBox("Another instance of " & _
Diagnostics.Process.GetCurrentProcess.ProcessName & _
" is already running in another user's session on this computer." & _
" You cannot run two instances at a time. You must use the other instance.")
End
End If
'
End If
'
End Sub
'
'This is a function to verify whether the application is already running.
'If the application is already running, set the focus on the application.
'
Private Function ActivatePrevInstance(ByVal argStrAppToFind As String) As Boolean
'
Dim PrevHndl As IntPtr = Nothing
Dim result As Boolean
Dim objProcess As New Process() 'This is a variable to hold an individual process.
Dim objProcesses() As Process 'This is a collection of all the processes that are running on the local computer.
'
objProcesses = Process.GetProcesses() 'Get all processes into the collection.()
'
For Each objProcess In objProcesses
'
'Check and then exit if there is an application that is already running.
'
If UCase(objProcess.MainWindowTitle) = UCase(argStrAppToFind) Then
MsgBox("Another instance of " & argStrAppToFind & _
" is already running on this computer. " & _
"You cannot run two instances at a time." & _
" You must use the other instance.")
PrevHndl = objProcess.MainWindowHandle
Exit For
End If
Next
'
If PrevHndl.Equals(IntPtr.Zero) Then
ActivatePrevInstance = False
Exit Function 'If no previous instance is found, exit the application.
Else
ActivatePrevInstance = True
End If
'
result = OpenIcon(PrevHndl) 'Restore the program.
result = SetForegroundWindow(PrevHndl) 'Activate the application.
End 'End the current instance of the application.
'
End Function
'
End Class
can anybody help me?