大雀软件园

首页 软件下载 安卓市场 苹果市场 电脑游戏 安卓游戏 文章资讯 驱动下载
技术开发 网页设计 图形图象 数据库 网络媒体 网络安全 站长CLUB 操作系统 媒体动画 安卓相关
当前位置: 首页 -> 技术开发 -> NET专区 -> VB.NET中实现关机/休眠/重启/注销的类

VB.NET中实现关机/休眠/重启/注销的类

时间: 2021-07-31 作者:daque

imports systemimports system.textimports system.diagnosticsimports system.runtime.interopservices public class windowscontroller public enum restartoptions logoff = 0 poweroff = 8 reboot = 2 shutdown = 1 suspend = -1 hibernate = -2 end enum public structure luid dim lowpart as integer dim highpart as integer end structure public structure luid_and_attributes dim pluid as luid dim attributes as integer end structure public structure token_privileges dim privilegecount as integer dim privileges as luid_and_attributes end structure private const token_adjust_privileges = &h20 private const token_query = &h8 private const se_privilege_enabled = &h2 private const format_message_from_system = &h1000 private const ewx_force = 4 declare function loadlibrary lib "kernel32" alias "loadlibrarya" (byval lplibfilename as string) as intptr declare function freelibrary lib "kernel32" (byval hlibmodule as intptr) as integer declare function getprocaddress lib "kernel32" (byval hmodule as intptr, byval lpprocname as string) as intptr declare function setsuspendstate lib "powrprof" (byval hibernate as integer, byval forcecritical as integer, byval disablewakeevent as integer) as integer declare function openprocesstoken lib "advapi32.dll" (byval processhandle as intptr, byval desiredaccess as integer, byref tokenhandle as intptr) as integer declare function lookupprivilegevalue lib "advapi32.dll" alias "lookupprivilegevaluea" (byval lpsystemname as string, byval lpname as string, byref lpluid as luid) as integer declare function adjusttokenprivileges lib "advapi32.dll" (byval tokenhandle as intptr, byval disableallprivileges as integer, byref newstate as token_privileges, byval bufferlength as integer, byref previousstate as token_privileges, byref returnlength as integer) as integer declare function exitwindowsex lib "user32" (byval uflags as integer, byval dwreserved as integer) as integer declare function formatmessage lib "kernel32" alias "formatmessagea" (byval dwflags as integer, byval lpsource as intptr, byval dwmessageid as integer, byval dwlanguageid as integer, byval lpbuffer as stringbuilder, byval nsize as integer, byval arguments as integer) as integer public sub exitwindows(byval how as restartoptions, byval force as boolean) select case how case restartoptions.suspend suspendsystem(false, force) case restartoptions.hibernate suspendsystem(true, force) case else exitwindows(convert.toint32(how), force) end select end sub protected sub exitwindows(byval how as integer, byval force as boolean) enabletoken("seshutdownprivilege") if force then how = how or ewx_force if (exitwindowsex(how, 0) = 0) then throw new privilegeexception(formaterror(marshal.getlastwin32error())) end sub protected sub enabletoken(byval privilege as string) if not checkentrypoint("advapi32.dll", "adjusttokenprivileges") then return dim tokenhandle as intptr = intptr.zero dim privilegeluid = new luid() dim newprivileges = new token_privileges() dim tokenprivileges as token_privileges if (openprocesstoken(process.getcurrentprocess().handle, token_adjust_privileges or token_query, tokenhandle)) = 0 then throw new privilegeexception(formaterror(marshal.getlastwin32error())) if (lookupprivilegevalue("", privilege, privilegeluid)) = 0 then throw new privilegeexception(formaterror(marshal.getlastwin32error())) tokenprivileges.privilegecount = 1 tokenprivileges.privileges.attributes = se_privilege_enabled tokenprivileges.privileges.pluid = privilegeluid dim size as integer = 4 if (adjusttokenprivileges(tokenhandle, 0, tokenprivileges, 4 + (12 * tokenprivileges.privilegecount), newprivileges, size)) = 0 then throw new privilegeexception(formaterror(marshal.getlastwin32error())) end sub protected sub suspendsystem(byval hibernate as boolean, byval force as boolean) if not checkentrypoint("powrprof.dll", "setsuspendstate") then throw new platformnotsupportedexception("the setsuspendstate method is not supported on this system!") setsuspendstate(convert.toint32(iif(hibernate, 1, 0)), convert.toint32(iif(force, 1, 0)), 0) end sub protected function checkentrypoint(byval library as string, byval method as string) as boolean dim libptr as intptr = loadlibrary(library) if not libptr.equals(intptr.zero) then if not getprocaddress(libptr, method).equals(intptr.zero) then freelibrary(libptr) return true end if freelibrary(libptr) end if return false end function protected function formaterror(byval number as integer) as string dim buffer = new stringbuilder(255) formatmessage(format_message_from_system, intptr.zero, number, 0, buffer, buffer.capacity, 0) return buffer.tostring() end function end class public class privilegeexception inherits exception public sub new()mybase.new() end sub public sub new(byval message as string) mybase.new(message) end sub end class比方实行刊出功效: dim shutdown as new windowscontroller() shutdown.exitwindows(shutdown.restartoptions.logoff, fa

热门阅览

最新排行

Copyright © 2019-2021 大雀软件园(www.daque.cn) All Rights Reserved.