国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

VB.NET中實現(xiàn)關(guān)機(jī)/休眠/重啟/注銷的類

2024-07-10 13:02:39
字體:
供稿:網(wǎng)友
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

例如實現(xiàn)注銷功能:
dim shutdown as new windowscontroller()
shutdown.exitwindows(shutdown.restartoptions.logoff, false)
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 兴安县| 炎陵县| 长乐市| 忻城县| 蓝田县| 宁波市| 宝丰县| 新干县| 西藏| 梅河口市| 龙井市| 银川市| 新化县| 德令哈市| 晋州市| 承德市| 偏关县| 仁怀市| 章丘市| 当涂县| 隆尧县| 汽车| 夏邑县| 托克逊县| 广安市| 惠州市| 昌都县| 林周县| 襄汾县| 敖汉旗| 将乐县| 衡山县| 庐江县| 合肥市| 义乌市| 博湖县| 苏州市| 蒙自县| 永德县| 乳源| 交口县|