imports system
imports system.text
imports system.diagnostics
imports 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, false)