时间: 2021-07-31 作者:daque
在windows nt下,你能强迫当地或长途呆板准时封闭。这段代码将报告你如何做。你能指定体例封闭前的等候功夫(0代办登时封闭),封闭过程的优先级(确定能否承诺生存未实行的处事)和呆板能否要从新启用。 发端一个新的project,介入一个module,而后介入一下代码: '确定体例能否为nt: private type osversioninfo dwosversioninfosize as long dwmajorversion as long dwminorversion as long dwbuildnumber as long dwplatformid as long szcsdversion as string * 128 ' maintenance string for pss usage end type private declare function getversionex lib "kernel32" alias "getversionexa" (lpversioninformation as osversioninfo) as long private const ver_platform_win32_nt = 2 private const ver_platform_win32_windows = 1 private const ver_platform_win32s = 0 '汇报api缺点: private const format_message_allocate_buffer = &h100 private const format_message_argument_array = &h2000 private const format_message_from_hmodule = &h800 private const format_message_from_string = &h400 private const format_message_from_system = &h1000 private const format_message_ignore_inserts = &h200 private const format_message_max_width_mask = &hff private declare function formatmessage lib "kernel32" alias "formatmessagea" (byval dwflags as long, lpsource as any, byval dwmessageid as long, byval dwlanguageid as long, byval lpbuffer as string, byval nsize as long, arguments as long) as long ' ===================================================================== ' nt only 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 to 0) as luid_and_attributes end type private declare function getcurrentprocess lib "kernel32" () 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 closehandle lib "kernel32" (byval hobject as long) as long private declare function gettokeninformation lib "advapi32.dll" (byval tokenhandle as long, tokeninformationclass as integer, tokeninformation as any, byval tokeninformationlength as long, returnlength 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 token_privileges, returnlength as long) as long private declare function lookupprivilegevalue lib "advapi32.dll" alias "lookupprivilegevaluea" (byval lpsystemname as string, byval lpname as string, lpluid as luid) as long private const se_shutdown_name = "seshutdownprivilege" private const se_privilege_enabled = &h2 private const read_control = &h20000 private const standard_rights_all = &h1f0000 private const standard_rights_execute = (read_control) private const standard_rights_read = (read_control) private const standard_rights_required = &hf0000 private const standard_rights_write = (read_control) private const token_assign_primary = &h1 private const token_duplicate = (&h2) private const token_impersonate = (&h4) private const token_query = (&h8) private const token_query_source = (&h10) private const token_adjust_privileges = (&h20) private const token_adjust_groups = (&h40) private const token_adjust_default = (&h80) private const token_all_access = (standard_rights_required or _ token_assign_primary or _ token_duplicate or _ token_impersonate or _ token_query or _ token_query_source or _ token_adjust_privileges or _ token_adjust_groups or _ token_adjust_default) private const token_read = (standard_rights_read or token_query) private const token_write = (standard_rights_write or _ token_adjust_privileges or _ token_adjust_groups or _ token_adjust_default) private const token_execute = (standard_rights_execute) private const tokendefaultdacl = 6 private const tokengroups = 2 private const tokenimpersonationlevel = 9 private const tokenowner = 4 private const tokenprimarygroup = 5 private const tokenprivileges = 3 private const tokensource = 7 private const tokenstatistics = 10 private const tokentype = 8 private const tokenuser = 1 private declare function initiatesystemshutdown lib "advapi32.dll" alias "initiatesystemshutdowna" (byval lpmachinename as string, byval lpmessage as string, byval dwtimeout as long, byval bforceappsclosed as long, byval brebootaftershutdown as long) as long private declare function abortsystemshutdown lib "advapi32.dll" alias "abortsystemshutdowna" (byval lpmachinename as string) as long ' ================================================================ public function winerror(byval llastdllerror as long) as string dim sbuff as string dim lcount as long '归来与lastdllerror关系的缺点动静: sbuff = string$(256, 0) lcount = formatmessage(format_message_from_system or format_message_ignore_inserts, _ 0, llastdllerror, 0&, sbuff, len(sbuff), byval 0) if lcount then winerror = left$(sbuff, lcount) end if end function public function isnt() as boolean static bonce as boolean static bvalue as boolean '归来体例能否为nt: if not (bonce) then dim tvi as osversioninfo tvi.dwosversioninfosize = len(tvi) if (getversionex(tvi) <> 0) then bvalue = (tvi.dwplatformid = ver_platform_win32_nt) bonce = true end if end if isnt = bvalue end function private function ntenableshutdown(byref smsg as string) as boolean dim tluid as luid dim hprocess as long dim htoken as long dim ttp as token_privileges, ttpold as token_privileges dim ltpold as long dim lr as long '在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_privileges or token_query, htoken) if (lr <> 0) then '好,咱们此刻不妨安排shutdown特权了: with ttp .privilegecount = 1 with .privileges(0) .attributes = se_privilege_enabled .pluid.highpart = tluid.highpart .pluid.lowpart = tluid.lowpart end with end with '此刻承诺这个过程封闭体例: lr = adjusttokenprivileges(htoken, 0, ttp, len(ttp), ttpold, ltpold) if (lr <> 0) then ntenableshutdown = true else err.raise eessderrorbase + 6, app.exename & ".mshutdown", "不许shutdown:你没相关闭本体例的权力。[" & winerror(err.lastdllerror) & "]" end if '牢记用完后封闭这个句柄: closehandle htoken else err.raise eessderrorbase + 6, app.exename & ".mshutdown", "不许shutdown:你没相关闭本体例的权力。[" & winerror(err.lastdllerror) & "]" end if else err.raise eessderrorbase + 5, app.exename & ".mshutdown", "不许shutdown:不许中断当进步程。[" & winerror(err.lastdllerror) & "]" end if else err.raise eessderrorbase + 4, app.exename & ".mshutdown", "不许shutdown:找不到se_shutdown_name特权值。[" & winerror(err.lastdllerror) & "]" end if end function public function ntforcetimedshutdown( _ optional byval ltimeout as long = -1, _ optional byval smsg as string = "", _ optional byval smachinenetworkname as string = vbnullstring, _ optional byval bforceappstoclose as boolean = false, _ optional byval breboot as boolean = false _ ) as boolean dim lr as long if isnt then '即使咱们在nt下,坚信咱们仍旧给了这个过程封闭体例的特权: if not (ntenableshutdown(smsg)) then exit function end if '这是准时封闭体例的代码: lr = initiatesystemshutdown(smachinenetworkname, smsg, ltimeout, bforceappstoclose, breboot) if (lr = 0) then err.raise eessderrorbase + 2, app.exename & ".mshutdown", "initiatesystemshutdown failed: " & winerror(err.lastdllerror) end if else err.raise eessderrorbase + 1, app.exename & ".mshutdown", "因变量仅在windows nt下灵验。" end if end function public function ntaborttimedshutdown(optional byval smachinenetworkname as string = vbnullstring) abortsystemshutdown smachinenetworkname end function 为了考查一次shutdown,在窗体上放两个command按钮和一个text,而后粘贴一下代码。提防,你必需在运转前生存你的处事,由于shutdown也将封闭vb且不会给你任何相关生存的咨询! 点击command1,它按照text里的值发端一次准时关灯。要中断shutdown,点command2。 private sub command1_click() if (msgbox("你决定要强迫准时关灯吗?", vbyesno or vbquestion) = vbyes) then ntforcetimedshutdown clng(text1.text), "体例将在" & text1.text & "秒后封闭..." end if end sub private sub command2_click() ntaborttimedshutdown end sub private sub form_load() text1.text = 60 end sub