关闭计算机就这几招(三) [转]

shanjuhua 2004-10-27 09:05:10
下面我们用vfp来实现它:)

shutdown.prg

*-- Define from Winnt.h
#DEFINE TOKEN_ADJUST_PRIVILEGES 0x0020
#DEFINE TOKEN_QUERY 0x0008
#DEFINE SE_PRIVILEGE_ENABLED 2

*-- Defines from Winbase.h
#DEFINE VER_PLATFORM_WIN32s 0
#DEFINE VER_PLATFORM_WIN32_WINDOWS 1
#DEFINE VER_PLATFORM_WIN32_NT 2

*-- Defines from WinUser.h
#DEFINE EWX_LOGOFF 0 && Shuts down all processes running
&& in the security
&& context of the process that
&& called the ExitWindowsEx function.
&& Then it logs the user off.

#DEFINE EWX_SHUTDOWN 1 && Shuts down the system to a point
&& at which it is safe to turn off the
&& power. All file buffers have been
&& flushed to disk, and all
&& running processes have stopped.

&& Windows NT/2000: The calling process
&& must have the SE_SHUTDOWN_NAME
&& privilege.

#DEFINE EWX_REBOOT 2 && Shuts down the system and then
&& restarts the system.

&& Windows NT/2000: The calling process
&& must have the SE_SHUTDOWN_NAME
&& privilege.

#DEFINE EWX_POWEROFF 8 && Shuts down the system and turns
&& off the power. The system must support
&& the power-off feature.

&& Windows NT/2000: The calling process
&& must have the SE_SHUTDOWN_NAME
&& privilege.

*-- Change EWX_SHUTDOWN to any of the above #define values to
*-- either logoff, shutdown, reboot, or power off.
DO ExitWindowsFox WITH EWX_SHUTDOWN

*----------------------------------
PROCEDURE ExitWindowsFox
*----------------------------------
PARAMETERS ExitMode
LOCAL iRc
iRc = 0
DECLARE ExitWindowsEx IN Win32Api INTEGER, INTEGER

*-- Check the OS version, and call the SetPrivilege function if NT/Win2K
liPlatform = GetPlatform()
IF liPlatform != VER_PLATFORM_WIN32_NT && We can do whatever we wish
&& without worrying about security
iRc = ExitWindowsEx(ExitMode, 0)
IF ExitMode = EWX_LOGOFF OR ExitMode = EWX_POWEROFF

*-- Necessary because VFP will not exit using
*-- either of these parameters under Win9x.
QUIT
ENDIF
ELSE && We have to set the process security
iRc = SetProcPrivilege()
IF iRc <> 0
iRc = ExitWindowsEx(ExitMode, 0)
ENDIF
ENDIF && liPlatform != VER_PLATFORM_WIN32_NT

*----------------------------------
PROCEDURE SetProcPrivilege
*-- Sets the appropriate process privilege to allow shutdown on NT/Win2K
*----------------------------------
*-- Declare function to obtain current Process ID, needed to open the process
*-- get the process token.
LOCAL iRc
iRc = 0
DECLARE INTEGER GetCurrentProcessId IN kernel32.DLL
DECLARE INTEGER OpenProcess IN Kernel32.DLL INTEGER, ;
INTEGER, ;
INTEGER
DECLARE INTEGER OpenProcessToken IN AdvApi32.DLL INTEGER, ;
INTEGER, ;
INTEGER@

*-- Declare function to retrieve a LUID for the necessary security
*-- privilege.
DECLARE INTEGER LookupPrivilegevalue IN AdvApi32.DLL STRING, ;
STRING, ;
INTEGER@ lsLuid

*-- Declare function to adjust the process token privileges so that
*-- we can shut down NT/Windows 2000
DECLARE INTEGER AdjustTokenPrivileges IN AdvApi32.DLL INTEGER, ;
INTEGER, ;
STRING@ lsNewState, ;
INTEGER, ;
INTEGER, ;
INTEGER

liAccessToken = 0 && Placeholder for the access token whose privileges we'll change
lsLuidBuffer = SPACE(8) && Placeholder for LUID used to change access privileges
lsName = SPACE(15) && Placeholder for computer name
liBufferLen = 15 && Placeholder for the computer name buffer length
liLuid = 0
liProc = 0
liProc = GetCurrentProcessId()
hProc = OpenProcess(2035711, 0, liProc)
iRc = OpenProcessToken(hProc, BITOR(TOKEN_ADJUST_PRIVILEGES, TOKEN_QUERY), @liAccessToken)
IF iRc <> 0
IF iRc <> 0

*-- "SeShutdownPrivilege" is the string value for the SE_SHUTDOWN_NAME value.
LookupPrivilegevalue("", "SeShutdownPrivilege", @liLuid)
lsLuidBuffer = LongToStr(liLuid) + CHR(0) + CHR(0) + CHR(0) + CHR(0)

*-- Declare a string to hold the TOKEN_PRIVILEGES structure
lsNewState = SPACE(16)

*-- Fill in the structure
lsNewState = LongToStr(1) + lsLuidBuffer + LongToStr(SE_PRIVILEGE_ENABLED)
iRc = AdjustTokenPrivileges(liAccessToken, 0, @lsNewState, LEN(lsNewState), 0, 0)
RETURN iRc
ELSE
RETURN iRc
ENDIF
ELSE
RETURN iRc
ENDIF
ENDPROC

*----------------------------------
PROCEDURE GetPlatform
*----------------------------------
LOCAL liPlatform, iRc
liPlatform = 0
iRc = 0

DECLARE INTEGER GetVersionEx IN Win32Api STRING@

*-- Declare a string to hold the OSVERSIONINFO structure
lsOSVersionInfo = LongToStr(148) + SPACE(144)
iRc = GetVersionEx(@lsOSVersionInfo)
liPlatform = StrToLong(SUBSTR(lsOSVersionInfo, 17, 4))
RETURN liPlatform
ENDPROC

*-- The following function converts a long integer to an ASCII
*-- character representation of the passed value in low-high format.
*----------------------------------
FUNCTION LongToStr
*----------------------------------
* Passed : 32-bit non-negative numeric value (lnLongval)
* Returns : ascii character representation of passed value in low-high
* format (lcRetstr)
* Example :
* m.long = "999999"
* m.longstr = long2str(m.long)

PARAMETERS lnLongval

PRIVATE i, lcRetstr

lcRetstr = ""
FOR i = 24 TO 0 STEP -8
lcRetstr = CHR(INT(lnLongval/(2^i))) + lcRetstr
lnLongval = MOD(lnLongval, (2^i))
NEXT
RETURN lcRetstr

*-- The following function converts a string in low-high format to a
*-- long integer.
*----------------------------------
FUNCTION StrToLong
*----------------------------------
* Passed: 4-byte character string (lcLongstr) in low-high ASCII format
* Returns: long integer value
* Example:
* m.longstr = "1111"
* m.longval = str2long(m.longstr)

PARAMETERS lcLongstr

PRIVATE i, lnRetval

lnRetval = 0
FOR i = 0 TO 24 STEP 8
lnRetval = lnRetval + (ASC(lcLongstr) * (2^i))
lcLongstr = RIGHT(lcLongstr, LEN(lcLongstr) - 1)
NEXT

RETURN lnRetval

(完)
...全文
48 1 打赏 收藏 举报
写回复
1 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
shanjuhua 2004-10-27
  • 打赏
  • 举报
回复
..
发帖
VFP

2574

社区成员

VFP,是Microsoft公司推出的数据库开发软件,用它来开发数据库,既简单又方便。
社区管理员
  • VFP社区
加入社区
帖子事件
创建了帖子
2004-10-27 09:05
社区公告
暂无公告