怎么用VB编写WINDOWS XP 关机程序
发布网友
发布时间:2022-04-23 02:41
我来回答
共3个回答
热心网友
时间:2022-04-22 12:46
如果系统是XP的话,你可以用这个语句:
shell"shutdown-r-t"&text1.text'&"-c"&text2.text
text1.text就是你要设置的关机时间,单位为秒,text2.text是你的关机提示.不想关机的话,就运行"shutdown-a"就行了.
如果你倒计时间是固定的话(比如30秒),可以用以下的:
shell"shutdown-r-t30"
如果说别的系统的话,这里有一个模块,你自己调用吧
PrivateTypeOSVERSIONINFO
dwOSVersionInfoSizeAsLong
dwMajorVersionAsLong
dwMinorVersionAsLong
dwBuildNumberAsLong
dwPlatformIdAsLong
szCSDVersionAsString*128'MaintenancestringforPSSusage
EndType
PrivateDeclareFunctionGetVersionExLib"kernel32"Alias"GetVersionExA"(lpVersionInformationAsOSVERSIONINFO)AsLong
PrivateConstVER_PLATFORM_WIN32_NT=2
PrivateConstVER_PLATFORM_WIN32_WINDOWS=1
PrivateConstVER_PLATFORM_WIN32s=0
'报告API错误:
PrivateConstFORMAT_MESSAGE_ALLOCATE_BUFFER=&H100
PrivateConstFORMAT_MESSAGE_ARGUMENT_ARRAY=&H2000
PrivateConstFORMAT_MESSAGE_FROM_HMODULE=&H800
PrivateConstFORMAT_MESSAGE_FROM_STRING=&H400
PrivateConstFORMAT_MESSAGE_FROM_SYSTEM=&H1000
PrivateConstFORMAT_MESSAGE_IGNORE_INSERTS=&H200
PrivateConstFORMAT_MESSAGE_MAX_WIDTH_MASK=&HFF
PrivateDeclareFunctionFormatMessageLib"kernel32"Alias"FormatMessageA"(ByValdwFlagsAsLong,lpSourceAsAny,ByValdwMessageIdAsLong,ByValdwLanguageIdAsLong,ByVallpBufferAsString,ByValnSizeAsLong,ArgumentsAsLong)AsLong
'=====================================================================
'NTOnly
PrivateTypeLARGE_INTEGER
LowPartAsLong
HighPartAsLong
EndType
PrivateTypeLUID
LowPartAsLong
HighPartAsLong
EndType
PrivateTypeLUID_AND_ATTRIBUTES
pLuidAsLUID
AttributesAsLong
EndType
PrivateTypeTOKEN_PRIVILEGES
PrivilegeCountAsLong
Privileges(0To0)AsLUID_AND_ATTRIBUTES
EndType
PrivateDeclareFunctionGetCurrentProcessLib"kernel32"()AsLong
PrivateDeclareFunctionOpenProcessTokenLib"advapi32.dll"(ByValProcessHandleAsLong,ByValDesiredAccessAsLong,TokenHandleAsLong)AsLong
PrivateDeclareFunctionCloseHandleLib"kernel32"(ByValhObjectAsLong)AsLong
PrivateDeclareFunctionGetTokenInformationLib"advapi32.dll"(ByValTokenHandleAsLong,TokenInformationClassAsInteger,TokenInformationAsAny,ByValTokenInformationLengthAsLong,ReturnLengthAsLong)AsLong
PrivateDeclareFunctionAdjustTokenPrivilegesLib"advapi32.dll"(ByValTokenHandleAsLong,ByValDisableAllPrivilegesAsLong,NewStateAsTOKEN_PRIVILEGES,ByValBufferLengthAsLong,PreviousStateAsTOKEN_PRIVILEGES,ReturnLengthAsLong)AsLong
PrivateDeclareFunctionLookupPrivilegeValueLib"advapi32.dll"Alias"LookupPrivilegeValueA"(ByVallpSystemNameAsString,ByVallpNameAsString,lpLuidAsLUID)AsLong
PrivateConstSE_SHUTDOWN_NAME="SeShutdownPrivilege"
PrivateConstSE_PRIVILEGE_ENABLED=&H2
PrivateConstREAD_CONTROL=&H20000
PrivateConstSTANDARD_RIGHTS_ALL=&H1F0000
PrivateConstSTANDARD_RIGHTS_EXECUTE=(READ_CONTROL)
PrivateConstSTANDARD_RIGHTS_READ=(READ_CONTROL)
PrivateConstSTANDARD_RIGHTS_REQUIRED=&HF0000
PrivateConstSTANDARD_RIGHTS_WRITE=(READ_CONTROL)
PrivateConstTOKEN_ASSIGN_PRIMARY=&H1
PrivateConstTOKEN_DUPLICATE=(&H2)
PrivateConstTOKEN_IMPERSONATE=(&H4)
PrivateConstTOKEN_QUERY=(&H8)
PrivateConstTOKEN_QUERY_SOURCE=(&H10)
PrivateConstTOKEN_ADJUST_PRIVILEGES=(&H20)
PrivateConstTOKEN_ADJUST_GROUPS=(&H40)
PrivateConstTOKEN_ADJUST_DEFAULT=(&H80)
PrivateConstTOKEN_ALL_ACCESS=(STANDARD_RIGHTS_REQUIREDOr_
TOKEN_ASSIGN_PRIMARYOr_
TOKEN_DUPLICATEOr_
TOKEN_IMPERSONATEOr_
TOKEN_QUERYOr_
TOKEN_QUERY_SOURCEOr_
TOKEN_ADJUST_PRIVILEGESOr_
TOKEN_ADJUST_GROUPSOr_
TOKEN_ADJUST_DEFAULT)
PrivateConstTOKEN_READ=(STANDARD_RIGHTS_READOrTOKEN_QUERY)
PrivateConstTOKEN_WRITE=(STANDARD_RIGHTS_WRITEOr_
TOKEN_ADJUST_PRIVILEGESOr_
TOKEN_ADJUST_GROUPSOr_
TOKEN_ADJUST_DEFAULT)
PrivateConstTOKEN_EXECUTE=(STANDARD_RIGHTS_EXECUTE)
PrivateConstTokenDefaultDacl=6
PrivateConstTokenGroups=2
PrivateConstTokenImpersonationLevel=9
PrivateConstTokenOwner=4
PrivateConstTokenPrimaryGroup=5
PrivateConstTokenPrivileges=3
PrivateConstTokenSource=7
PrivateConstTokenStatistics=10
PrivateConstTokenType=8
PrivateConstTokenUser=1
PrivateDeclareFunctionInitiateSystemShutdownLib"advapi32.dll"Alias"InitiateSystemShutdownA"(ByVallpMachineNameAsString,ByVallpMessageAsString,ByValdwTimeoutAsLong,ByValbForceAppsClosedAsLong,ByValbRebootAfterShutdownAsLong)AsLong
PrivateDeclareFunctionAbortSystemShutdownLib"advapi32.dll"Alias"AbortSystemShutdownA"(ByVallpMachineNameAsString)AsLong
'================================================================
PublicFunctionWinError(ByVallLastDLLErrorAsLong)AsString
DimsBuffAsString
DimlCountAsLong
'返回与LastDLLError关联的错误消息:
sBuff=String$(256,0)
lCount=FormatMessage(FORMAT_MESSAGE_FROM_SYSTEMOrFORMAT_MESSAGE_IGNORE_INSERTS,_
0,lLastDLLError,0&,sBuff,Len(sBuff),ByVal0)
IflCountThen
WinError=Left$(sBuff,lCount)
EndIf
EndFunction
PublicFunctionIsNT()AsBoolean
StaticbOnceAsBoolean
StaticbValueAsBoolean
'返回系统是否为NT:
IfNot(bOnce)Then
DimtVIAsOSVERSIONINFO
tVI.dwOSVersionInfoSize=Len(tVI)
If(GetVersionEx(tVI)<0)Then
bValue=(tVI.dwPlatformId=VER_PLATFORM_WIN32_NT)
bOnce=True
EndIf
EndIf
IsNT=bValue
EndFunction
PrivateFunctionNTEnableShutDown(ByRefsMsgAsString)AsBoolean
DimtLUIDAsLUID
DimhProcessAsLong
DimhTokenAsLong
DimtTPAsTOKEN_PRIVILEGES,tTPOldAsTOKEN_PRIVILEGES
DimlTpOldAsLong
DimlRAsLong
'在NT下,我们必须给试图关闭系统的进程SE_SHUTDOWN_NAME特权
'否则,所有企图关闭系统的调用都会无效!
'寻找Shoudown特权令牌的LUID:
lR=LookupPrivilegeValue(vbNullString,SE_SHUTDOWN_NAME,tLUID)
'如果我们找到了
If(lR<0)Then
'取得当前进程的句柄:
hProcess=GetCurrentProcess()
If(hProcess<0)Then
'打开令牌来Adjust和Query(用户可能没有权限)
lR=OpenProcessToken(hProcess,TOKEN_ADJUST_PRIVILEGESOrTOKEN_QUERY,hToken)
If(lR<0)Then
'好,我们现在可以调整Shutdown特权了:
WithtTP
.PrivilegeCount=1
With.Privileges(0)
.Attributes=SE_PRIVILEGE_ENABLED
.pLuid.HighPart=tLUID.HighPart
.pLuid.LowPart=tLUID.LowPart
EndWith
EndWith
'现在允许这个进程关闭系统:
lR=AdjustTokenPrivileges(hToken,0,tTP,Len(tTP),tTPOld,lTpOld)
If(lR<0)Then
NTEnableShutDown=True
Else
Err.RaiseeeSSDErrorBase 6,App.EXEName&".mShutDown","不能shutdown:你没有关闭本系统的权限。["&WinError(Err.LastDllError)&"]"
EndIf
'记得用完后关闭这个句柄:
CloseHandlehToken
Else
Err.RaiseeeSSDErrorBase 6,App.EXEName&".mShutDown","不能shutdown:你没有关闭本系统的权限。["&WinError(Err.LastDllError)&"]"
EndIf
Else
Err.RaiseeeSSDErrorBase 5,App.EXEName&".mShutDown","不能shutdown:不能终止当前进程。["&WinError(Err.LastDllError)&"]"
EndIf
Else
Err.RaiseeeSSDErrorBase 4,App.EXEName&".mShutDown","不能shutdown:找不到SE_SHUTDOWN_NAME特权值。["&WinError(Err.LastDllError)&"]"
EndIf
EndFunction
PublicFunctionNTForceTimedShutdown(_
OptionalByVallTimeOutAsLong=-1,_
OptionalByValsMsgAsString="",_
OptionalByValsMachineNetworkNameAsString=vbNullString,_
OptionalByValbForceAppsToCloseAsBoolean=False,_
OptionalByValbRebootAsBoolean=False_
)AsBoolean
DimlRAsLong
IfIsNTThen
'如果我们在NT下,确信我们已经给了这个进程关闭系统的特权:
IfNot(NTEnableShutDown(sMsg))Then
ExitFunction
EndIf
'这是定时关闭系统的代码:
lR=InitiateSystemShutdown(sMachineNetworkName,sMsg,lTimeOut,bForceAppsToClose,bReboot)
If(lR=0)Then
Err.RaiseeeSSDErrorBase 2,App.EXEName&".mShutDown","InitiateSystemShutdownfailed:"&WinError(Err.LastDllError)
EndIf
Else
Err.RaiseeeSSDErrorBase 1,App.EXEName&".mShutDown","函数仅在WindowsNT下有效。"
EndIf
EndFunction
PublicFunctionNTAbortTimedShutdown(OptionalByValsMachineNetworkNameAsString=vbNullString)
AbortSystemShutdownsMachineNetworkName
EndFunction
热心网友
时间:2022-04-22 14:04
关机可以调用系统的API,
用法: shutdown [-i | -l | -s | -r | -a] [-f] [-m \\computername] [-t xx] [-c "co
mment"] [-d up:xx:yy]
没有参数 显示此消息(与 ? 相同)
-i 显示 GUI 界面,必须是第一个选项
-l 注销(不能与选项 -m 一起使用)
-s 关闭此计算机
-r 关闭并重启动此计算机
-a 放弃系统关机
-m \\computername 远程计算机关机/重启动/放弃
-t xx 设置关闭的超时为 xx 秒
-c "comment" 关闭注释(最大 127 个字符)
-f 强制运行的应用程序关闭而没有警告
-d [u][p]:xx:yy 关闭原因代码
u 是用户代码
p 是一个计划的关闭代码
xx 是一个主要原因代码(小于 256 的正整数)
yy 是一个次要原因代码(小于 65536 的正整数)
热心网友
时间:2022-04-22 15:39
什么时候能成这样的高手啊