View Full Version : ExitwindowsEx

جمعه 27 تیر 1382, 21:03 عصر
سلام بچه ها یه مثال راجه به تابع ExitwindowsEx برای vb.net میخوام یا یه لینک یا هر راهنمایی دیگه البته با قدرت انتخاب برای هر نسخه از ویندوز
با تشکر

شنبه 11 مرداد 1382, 16:19 عصر
با سلام خدمت برنامه نویسان !
آقا یکی نبود یه تحقیق یه توضیحی راجب طرز کار اون در دات نت بده چون با وی بی 6 فرق میکنه

بعداٌ میگین چرا بخش دلفی فعاله خوب معلومه وقتی کسی سوالی جواب نمی ده

من خودم شخصا یکی دو روز رو اینترنت گشتم نتونستم مثالی پیدا کنم و گرنه مزاحم شما دوستان نمی شدم

با تشکر

Abbas Arizi
شنبه 11 مرداد 1382, 16:31 عصر
اتفاقا همون روزی که شما گفتید من سعی کردم یه سری کد نمونه پیدا کنم ولی متاسفانه موفق نشدم.
به هر حال نکته مهم اینه که اگه از این تابع بخواهید برای ShutDown یا Restart استفاده کنید این تابع توی ویندوزهای NT جواب نمیده و باید چندتا تابع دیگه رو هم استفاده کنید.

شنبه 11 مرداد 1382, 18:58 عصر
با سلام خدمت دوستان
عباس جان گشتم پیدا شد گفتم جویندگان علم در جریان باشند
با تشکر شهریار
ExitWindowsEx provides the ability for an application to log off the current user, shut down the system, shut down and restart Windows, or shut down and power off the machine (where supported by the system).
This demo shows how to call this API, how to set the appropriate flags, and how to call AdjustTokenPrivileges in order to allow the current process sufficient rights to execute ExitWindowsEx under NT/2000/XP. Win9x systems do not call AdjustTokenPrivileges, so to use the code on a 9x box you can just add an Else condition to the Command1_Click code and call the API directly, or create another wrapper allowing you to specify the flags etc.

ExitWindowsEx can be unforgiving. You may have open apps with unsaved changes that would normally prompt for file saving, and where normally pressing the Save dialog's Cancel button would terminate the shutdown. However, in performing the shutdown, Windows chooses the shutdown "order" of open apps and services in response to the selected event, therefore the exact shutdown order can not be predicted. This can wreck havoc. In development tests for this demo using a dirty Notepad document containing unsaved text, the system correctly cancelled the shutdown when Save dialog's Cancel was pressed, but on more than one occasion, because of the unpredictable order of the shutdown I ended up loosing some apps running as services, most frequently my Intellipoint mouse driver, because the shutdown order caused those to close before ExitWindowsEx caused Notepad to present its Save dialog allowing the Cancel. Therefore, I recommend you don't call this API unless you really intend to shut down the system, and only call once all cleanup within your own app has already performed.

One more caveat .... should the FORCE flag be specified in the flags parameter, pressing Cancel in response to a Save prompt following the ExitWindowsEx call does nothing - it does not not prevent the app from terminating, nor does it prevent the shutdown/logoff/reboot action from taking place. In other words, do not use Force unless you can truly afford to loose anything currently open and unsaved

Add a command button (Command1) and four option buttons (Option1 - Option4), as well as two check boxed (Check1, Check2), and the following code:


Option Explicit
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Copyright ©1996-2003 VBnet, Randy Birch, All Rights Reserved.
' Some pages may also contain other copyrights by the author.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
' Distribution: You can freely use this code in your own
' applications, but you may not reproduce
' or publish this code on any web site,
' online service, or distribute as source
' on any media without express permission.
'''''''''''''''''''''''''''''''''''''''''''''''''' ''''''''''''''
Private Const TOKEN_ADJUST_PRIVILEGES As Long = &H20
Private Const TOKEN_QUERY As Long = &H8
Private Const SE_PRIVILEGE_ENABLED As Long = &H2

Private Const EWX_LOGOFF As Long = &H0
Private Const EWX_SHUTDOWN As Long = &H1
Private Const EWX_REBOOT As Long = &H2
Private Const EWX_FORCE As Long = &H4
Private Const EWX_POWEROFF As Long = &H8
Private Const EWX_FORCEIFHUNG As Long = &H10 '2000/XP only

Private Const VER_PLATFORM_WIN32_NT As Long = 2

OSVSize As Long
dwVerMajor As Long
dwVerMinor As Long
dwBuildNumber As Long
PlatformID As Long
szCSDVersion As String * 128
End Type

Private Type LUID
dwLowPart As Long
dwHighPart As Long
End Type

dwAttributes As Long
End Type

PrivilegeCount As Long
End Type

Private Declare Function ExitWindowsEx Lib "user32" _
(ByVal dwOptions As Long, _
ByVal dwReserved As Long) As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function OpenProcessToken Lib "advapi32" _
(ByVal ProcessHandle As Long, _
ByVal DesiredAccess As Long, _
TokenHandle As Long) As Long

Private Declare Function LookupPrivilegeValue Lib "advapi32" _
Alias "LookupPrivilegeValueA" _
(ByVal lpSystemName As String, _
ByVal lpName As String, _
lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32" _
(ByVal TokenHandle As Long, _
ByVal DisableAllPrivileges As Long, _
ByVal BufferLength As Long, _
PreviousState As Any, _
ReturnLength As Long) As Long

Private Declare Function GetVersionEx Lib "kernel32" _
Alias "GetVersionExA" _
(lpVersionInformation As OSVERSIONINFO) As Long

Private Sub Command1_Click()

Dim uflags As Long
Dim success As Long

If Option1.Value = True Then uflags = EWX_LOGOFF
If Option2.Value = True Then uflags = EWX_SHUTDOWN
If Option3.Value = True Then uflags = EWX_REBOOT
If Option4.Value = True Then uflags = EWX_POWEROFF

If Check1.Value = vbChecked Then uflags = uflags Or EWX_FORCE
If Check2.Value = vbChecked Then uflags = uflags Or EWX_FORCEIFHUNG

'assume success
success = True

'if running under NT or better,
'the shutdown privledges need to
'be adjusted to allow the ExitWindowsEx
'call. If the adjust call fails on a NT+
'system, success holds False, preventing shutdown.
If IsWinNTPlus Then
success = EnableShutdownPrivledges()
End If

If success Then Call ExitWindowsEx(uflags, 0&)

End Sub

Private Function IsWinNTPlus() As Boolean

'returns True if running Windows NT,
'Windows 2000, Windows XP, or .net server
#If Win32 Then


OSV.OSVSize = Len(OSV)

If GetVersionEx(OSV) = 1 Then

IsWinNTPlus = (OSV.PlatformID = VER_PLATFORM_WIN32_NT) And _
(OSV.dwVerMajor >= 4)
End If

#End If

End Function

Private Function EnableShutdownPrivledges() As Boolean

Dim hProcessHandle As Long
Dim hTokenHandle As Long
Dim lpv_la As LUID

hProcessHandle = GetCurrentProcess()

If hProcessHandle <> 0 Then

'open the access token associated
'with the current process. hTokenHandle
'returns a handle identifying the
'newly-opened access token
If OpenProcessToken(hProcessHandle, _
hTokenHandle) <> 0 Then

'obtain the locally unique identifier
'(LUID) used on the specified system
'to locally represent the specified
'privilege name. Passing vbNullString
'causes the api to attempt to find
'the privilege name on the local system.
If LookupPrivilegeValue(vbNullString, _
"SeShutdownPrivilege", _
lpv_la) <> 0 Then

'TOKEN_PRIVILEGES contains info about
'a set of privileges for an access token.
'Prepare the TOKEN_PRIVILEGES structure
'by enabling one privilege.
With token
.PrivilegeCount = 1
.laa.udtLUID = lpv_la
.laa.dwAttributes = SE_PRIVILEGE_ENABLED
End With

'Enable the shutdown privilege in
'the access token of this process.
'hTokenHandle: access token containing the
' privileges to be modified
'DisableAllPrivileges: if True the function
' disables all privileges and ignores the
' NewState parameter. If FALSE, the
' function modifies privileges based on
' the information pointed to by NewState.
'token: TOKEN_PRIVILEGES structure specifying
' an array of privileges and their attributes.
'Since were just adjusting to shut down,
'BufferLength, PreviousState and ReturnLength
'can be passed as null.
If AdjustTokenPrivileges(hTokenHandle, _
False, _
token, _
ByVal 0&, _
ByVal 0&, _
ByVal 0&) <> 0 Then

'success, so return True
EnableShutdownPrivledges = True

End If 'AdjustTokenPrivileges
End If 'LookupPrivilegeValue
End If 'OpenProcessToken
End If 'hProcessHandle

End Function
'--end block--'

rezaei manesh
دوشنبه 22 فروردین 1384, 14:10 عصر
آقا خوب بود اما فقط shutdown -Restart می کنه
بعد این به زبان vb6
دات نتش رو چه کار باید بکنم
با تشکر حامد :تشویق: :flower:

دوشنبه 22 فروردین 1384, 22:51 عصر
تقریبا" دو سال پیش که من پرسیدم روی شبکه مثالی از دات نت نبود ولی الان با یه سرچ توی گوگل صد تا مثال پیدا میکنی