VB中怎样实现关机的功能!!!(在线等待)

flyerlxg 2002-12-28 04:25:49
在VB中,我想实现一个功能,那就是程序退出就关机,用哪条语句呢?请大侠帮帮我吧!在线等待!!!!!
...全文
108 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
theMost 2003-01-11
  • 打赏
  • 举报
回复
Option Explicit

Dim mDattime As Date

Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As Long, ByVal dwReserved As Long) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, PreviousState As Long, ReturnLength As Long) As Long
Private Declare Function GetTokenInformation Lib "advapi32.dll" (ByVal TokenHandle As Long, ByVal TokenInformationClass As Long, TokenInformation As Any, ByVal TokenInformationLength As Long, ReturnLength As Long) As Long
Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, ByRef lpLuid As LARGE_INTEGER) As Long

Private Declare Function OpenProcessToken Lib "advapi32.dll" (ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) As Long
Private Declare Function GetCurrentProcessId Lib "kernel32" () As Long
Private Declare Function GetCurrentProcess Lib "kernel32" () As Long
Private Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

Private Const EWX_LOGOFF = 0 '中止进程,然后注销
Private Const EWX_POWEROFF = 8 '关掉系统电源
Private Const EWX_SHUTDOWN = 1 '关闭系统
Private Const EWX_REBOOT = 2 '重新引导系统
Private Const EWX_FORCE = 4 '强迫中止没有响应的进程
Private Const EWX_FORCEIFHUNG = 16 '如果应用程序已挂起,强制关闭

Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1
Private Const SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000

Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY_SOURCE = &H10
Private Const TOKEN_QUERY = &H8

Private Const ANYSIZE_ARRAY = 1

Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"

Private Const PROCESS_SET_INFORMATION = &H200
Private Const PROCESS_QUERY_INFORMATION = &H400

Private Type LARGE_INTEGER
lowpart As Long
highpart As Long
End Type

Private Type LUID
lowpart As Long
highpart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
pLuid As LUID
Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
PrivilegeCount As Long
Privileges(0) As LUID_AND_ATTRIBUTES
End Type

Dim Opt As Long

Private Sub Shutdown(Optional Mode As Long = EWX_POWEROFF)
Dim lAPIReturn As Long
Dim tTOKPRI As TOKEN_PRIVILEGES
Dim tLUID As LUID
Dim tLarInt As LARGE_INTEGER
Dim lRequired As Long
Dim hTokHan As Long
Dim hProcess As Long

If LookupPrivilegeValue(vbNullString, SE_SHUTDOWN_NAME, tLarInt) = 0 Then
MsgBox "Get LUID error!", vbOKOnly, "Error"
Exit Sub
End If

tLUID.highpart = tLarInt.highpart
tLUID.lowpart = tLarInt.lowpart

tTOKPRI.PrivilegeCount = 1
tTOKPRI.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
tTOKPRI.Privileges(0).pLuid = tLUID

hProcess = GetCurrentProcess()
lAPIReturn = OpenProcessToken(hProcess, TOKEN_ADJUST_PRIVILEGES + TOKEN_QUERY, hTokHan)
lAPIReturn = AdjustTokenPrivileges(hTokHan, 0, tTOKPRI, Len(tTOKPRI), 0, 0)
lAPIReturn = ExitWindowsEx(Mode, 0)
If lAPIReturn = 0 Then
MsgBox "Function calling error!", vbOKOnly, "错误"
End If

lAPIReturn = CloseHandle(hTokHan)
Unload Me
End Sub

////////////////////////////////////////////////

将:上面程序的常量定义部分的
Private Const EWX_POWEROFF = 8 '关掉系统电源
中的8改为5,6,8,7,9,10。。。。每一个都试一下。
保证有一个可以实现!
///////////////////////////////////////////////////////
浩子 2002-12-28
  • 打赏
  • 举报
回复
ExitWindowsEx
The ExitWindowsEx function either logs off the current user, shuts down the system, or shuts down and restarts the system. It sends the WM_QUERYENDSESSION message to all applications to determine if they can be terminated.

BOOL ExitWindowsEx(
UINT uFlags, // shutdown operation
DWORD dwReserved // reserved
);

Parameters
uFlags
Specifies the type of shutdown. This parameter must include one of the following values: Value Meaning
EWX_LOGOFF Shuts down all processes running in the security context of the process that called the ExitWindowsEx function. Then it logs the user off.
EWX_POWEROFF Shuts down the system and turns off the power. The system must support the power-off feature.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

EWX_REBOOT Shuts down the system and then restarts the system.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.

EWX_SHUTDOWN Shuts down the system to a point at which it is safe to turn off the power. All file buffers have been flushed to disk, and all running processes have stopped.
Windows NT: The calling process must have the SE_SHUTDOWN_NAME privilege. For more information, see the following Remarks section.



This parameter can optionally include the following values: Value Meaning
EWX_FORCE Forces processes to terminate. When this flag is set, the system does not send the WM_QUERYENDSESSION and WM_ENDSESSION messages. This can cause the applications to lose data. Therefore, you should only use this flag in an emergency.
EWX_FORCEIFHUNG Windows NT 5.0 and later: Forces processes to terminate if they do not respond to the WM_QUERYENDSESSION or WM_ENDSESSION message. This flag is ignored if EWX_FORCE is used.



dwReserved
Reserved; this parameter is ignored.
Return Values
If the function succeeds, the return value is nonzero.

If the function fails, the return value is zero. To get extended error information, callGetLastError.

Remarks
The ExitWindowsEx function returns as soon as it has initiated the shutdown. The shutdown or logoff then proceeds asynchronously.

During a shutdown or log-off operation, applications that are shut down are allowed a specific amount of time to respond to the shutdown request. If the time expires, the system displays a dialog box that allows the user to forcibly shut down the application, to retry the shutdown, or to cancel the shutdown request. If the EWX_FORCE value is specified, the system always forces applications to close and does not display the dialog box. If the EWX_FORCEIFHUNG value is specified, the system forces hung applications to close and does not display the dialog box.

Console processes receive a separate notification message, CTRL_SHUTDOWN_EVENT or CTRL_LOGOFF_EVENT, as the situation warrants. A console process routes these messages to its HandlerRoutine functions, which are added and removed by calls to the SetConsoleCtrlHandler function. ExitWindowsEx sends these notification messages asynchronously; thus, an application cannot assume that the console notification messages have been handled when a call to ExitWindowsEx returns.

Windows NT: To shut down or restart the system, the calling process must use theAdjustTokenPrivileges function to enable the SE_SHUTDOWN_NAME privilege. For more information about security privileges, seePrivileges.

Windows 95: ExitWindowEx does not work from a console application, as it does on Windows NT.

http://www.xygu.com

7,763

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧