IonicWind Software

IWBasic => General Questions => Topic started by: chris on July 16, 2009, 08:42:02 PM

Title: shuting down the computer
Post by: chris on July 16, 2009, 08:42:02 PM
i'm sure this a rather easy but i'm trying to shut down  the computer from a program i'm runing, i tryed
system("cmd.exe","shutdown /f") but all that dose is open a cmd window, so how can i shutdown the computer from my program?
Title: Re: shuting down the computer
Post by: Ionic Wind Support Team on July 16, 2009, 09:26:27 PM
Chris,
Open up the appbar.eba example that comes with your installation, your find the code you need there.

Paul.
Title: Re: shuting down the computer
Post by: chris on July 16, 2009, 09:47:05 PM
okay thanks i'll look in to it theres a lot of code to dig through thanks for the quick reply
Title: Re: shuting down the computer
Post by: splakidas on July 17, 2009, 12:49:56 AM
Here your code...


$INCLUDE "windows.inc"

'CONST EWX_LOGOFF = 0
'CONST EWX_SHUTDOWN = 1
'CONST EWX_REBOOT = 2
'CONST EWX_FORCE = 4

.....
.....


IF MESSAGEBOX(0,"Do you want to shutdown your pc ?","Turn off",@MB_YESNO | @MB_ICONQUESTION) = @IDYES
_ExitWindowsEx( 5, 0)
ENDIF
END



and an example....


'End Session v1.0 - Count down Timer To shutdown NT based systems.
'Requires NT/2000/XP only tested under XP SP2
'Requires Control_Pak 1.6
'Steve Boothman December 2006
'Some code borrowed from Paul Turleys Appbar.iba
AutoDefine "off"

Declare "Kernel32",GetProcAddress(hModule:UInt,lpfnname:String),UInt
Declare "Kernel32",GetVersion(),Int
Declare "Kernel32",GetCurrentProcess(),Int
Declare "User32",ExitWindowsEx(res1:Int,res2:Int),Int

'ExitWindowsEx flags
SetID "EW_RESTARTWINDOWS",0x0042
SetID "EW_REBOOTSYSTEM",  0x0043
SetID "EW_EXITANDEXECAPP",0x0044
SetID "ENDSESSION_LOGOFF",0x80000000
SetID "EWX_LOGOFF",   0
SetID "EWX_SHUTDOWN", 1
SetID "EWX_REBOOT",   2
SetID "EWX_FORCE",    4
SetID "EWX_POWEROFF", 8

'structures needed For security on NT and greater
Type LUID
    Def LowPart:Int
    Def HighPart:Int
EndType

Type TOKENPRIVILEGES2
    Def PrivilegeCount:Int
    Def LowPart:Int
    Def HighPart:Int
    Def Attributes:Int
EndType

'structure needed For enumerating processes
Type PROCESSENTRY32
  Def dwSize:Int
  Def cntUsage:Int
  Def th32ProcessID:Int
  Def th32DefaultHeapID:Int
  Def th32ModuleID:Int
  Def cntThreads:Int
  Def th32ParentProcessID:Int
  Def pcPriClassBase:Int
  Def dwFlags:Int
  Def szExeFile[259]:Int
EndType

'we need some declares For NT (or greater) systems
'in order To change the process privilege To allow
'us To shutdown or restart the System
SetID "SE_PRIVILEGE_ENABLED",0x00000002
SetID "TOKEN_ASSIGN_PRIMARY",    0x0001
SetID "TOKEN_DUPLICATE",         0x0002
SetID "TOKEN_IMPERSONATE",       0x0004
SetID "TOKEN_QUERY",             0x0008
SetID "TOKEN_QUERY_SOURCE",      0x0010
SetID "TOKEN_ADJUST_PRIVILEGES", 0x0020
SetID "TOKEN_ADJUST_GROUPS",     0x0040
SetID "TOKEN_ADJUST_Default",    0x0080
Declare "advapi32",OpenProcessToken(ProcessHandle:Int,Access:Int,TokenHandle:Pointer),Int
Declare "advapi32",AdjustTokenPrivileges(TokenHandle:Int,disable:Int,tkp:TOKENPRIVILEGES2,BufferLength:Int,PreviousState:Int,RetLength:Int),Int
Declare "advapi32",LookupPrivilegeValueA(SystemName:String,Name:String,luid:LUID),Int

Const COMBO_1 = 1
Const GROUP_1 = 2
Const GROUP_2 = 3
Const EDIT1 = 11
Const EDIT2 = 12
Const EDIT3 = 13
Const EDIT4 = 14
Const UDS_1 = 21
Const UDS_2 = 22
Const UDS_3 = 23
Const BUTTON_1 = 31
Const BUTTON_2 = 32

Dialog d1
CreateDialog d1,0,0,210,202,0x80CA0080,0,"End Session",&d1_handler
Control d1,@ComboBox,"ComboBox1",21,23,169,80,0x50800603,COMBO_1
Control d1,@GroupBox,"Options",7,3,195,53,0x50000007,GROUP_1
Control d1,@GroupBox," Hours    ---     Minutes   ---   Seconds",7,63,195,53,0x50000007,GROUP_2

Control d1,@Edit,"0",19,85,50,20,0x50832000,EDIT1
UPDownCTRL(d1,40,80,206,20,@UDS_SETBUDDYINT|@UDS_AUTOBUDDY|@UDS_ALIGNRIGHT ,0,UDS_1)
Control d1,@Edit,"0",81,85,50,20,0x50812000,EDIT2
UPDownCTRL(d1,40,80,206,20,@UDS_SETBUDDYINT|@UDS_AUTOBUDDY|@UDS_ALIGNRIGHT ,0,UDS_2)
Control d1,@Edit,"0",143,85,50,20,0x50812000,EDIT3
UPDownCTRL(d1,40,80,206,20,@UDS_SETBUDDYINT|@UDS_AUTOBUDDY|@UDS_ALIGNRIGHT ,0,UDS_3)

Control d1,@SysButton,"&Start",21,133,70,20,0x50030001,BUTTON_1
Control d1,@SysButton,"&Cancel",118,133,70,20,0x58010000,BUTTON_2
Control d1,@Status,"",0,0,0,0,0,40


UInt seconds,minutes,hours,totaltime
String status
Domodal d1
End

Sub d1_handler
    Select @Message
        Case @IDTimer
            totaltime -= 1000
            If totaltime <= 0
                StopTimer d1
                ShutDownWindows(GetSelected(d1,COMBO_1))
            EndIf
            status = ibMSToStr(totaltime)
            SetControlText d1,40,"Remaining: " + status
        Case @IDInitDialog
            Init()
        Case @IDMenuPick
            Select @MenuNum
                Case 1
                    CloseDialog d1,@IDOK
                Case 2
                    MessageBox d1,"Shutdown Tool\nStathis Plakidas 2006","About"
            EndSelect
        Case @IDControl
            Select @ControlID
                Case COMBO_1

                Case BUTTON_1
                    If @NotifyCode = 0
                        seconds = Val(GetControlText(d1,EDIT3)) * 1000
                        minutes = Val(GetControlText(d1,EDIT2)) * 60000
                        hours   = Val(GetControlText(d1,EDIT1)) * 3600000
                        totaltime = hours + minutes + seconds
                        If totaltime > 0
                            StartTimer d1,1000
                            EnableDisableControls(0,1)
                        Else
                            StopTimer d1
                            MessageBox d1,"Select Time Out","End Session"
                        EndIf
                        status = ibMSToStr(totaltime)
                        SetControlText d1,40,"Remaining: " + status
                    EndIf
                Case BUTTON_2
                    If @NotifyCode = 0
                        StopTimer d1
                        EnableDisableControls(1,0)
                        SetControlText d1,40,"Aborted."
                    EndIf
            EndSelect
    EndSelect
Return
EndSub

'-----------------------------------------------------------------
'Initialize any controls here
Sub Init
    CenterWindow d1
    If SetShutdownPrivilege()    :'returns 0 If Not NT System
        BeginMenu d1
            MenuTitle "File"
                MenuItem "Exit", 0, 1
            MenuTitle "Help"
                MenuItem "About", 0, 2
        EndMenu
        AddString(d1,COMBO_1,"Shut Down")
        AddString(d1,COMBO_1,"Restart")
        AddString(d1,COMBO_1,"Log Off")
        SetSelected d1,COMBO_1,0
        udSetRange32(d1,UDS_1,0,336)    :'hours        14 days
        udSetRange32(d1,UDS_2,0,59)        :'minutes
        udSetRange32(d1,UDS_3,0,59)        :'seconds
        StatusResize(d1,40)
    Else
        MessageBox d1,"Requires NT/2000/XP","End Session"
        CloseDialog d1,@IDCancel
    EndIf
Return
EndSub

Sub EnableDisableControls(a:Int,b:Int)
    EnableControl d1,BUTTON_1,a
    EnableControl d1,BUTTON_2,b
    EnableControl d1,EDIT1,a
    EnableControl d1,EDIT2,a
    EnableControl d1,EDIT3,a
    EnableControl d1,COMBO_1,a
Return
EndSub

Sub ShutDownWindows(shutdown:Int)
    Select shutdown
        Case 0
            ExitWindowsEx(@EWX_SHUTDOWN | @EWX_POWEROFF | @EWX_FORCE,0)
        Case 1
            ExitWindowsEx(@EWX_REBOOT | @EWX_FORCE,0)
        Case 2
            ExitWindowsEx(@EWX_LOGOFF | @EWX_FORCE,0)
    EndSelect
Return
EndSub
'-----------------------------------------------------------------
'SetShutdownPrivilege
'Raises our process privlege so our program can shutdown/restart the System
'Needed For NT/2000/XP
'-----------------------------------------------------------------
Sub SetShutdownPrivilege(),Int
    'If the high bit of GetVersion is set Then
    'this is a Win9x System.
    If((GetVersion() & 0x80000000) = 0)
        'only Do For NT or greater
        Int hToken,hProcess
        TOKENPRIVILEGES2 tkp2
        LUID luid
        hProcess = GetCurrentProcess()
        If(OpenProcessToken(hProcess,(@TOKEN_ADJUST_PRIVILEGES | @TOKEN_QUERY),hToken))
            LookupPrivilegeValueA("","SeShutdownPrivilege",luid)
            tkp2.PrivilegeCount = 1
            tkp2.Attributes = @SE_PRIVILEGE_ENABLED
            tkp2.HighPart = luid.HighPart
            tkp2.LowPart = luid.LowPart
            Return AdjustTokenPrivileges(hToken,0,tkp2,0,0,0)
        Else
            Return 0
        EndIf
    Else
        Return 0
    EndIf
EndSub