Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Program a workstation restart

Status
Not open for further replies.

AlanArons

Programmer
Aug 1, 2002
91
US
Hi all,

Does anyone know a way to program a workstation restart? I have a routine that runs at night and works great as long as nothing gets in its way. I would like to be able to restart the workstation in certain situations?

Thanks for your help

Alan Arons [ponder]
 
try this, I have never tried it so you may have to tweek:

Code:
*!* This routine will shutdown, reboot, logoff, suspend or other

*!* Parameter nExitMode
*!*  0 = LogOff
*!*  1 = Shutdown with Manual power off
*!*  2 = Reboot the system
*!*  3 = Power Off (if supported)

*!* Return Success level > 0 = success


Function Kill(nExitMode)
	*!* Much of this routine is copied from the MSKB

	*-- 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 mustsupport
	*!*                                 && the power-off feature.

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


	*!* DO ExitWindowsFox WITH EWX_SHUTDOWN

	*!* Cleanup will have to save all varibles as well
	Do CLEANUP  && clean up Fox environment before shutting down

	Do ExitWindowsFox With nExitMode


	*----------------------------------
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
Endproc

*----------------------------------
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_NAMEvalue.
			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
Endfunc

*-- 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

	******************* or
	#Define EWX_LOGOFF        0
	#Define EWX_SHUTDOWN      1
	#Define EWX_REBOOT        2
	#Define EWX_FORCE         4
	#Define EWX_POWEROFF      8
	#Define EWX_FORCEIFHUNG  16

	Declare Integer ExitWindows In user32;
		INTEGER dwReserved, Integer uReturnCode

	Declare Integer ExitWindowsEx In user32;
		INTEGER uFlags, Integer dwReserved

	* logoff -- change user
	= ExitWindowsEx (EWX_LOGOFF, 0)

	* reboot
	= ExitWindowsEx (EWX_REBOOT, 0)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top