1

Konu: bilgisayarı kapatmak

vfp içinden bilgisayarı nasıl kapatırım?

Haksızlıklar karşısında susanlar, dilsiz şeytanlardır!
www.metinemre.com

2

Re: bilgisayarı kapatmak

* Kod: visualfoxpro

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

VFP9 SP2

3

Re: bilgisayarı kapatmak

kaynağını hatırlamıyorum, bulursam yazacağım

VFP9 SP2

4

Re: bilgisayarı kapatmak

teşekkürler. VFP code içine almaman çok iyi olmuş. çünkü VFP code bloğu 1.2.3.4. diye numaralar koyuyor. onları temizlemek sıkıntı oluyor (SOYKAN SOYKAN duy sesimiziiii)...

Haksızlıklar karşısında susanlar, dilsiz şeytanlardır!
www.metinemre.com

5

Re: bilgisayarı kapatmak

Alıntı yap diyerek kodlari copy-paste ile alabilirsiniz

6

Re: bilgisayarı kapatmak

Bunun daha kısa yolu yok mu?
Visual basicte Shell("rundll.exe user.exe,exitwindows") şeklinde bir komut var

7

Re: bilgisayarı kapatmak

konuka yazdı:

kaynağını hatırlamıyorum, bulursam yazacağım


http://support.microsoft.com/kb/251310

8

Re: bilgisayarı kapatmak

smentes yazdı:

Bunun daha kısa yolu yok mu?
Visual basicte Shell("rundll.exe user.exe,exitwindows") şeklinde bir komut var


Onun VB ile igisi yok, Shell'in normal komutunu cagiriyor. VFP'de run ya da ! (ya da ShellExecute). Onda kullanıcı hakkı kontrolu filan yok.

9

Re: bilgisayarı kapatmak

smentes yazdı:

Bunun daha kısa yolu yok mu?
Visual basicte Shell("rundll.exe user.exe,exitwindows") şeklinde bir komut var


Visual Fox Pro
strComputer="."

objWMIService=GetObject("winmgmts:{impersonationLevel=impersonate,(Shutdown)}!\\"+strComputer+"\root\cimv2")
colOperatingSystems=objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
    objOperatingSystem.Win32Shutdown(1)    &&Shut Down a Computer
    *objOperatingSystem.Reboot()                 &&Restart a Computer
Next

Bu kısa bir yol.

10

Re: bilgisayarı kapatmak

daha da kısalttım:

Visual Fox Pro
! %windir%\system32\shutdown.exe -s -f -t 0
VFP9 SP2