VFP 愛用者社區 首頁 VFP 愛用者社區
本討論區為 Visual Foxpro 愛用者經驗交流的地方, 請多多利用"搜尋"的功能, 先查看看有無前例可循, 如果還有不懂的再發問. 部份主題有附加檔案, 須先註冊成為社區居民才可以下載.
 
 常見問題常見問題   搜尋搜尋   會員列表會員列表   會員群組會員群組   會員註冊會員註冊 
 個人資料個人資料   登入檢查您的私人訊息登入檢查您的私人訊息   登入登入

重新啟動Windows(轉貼)

 
發表新主題   回覆主題    VFP 愛用者社區 首頁 -> VFP 討論區
上一篇主題 :: 下一篇主題  
發表人 內容
Ruey



註冊時間: 2003-03-12
文章: 1698
來自: tunglo

第 1 樓

發表發表於: 星期六 五月 03, 2003 6:03 pm    文章主題: 重新啟動Windows(轉貼) 引言回覆

1.在VFP中重啟WinNT

#DEFINE EWX_REBOOT 2
#DEFINE EWX_FORCE 4
DECLARE INTEGER ExitWindowsEx IN WIN32API INTEGER flags, INTEGER reserved
lnRetVal = ExitWindowEx(EWX_REBOOT, 0)
如過ExitWindowEx函數執行失敗,lnRetVal將會返回0值。
為了保證這個重新啟動的動作是安全的,你的VFP程式需要有一定的程式來回應這個ShutDown事件。比如,你可以在程式中重新定義你的ON SHUTDOWN動作。
有一些應用程式對ExitWindowEx函數發出的WM_QUERYENDSESSION消息不與回應。這時候你需要在程式中加入
EWX_FORCE和EWX_REBOOT參數以迫使該程式對系統消息發生回應。
lnRetVal = ExitWindowEx(EWX_REBOOT+EWX_FORCE, 0)

2.在VFP中重啟Win9x

******************
*重新啟動Windows 9x
Function WindowsExit
******************
*!* 可用於關閉 windows, 重新啟動, 或以新口令登錄.
#Define EW_FORCE 0 && 強制系統關閉, 不管打開的應用程式
#Define EW_LOGOFF 1 && 登出當前用戶並顯示登錄螢幕
#Define EW_POWEROFF 2 && 退出 windows ,關閉系統
#Define EW_RESTART 3 && 重新啟動 windows
#Define EW_SHUTDOWN 4 && 退出 windows 到 "It's safe" 信息
* 定義API函數.
Declare Integer ExitWindowsEx IN WIN32API Integer, Integer
* 讓我們確信所有東西都是關閉了的.
CLOS ALL
= ExitWindowsEx(3,EW_FORCE)
*****************
回頂端
檢視會員個人資料 發送私人訊息
Ruey



註冊時間: 2003-03-12
文章: 1698
來自: tunglo

第 2 樓

發表發表於: 星期五 五月 16, 2003 11:00 pm    文章主題: 引言回覆

How can I shut down or reboot Windows for all versions?

Summary

ExitWindowsEx() shuts down, reboots or logs out from Windows, but by itself, is only able to log out the user under NT or Win2K because of process permission requirements. The following code will permit a VFP app to reboot or shut down Windows NT or Windows 2000 if the user now logged in is allowed to restart Windows through the normal Windows Shutdown dialog:

Description


*ShutdownWindowsAllVersions.PRG
LPARAMETERS tlShutdownRequested, tlInteractiveShutdown
* Default - reboot and force all apps to terminate without dialogs
*
* Parameters:
*
* tlShutdownRequested - .T. Shuts down Windows, .F. (default) reboots
* tlInteractiveShutdown - .T. Runs shutdown dialogs, .F. (default) forces exit

* This program will attempt to reboot Windows from within VFP; it will make the API call to make
* necessary privilege adjustments to permit the reboot to occur under Windows NT 4.0 or Windows 2000
* if it can. The function will return .F. if it is unable to make the required adjustment to the
* process token to permit the SE_SHUTDOWN_NAME privilege to be granted; it will force a reboot of
* the system under Win9x or if the privilege is granted to VFP by the OS. Tested under WinNT 4.0 SP6,
* Win2K Pro, Win98 and WinME. It should work for VFP 5.0 or 6.0
*

* Common constant definitions

#DEFINE SE_SHUTDOWN_NAME "SeShutdownPrivilege" && Privilege to shut down windows
#DEFINE SE_PRIVILEGE_ENABLED 2 && Enable privilege flag
#DEFINE TOKEN_QUERY 2 && Token may query status
#DEFINE TOKEN_ADJUST_PRIVILEGE 0x20 && Token may adjust privileges
#DEFINE EWX_SHUTDOWN 1
#DEFINE EWX_REBOOT 2 && Initiate a reboot
#DEFINE EWX_FORCE 4 && Force processes to close
#DEFINE SIZEOFTOKENPRIVILEGE 16

* Windows Shutdown API - all platforms
DECLARE ExitWindowsEx IN WIN32API INTEGER uFlags, INTEGER dwReserved && API call to shut down Windows

* Check the OS to see if we need to request the privilege
IF ('4.0' $ OS() OR '5.0' $ OS() OR 'NT' $ OS())
* API Calls below are NT/2K specific or only needed to manipulate process permissions

* Retrieve the LUID of a specific privilege name - changes each time Windows restarts
DECLARE SHORT LookupPrivilegeValue IN ADVAPI32 ;
INTEGER lpSystemName, ;
STRING @ lpPrivilegeName, ;
STRING @ pluid

* Get an hToken with specific permission sets for a given process handle
DECLARE SHORT OpenProcessToken IN Win32API ;
INTEGER hProcess, ;
INTEGER dwDesiredAccess, ;
INTEGER @ TokenHandle

* Alter the privileges granted to a Process via a specific hToken
DECLARE INTEGER AdjustTokenPrivileges IN ADVAPI32 ;
INTEGER hToken, ;
INTEGER bDisableAllPrivileges, ;
STRING @ NewState, ;
INTEGER dwBufferLen, ;
INTEGER PreviousState, ;
INTEGER @ pReturnLength

* Get the Process handle for this process
DECLARE INTEGER GetCurrentProcess IN WIN32API

LOCAL cLUID, nhToken, cTokenPrivs, nFlag

cLUID = REPL(CHR(0),Cool && 64 bit Locally Unique IDentifier for the Privilege

IF LookupPrivilegeValue(0, SE_SHUTDOWN_NAME, @cLUID) = 0
RETURN .F. && Privilege not defined for this process
ENDIF

nhToken = 0 && Process Token to be used to manipulate process privilege set

IF OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY + TOKEN_ADJUST_PRIVILEGE , @nhToken) = 0
RETURN .F. && OS will not grant handle with necessary rights
ENDIF

* Create a TOKEN_PRIVILEGES structure, a 4 byte DWORD indicating # permissions,
* followed by an array of 8 byte LUIDs and the 4 byte DWORD permission attributes
* for it. One privilege, the LUID retrieved for shutdown, enable attribute set
cTokenPrivs = CHR(1) + REPL(CHR(0),3) + cLUID + CHR(SE_PRIVILEGE_ENABLED) + REPL(CHR(0), 3)
IF AdjustTokenPrivileges(nhToken, 0, @cTokenPrivs, SIZEOFTOKENPRIVILEGE, 0, 0) = 0
RETURN .F. && Privilege denied
ENDIF
ENDIF

CLOSE ALL && Start shutting down VFP tables
FLUSH && Suggest that the OS flush all buffers
CLEAR EVENTS && Avoid VFP being obstinate
ON SHUTDOWN && don't try to run anything extra
* Select shutdown flags
DO CASE
CASE tlShutdownRequested AND tlInteractiveShutdown
nFlag = EWX_SHUTDOWN
CASE tlShutdownRequested
nFlag = EWX_SHUTDOWN + EWX_FORCE
CASE tlInteractiveShutdown
nFlag = EWX_REBOOT
OTHERWISE
nFlag = EWX_REBOOT + EWX_FORCE
ENDCASE
=ExitWindowsEx(nFlag, 0) && Force a reboot to be initiated
QUIT && Start an orderly shutdown of VFP

_________________
#############################
快樂媽咪系列幸福宅配,喝十全雞湯~原來幸福那麼簡單!!

學會VFP使用者社區的搜尋,Code才會更有趣~
#############################
回頂端
檢視會員個人資料 發送私人訊息
從之前的文章開始顯示:   
發表新主題   回覆主題    VFP 愛用者社區 首頁 -> VFP 討論區 所有的時間均為 台北時間 (GMT + 8 小時)
1頁(共1頁)

 
前往:  
無法 在這個版面發表文章
無法 在這個版面回覆文章
無法 在這個版面編輯文章
無法 在這個版面刪除文章
無法 在這個版面進行投票
無法 在這個版面附加檔案
無法 在這個版面下載檔案


Powered by phpBB © 2001, 2005 phpBB Group
正體中文語系由 phpbb-tw 維護製作