Oppure

Loading
15/10/06 10:05
hossaini001
SALVE A TUTTI.
sono nuovo.
avrei un problema. sto creando un programma dove è presente un tasto "disconnetti".
premendo il tasto "disaconnetti" il programma dovrebbe disconnettersi da windows.
sapete come fare? grazie.

nb: non spegnere, non riavviare ma solamente disconnettersi.
Ultima modifica effettuata da hossaini001 15/10/06 10:06
aaa
15/10/06 13:31
natamas
Questo codice serve a fare il LOGOFF di un utente da windows, ma comunque combiando alcune impostazioni nel codice si possono fare diverse operazione come spegnere o riavviare il pc.


CODICE DA INSERIRE IN UN MODULO:
Option Explicit
'***************
'*Shutdown part*
'***************
Public Const EWX_LOGOFF = 0       'fa il LOG-OFF dell'utente
Public Const EWX_SHUTDOWN = 1     'spenge il PC non completamente (con la schermata "Ora è possibile spegnere il computer")
Public Const EWX_REBOOT = 2       'riavvia il PC
Public Const EWX_FORCE = 4        'forza lo spengimento (può causare perdita di dati)
Public Const EWX_POWEROFF = 8     'spenge completamente il PC (se la scheda madre lo permette)
'The ExitWindowsEx function either logs off, shutsdown, or shutsdown and restarts the system.
Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
'The GetLastError function returns the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.
'Multiple threads do not overwrite each other's last-error code.
Public GetLasrError As Long
Public Declare Function GetLastError Lib "kernel32" () As Long
'OS constants
Public Const mlngWindows95 = 0
Public Const mlngWindowsNT = 1
Public glngWhichWindows32 As Long
'The GetVersion function returns the operating system in use.
Public Declare Function GetVersion Lib "kernel32" () As Long
Public Type LUID
  UsedPart As Long
  IgnoredForNowHigh32BitPart As Long
End Type
Public Type LUID_AND_ATTRIBUTES
  TheLuid As LUID
  Attributes As Long
End Type
Public Type TOKEN_PRIVILEGES
  PrivilegeCount As Long
  TheLuid As LUID
  Attributes As Long
End Type
'The GetCurrentProcess function returns a pseudohandle for the current process.
Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
'The OpenProcessToken function opens the access token associated with a process.
Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
'The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
'The AdjustTokenPrivileges function enables or disables privileges in the specified access token. Enabling or disabling privileges in an access token requires TOKEN_ADJUST_PRIVILEGES access.
Public Declare Function AdjustTokenPrivileges Lib "advapi32" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long
Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
'******************************************************
'*This procedure sets the proper privileges to allow a*
'*log off or a shutdown to occur under Windows NT.    *
'******************************************************
Public Sub AdjustToken()
  Const TOKEN_ADJUST_PRIVILEGES = &H20
  Const TOKEN_QUERY = &H8
  Const SE_PRIVILEGE_ENABLED = &H2
  Dim hdlProcessHandle, hdlTokenHandle, lBufferNeeded As Long
  Dim tmpLuid As LUID
  Dim tkp As TOKEN_PRIVILEGES
  Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  'Set the error code of the last thread to zero using the SetLast Error function. Do this so that the GetLastError function does not return a value other than zero for no apparent reason.
  SetLastError 0
  'Use the GetCurrentProcess function to set the hdlProcessHandle variable.
  hdlProcessHandle = GetCurrentProcess()
  If GetLastError <> 0 Then MsgBox "GetCurrentProcess error==" & GetLastError
  OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
  If GetLastError <> 0 Then MsgBox "OpenProcessToken error==" & GetLastError
  'Get the LUID for shutdown privilege.
  LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  If GetLastError <> 0 Then MsgBox "LookupPrivilegeValue error==" & GetLastError
  tkp.PrivilegeCount = 1    ' One privilege to set.
  tkp.TheLuid = tmpLuid
  tkp.Attributes = SE_PRIVILEGE_ENABLED
  'Enable the shutdown privilege in the access token of this process.
  AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  If GetLastError <> 0 Then MsgBox "AdjustTokenPrivileges error==" & GetLastError
End Sub


CODICE DA INSERIRE IN UN FORM CON IL PULSANTE AVENTE NOME: cmdCountdown
Private Sub cmdCountdown_Click()
  Call Shutdown
End Sub
Private Sub Form_Load()
  Dim lngVersion As Long
  'tipo di OS
  lngVersion = GetVersion()
  If ((lngVersion And &H80000000) = 0) Then
    'OS = Windows NT/2000/XP
    glngWhichWindows32 = mlngWindowsNT
  Else
    'OS = Windows 9x
    glngWhichWindows32 = mlngWindows95
  End If
End Sub
Public Sub Shutdown()
  'Procedura che provoca lo spengimento del PC:
  'se l'OS è Win NT/2000/XP allora prima di spengere setta i privilegi
  If glngWhichWindows32 = mlngWindowsNT Then
    'aggiusta i privilegi per poter spengere il PC
    AdjustToken
    'se AdjustToken va in errore allora visualizzo il tipo di errore
    If GetLasrError <> 0 Then MsgBox "Post-AdjustToken's GetLastError " & GetLastError
  End If
  'Eseguo l'operazione scelta: EWX_LOGOFF(disconnette pc), EWX_REBOOT(riavvia il pc), EWX_POWEROFF(spegne pc)
  ExitWindowsEx EWX_LOGOFF, &HFFFF
  'se ExitWindowsEx va in errore allora visualizzo il tipo di errore
  If GetLasrError <> 0 Then MsgBox "ExitWindowsEx's GetLastError " & GetLastError
End Sub


Se ci sono problemi scrivi, buon utilizzo.
Ciao
:k:
Ultima modifica effettuata da natamas 15/10/06 13:37
aaa
18/10/06 15:31
hossaini001
1) mi dispiace ma non riesco a farla funzionare a dovere. cè per caso qualche altro codice magari più semplice per disconnettere.

2) ò forse il problema è che io uso
visual basic 2005 express edition?

2) oppure un altra cosa mi basta solo che l'utente non riesca a lavorare finchè non preme il pulsante accedi. quindi mi serve qualcosa che blocchi la facciata di windows e si veda solo il bottone con scritto accedi.
aaa
18/10/06 21:06
natamas
2) ò forse il problema è che io uso
visual basic 2005 express edition?


Il codice che ti ho dato io è per Visual Basic 6 e non per Visual Basic 2005, perchè purtroppo per VB2005 le cose sono legermente diverse.
Ciao
:D
aaa
19/10/06 11:35
hossaini001
allora sapete per caso come posso fare in
vb 2005. :-?:-?:-?:-?
aaa
30/06/07 15:31
gius
Perchè tutto questo codice natamas se ci sono pochissime righe di codice:asd::asd:

In un modulo
Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal
wReserved&)
Global Const EWX_LOGOFF = 0 
Global Const EWX_SHUTDOWN = 1 
Global Const EWX_REBOOT = 2 
Global Const EWX_FORCE = 4 

In un bottone:
lresult = ExitWindowsEx(EWX_SHUTDOWN, 0&)


P.S.a posto di EWX_SHUTDOWN potresti cambiare con le altre variabili globali:D
aaa
30/06/07 15:37
Hacker
altra discussione del 2006:alert::alert:
aaa
01/07/07 7:32
natamas
Postato originariamente da gius:

Perchè tutto questo codice natamas se ci sono pochissime righe di codice:asd::asd:

In un modulo
Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal
wReserved&)
Global Const EWX_LOGOFF = 0 
Global Const EWX_SHUTDOWN = 1 
Global Const EWX_REBOOT = 2 
Global Const EWX_FORCE = 4 

In un bottone:
lresult = ExitWindowsEx(EWX_SHUTDOWN, 0&)


P.S.a posto di EWX_SHUTDOWN potresti cambiare con le altre variabili globali:D



Guarda che il mio codice è lungo ma funziona con tutti i windows, il tuo non credo che funzioni con windows 98 - 95
:)
aaa