Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Visual Basic 6 - disconnetti windows.
Forum - Visual Basic 6 - disconnetti windows.

Pagine: [ 1 2 ] Precedente | Prossimo
Avatar
hossaini001 (Normal User)
Newbie


Messaggi: 3
Iscritto: 15/10/2006

Segnala al moderatore
Postato alle 12:05
Domenica, 15/10/2006
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 il 15/10/2006 alle 12:06
PM Quote
Avatar
natamas (Member)
Pro


Messaggi: 115
Iscritto: 09/04/2006

Segnala al moderatore
Postato alle 15:31
Domenica, 15/10/2006
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:
Codice sorgente - presumibilmente VB.NET

  1. Option Explicit
  2. '***************
  3. '*Shutdown part*
  4. '***************
  5. Public Const EWX_LOGOFF = 0       'fa il LOG-OFF dell'utente
  6. Public Const EWX_SHUTDOWN = 1     'spenge il PC non completamente (con la schermata "Ora è possibile spegnere il computer")
  7. Public Const EWX_REBOOT = 2       'riavvia il PC
  8. Public Const EWX_FORCE = 4        'forza lo spengimento (può causare perdita di dati)
  9. Public Const EWX_POWEROFF = 8     'spenge completamente il PC (se la scheda madre lo permette)
  10. 'The ExitWindowsEx function either logs off, shutsdown, or shutsdown and restarts the system.
  11. Public Declare Function ExitWindowsEx Lib "user32" (ByVal dwOptions As Long, ByVal dwReserved As Long) As Long
  12. 'The GetLastError function returns the calling thread's last-error code value. The last-error code is maintained on a per-thread basis.
  13. 'Multiple threads do not overwrite each other's last-error code.
  14. Public GetLasrError As Long
  15. Public Declare Function GetLastError Lib "kernel32" () As Long
  16. 'OS constants
  17. Public Const mlngWindows95 = 0
  18. Public Const mlngWindowsNT = 1
  19. Public glngWhichWindows32 As Long
  20. 'The GetVersion function returns the operating system in use.
  21. Public Declare Function GetVersion Lib "kernel32" () As Long
  22. Public Type LUID
  23.   UsedPart As Long
  24.   IgnoredForNowHigh32BitPart As Long
  25. End Type
  26. Public Type LUID_AND_ATTRIBUTES
  27.   TheLuid As LUID
  28.   Attributes As Long
  29. End Type
  30. Public Type TOKEN_PRIVILEGES
  31.   PrivilegeCount As Long
  32.   TheLuid As LUID
  33.   Attributes As Long
  34. End Type
  35. 'The GetCurrentProcess function returns a pseudohandle for the current process.
  36. Public Declare Function GetCurrentProcess Lib "kernel32" () As Long
  37. 'The OpenProcessToken function opens the access token associated with a process.
  38. Public Declare Function OpenProcessToken Lib "advapi32" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
  39. 'The LookupPrivilegeValue function retrieves the locally unique identifier (LUID) used on a specified system to locally represent the specified privilege name.
  40. Public Declare Function LookupPrivilegeValue Lib "advapi32" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, lpLuid As LUID) As Long
  41. '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.
  42. 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
  43. Public Declare Sub SetLastError Lib "kernel32" (ByVal dwErrCode As Long)
  44. '******************************************************
  45. '*This procedure sets the proper privileges to allow a*
  46. '*log off or a shutdown to occur under Windows NT.    *
  47. '******************************************************
  48. Public Sub AdjustToken()
  49.   Const TOKEN_ADJUST_PRIVILEGES = &H20
  50.   Const TOKEN_QUERY = &H8
  51.   Const SE_PRIVILEGE_ENABLED = &H2
  52.   Dim hdlProcessHandle, hdlTokenHandle, lBufferNeeded As Long
  53.   Dim tmpLuid As LUID
  54.   Dim tkp As TOKEN_PRIVILEGES
  55.   Dim tkpNewButIgnored As TOKEN_PRIVILEGES
  56.   '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.
  57.   SetLastError 0
  58.   'Use the GetCurrentProcess function to set the hdlProcessHandle variable.
  59.   hdlProcessHandle = GetCurrentProcess()
  60.   If GetLastError <> 0 Then MsgBox "GetCurrentProcess error==" & GetLastError
  61.   OpenProcessToken hdlProcessHandle, (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), hdlTokenHandle
  62.   If GetLastError <> 0 Then MsgBox "OpenProcessToken error==" & GetLastError
  63.   'Get the LUID for shutdown privilege.
  64.   LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
  65.   If GetLastError <> 0 Then MsgBox "LookupPrivilegeValue error==" & GetLastError
  66.   tkp.PrivilegeCount = 1    ' One privilege to set.
  67.   tkp.TheLuid = tmpLuid
  68.   tkp.Attributes = SE_PRIVILEGE_ENABLED
  69.   'Enable the shutdown privilege in the access token of this process.
  70.   AdjustTokenPrivileges hdlTokenHandle, False, tkp, Len(tkpNewButIgnored), tkpNewButIgnored, lBufferNeeded
  71.   If GetLastError <> 0 Then MsgBox "AdjustTokenPrivileges error==" & GetLastError
  72. End Sub



CODICE DA INSERIRE IN UN FORM CON IL PULSANTE AVENTE NOME: cmdCountdown
Codice sorgente - presumibilmente VB.NET

  1. Private Sub cmdCountdown_Click()
  2.   Call Shutdown
  3. End Sub
  4. Private Sub Form_Load()
  5.   Dim lngVersion As Long
  6.   'tipo di OS
  7.   lngVersion = GetVersion()
  8.   If ((lngVersion And &H80000000) = 0) Then
  9.     'OS = Windows NT/2000/XP
  10.     glngWhichWindows32 = mlngWindowsNT
  11.   Else
  12.     'OS = Windows 9x
  13.     glngWhichWindows32 = mlngWindows95
  14.   End If
  15. End Sub
  16. Public Sub Shutdown()
  17.   'Procedura che provoca lo spengimento del PC:
  18.   'se l'OS è Win NT/2000/XP allora prima di spengere setta i privilegi
  19.   If glngWhichWindows32 = mlngWindowsNT Then
  20.     'aggiusta i privilegi per poter spengere il PC
  21.     AdjustToken
  22.     'se AdjustToken va in errore allora visualizzo il tipo di errore
  23.     If GetLasrError <> 0 Then MsgBox "Post-AdjustToken's GetLastError " & GetLastError
  24.   End If
  25.   'Eseguo l'operazione scelta: EWX_LOGOFF(disconnette pc), EWX_REBOOT(riavvia il pc), EWX_POWEROFF(spegne pc)
  26.   ExitWindowsEx EWX_LOGOFF, &HFFFF
  27.   'se ExitWindowsEx va in errore allora visualizzo il tipo di errore
  28.   If GetLasrError <> 0 Then MsgBox "ExitWindowsEx's GetLastError " & GetLastError
  29. End Sub



Se ci sono problemi scrivi, buon utilizzo.
Ciao
:k:

Ultima modifica effettuata da natamas il 15/10/2006 alle 15:37
PM Quote
Avatar
hossaini001 (Normal User)
Newbie


Messaggi: 3
Iscritto: 15/10/2006

Segnala al moderatore
Postato alle 17:31
Mercoledì, 18/10/2006
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.  

PM Quote
Avatar
natamas (Member)
Pro


Messaggi: 115
Iscritto: 09/04/2006

Segnala al moderatore
Postato alle 23:06
Mercoledì, 18/10/2006
Testo quotato


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

PM Quote
Avatar
hossaini001 (Normal User)
Newbie


Messaggi: 3
Iscritto: 15/10/2006

Segnala al moderatore
Postato alle 13:35
Giovedì, 19/10/2006
allora sapete per caso come posso fare in
vb 2005.  :-?:-?:-?:-?

PM Quote
Avatar
gius (Ex-Member)
Expert


Messaggi: 294
Iscritto: 20/06/2007

Segnala al moderatore
Postato alle 17:31
Sabato, 30/06/2007
Perchè tutto questo codice natamas se ci sono pochissime righe di codice:asd::asd:

In un modulo
Codice sorgente - presumibilmente VB.NET

  1. Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal
  2. wReserved&)
  3. Global Const EWX_LOGOFF = 0
  4. Global Const EWX_SHUTDOWN = 1
  5. Global Const EWX_REBOOT = 2
  6. Global Const EWX_FORCE = 4


In un bottone:
Codice sorgente - presumibilmente Plain Text

  1. lresult = ExitWindowsEx(EWX_SHUTDOWN, 0&)



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

PM Quote
Avatar
Hacker (Member)
Guru


Messaggi: 1014
Iscritto: 06/06/2006

Segnala al moderatore
Postato alle 17:37
Sabato, 30/06/2007
altra discussione del 2006:alert::alert:

PM Quote
Avatar
natamas (Member)
Pro


Messaggi: 115
Iscritto: 09/04/2006

Segnala al moderatore
Postato alle 9:32
Domenica, 01/07/2007
Testo quotato

Postato originariamente da gius:

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

In un modulo
Codice sorgente - presumibilmente VB.NET

  1. Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, ByVal
  2. wReserved&)
  3. Global Const EWX_LOGOFF = 0
  4. Global Const EWX_SHUTDOWN = 1
  5. Global Const EWX_REBOOT = 2
  6. Global Const EWX_FORCE = 4


In un bottone:
Codice sorgente - presumibilmente Plain Text

  1. 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
:)

PM Quote
Avatar
gius (Ex-Member)
Expert


Messaggi: 294
Iscritto: 20/06/2007

Segnala al moderatore
Postato alle 9:46
Domenica, 01/07/2007
Funziona con tutti i windows:rotfl::rotfl:

PM Quote
Pagine: [ 1 2 ] Precedente | Prossimo