Visual Basic - temporizador

Life is soft - evento anual de software empresarial
   
Vista:

temporizador

Publicado por jioty (1 intervención) el 02/06/2014 02:19:36
Me podrían ayudar con el código fuente de un temporizador que apaga o reinicie la pc
Gracias.
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de Antoni Masana

temporizador

Publicado por Antoni Masana (88 intervenciones) el 02/06/2014 08:37:54
Te pongo una rutina para rebotar o apagar el el Ordenador.

Es un modulo de Clase, llama al Reboot y con la variable Shut_Tipo decide si rebota o apaga el equipo

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
' </> ----------------------------------------------------------------- </>
' </> ---&--- </>                                           </> ---&--- </>
' </> ---&--- </>   Shutdown                                </> ---&--- </>
' </> ---&--- </>                                           </> ---&--- </>
' </> ----------------------------------------------------------------- </>
 
 
Option Explicit
 
'---------------------------------------------------------------------------
' API declarations to obtain the Windows version and the type of
' keyboard.
'---------------------------------------------------------------------------
 
Private Declare Function apiGetVersion _
        Lib "kernel32" _
        Alias "GetVersionExA" (ByRef lpVersionInformation _
                                       As OSVERSIONINFO) As Long
 
' size of 'Type' = (5 x 4 bytes) =  20 bytes (the 5 Longs)
'                                  128 bytes (fixed-length string)
'                                 ----- +
'                                  148 bytes
 
Private Type OSVERSIONINFO
    dwOSVersionInfoSize As Long      ' Has to be set to size of 'type'= 148
    dwMajorVersion As Long           ' Gives the Major version
    dwMinorVersion As Long           ' Gives the Minor version
    dwBuildNumber As Long            ' Gives the buildnumber (I don't use it)
    dwPlatformId As Long             ' Gives the operating system.
    szCSDVersion As String * 128     ' ?
End Type
 
'---------------------------------------------------------------------------
' API Declaración para Rebotar Windows 95/98
'---------------------------------------------------------------------------
 
'Declare Function ExitWindowsEx& Lib "user32" (ByVal uFlags&, _
                                              ByVal dwReserved&)
 
'---------------------------------------------------------------------------
' API Declaración para Rebotar Windows NT
'---------------------------------------------------------------------------
 
' Tipos definidos
 
Private Type LUID
    UsedPart As Long
    IgnoredForNowHigh32BitPart As Long
End Type
 
Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    TheLuid As LUID
    Attributes As Long
End Type
 
' Las funciones del API
 
Private Declare Function ExitWindowsEx _
        Lib "user32" (ByVal dwOptions As Long, _
                      ByVal dwReserved As Long) As Long
 
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
 
Private Declare Function OpenProcessToken _
        Lib "advapi32" (ByVal ProcessHandle As Long, _
                        ByVal DesiredAccess As Long, _
                              TokenHandle As Long) As Long
 
Private Declare Function LookupPrivilegeValue _
        Lib "advapi32" _
        Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, _
                                       ByVal lpName As String, _
                                             lpLuid As LUID) As Long
 
Private 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
 
'---------------------------------------------------------------------------
' SUB: WinVer
'
' This sub returns information about the operating system, and about
' the Windows Version.
'
' e.g. Windows 3.11
'      The sub will return:
'         - intMajor    = 3
'         - intMinor    = 11
'         - strPlatform = Windows 3.11
'
' OUT:  intMajor        - Integer containing the major version of Windows.
'       intMinor        - Integer containing the minor version of windows.
'
' strPlatfrom returns one of the following :
'   Windows 95
'   Windows NT
'   Windows + Version
'
' If the call fails intMajor = 0, intMinor = 0, and strPlatform = ""
'---------------------------------------------------------------------------
 
Public Sub WinVer(ByRef intMajor As Integer, _
                  ByRef intMinor As Integer, _
                  ByRef strPlatform As String)
 
    Dim OSystem As OSVERSIONINFO
 
    OSystem.dwOSVersionInfoSize = 148
 
    ' The size of the structure must be set before the call.
 
    If apiGetVersion(OSystem) Then
 
       ' Call the API. It fills the OSystem type.
 
       intMajor = OSystem.dwMajorVersion   ' Store the Major version
       intMinor = OSystem.dwMinorVersion   ' Store the Minor version
 
       Select Case OSystem.dwPlatformId    ' Set strPlatform
          Case 0: strPlatform = "W" + CStr(intMajor)
          Case 1: strPlatform = "W95"
          Case 2: strPlatform = "WNT"
       End Select
    Else
       ' The call failed, set the values to zero
       intMajor = 0
       intMinor = 0
       strPlatform = ""
   End If
End Sub
 
' Código para poner en el formulario
 
Private Sub AdjustToken()
    Const TOKEN_ADJUST_PRIVILEGES = &H20
    Const TOKEN_QUERY = &H8
    Const SE_PRIVILEGE_ENABLED = &H2
 
    Dim hdlProcessHandle As Long
    Dim hdlTokenHandle As Long
    Dim tmpLuid As LUID
    Dim tkp As TOKEN_PRIVILEGES
    Dim tkpNewButIgnored As TOKEN_PRIVILEGES
    Dim lBufferNeeded As Long
 
    hdlProcessHandle = GetCurrentProcess()
 
    OpenProcessToken hdlProcessHandle, _
                     (TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY), _
                     hdlTokenHandle
 
    ' Get the LUID for shutdown privilege.
    LookupPrivilegeValue "", "SeShutdownPrivilege", tmpLuid
 
    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
 
End Sub
 
Public Sub Reboot()
    Dim Opc As Long, _
        Mayor As Integer, _
        Menor As Integer, _
        Version As String
 
    WinVer Mayor, Menor, Version
 
    If Version = "WNT" Then AdjustToken
 
    Select Case Shut_Tipo
       Case 1:  Opc = ExitWindowsEx(EXC_REBOOT Or EXC_FORCE, &HFFFF)
       Case 2:  Opc = ExitWindowsEx(EXC_SHUTDOWN Or EXC_FORCE, &HFFFF)
    End Select
End Sub

Para más información busca en google ExitWindowsEx
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

temporizador

Publicado por Pico (114 intervenciones) el 02/06/2014 14:29:43
Shell("Shutdown /s") para apagar
Shell("Shutdwon /r") para reiniciar
Shell("Shutdown /l") para cerrar usuario
Shell("Shutdown /h") Para hibernar
Shell("Shutdown /m \\nombre") Para apagar otro equipo en red

Con el parámetro /t se puede especificar un tiempo de espera antes de empezar.

Tiene la ventaja de que es igual en cualquier windows y no necesita privilegios especiales.
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar