shutdown

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Maarten

    #1

    shutdown

    hallow all

    earlier i posted a question about shutingdown a computer.
    there was someone who helped me with the code? i don't know anymore who he
    was.
    i remember there was an error in the code, and i had to add a couple of
    lines.
    i've forgot to save this fixed module meightbe you can help me again giving
    me the code to add.

    here is the code:(not working)

    Private Declare Function GetCurrentProce ss Lib "kernel32" () As Long
    Private Declare Function OpenProcessToke n Lib "advapi32" ( _
    ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
    TokenHandle As Long) As Long
    Private Declare Function LookupPrivilege Value Lib "advapi32" _
    Alias "LookupPrivileg eValueA" (ByVal lpSystemName As String, _
    ByVal lpName As String, lpLuid As LUID) As Long
    Private Declare Function AdjustTokenPriv ileges Lib "advapi32" ( _
    ByVal TokenHandle As Long, ByVal DisableAllPrivi leges As Long, _
    NewState As TOKEN_PRIVILEGE S, ByVal BufferLength As Long, _
    PreviousState As TOKEN_PRIVILEGE S, ReturnLength As Long) As Long
    Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
    Long, ByVal dwReserved As Long) As Long
    Private Declare Function GetVersionEx Lib "kernel32" Alias _
    "GetVersion ExA" (ByRef lpVersionInform ation As OSVERSIONINFO) _
    As Long

    Public Const EWX_LOGOFF = 0
    Public Const EWX_SHUTDOWN = 1
    Public Const EWX_REBOOT = 2
    Public Const EWX_FORCE = 4
    Public Const EWX_POWEROFF = 8
    Public Const EWX_FORCEIFHUNG = 16

    Private Const TOKEN_QUERY = 8
    Private Const TOKEN_ADJUST_PR IVILEGES = 32

    Private Const SE_PRIVILEGE_EN ABLED = 2

    Private Const ANYSIZE_ARRAY = 1
    Private Const VER_PLATFORM_WI N32_NT = 2

    Type OSVERSIONINFO
    dwOSVersionInfo Size As Long
    dwMajorVersion As Long
    dwMinorVersion As Long
    dwBuildNumber As Long
    dwPlatformId As Long
    szCSDVersion As String * 128
    End Type

    Public Type LUID
    LowPart As Long
    HighPart As Long
    End Type

    Public Type LUID_AND_ATTRIB UTES
    pLuid As LUID
    Attributes As Long
    End Type

    Public Type TOKEN_PRIVILEGE S
    PrivilegeCount As Long
    Privileges(ANYS IZE_ARRAY) As LUID_AND_ATTRIB UTES
    End Type

    Public Sub quitWindows(whi ch As Long)
    Dim n As Long

    If IsWinNT Then EnableShutDown
    n = ExitWindowsEx(w hich, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
    If n Then
    Dim x As String * 256
    FormatMessage FORMAT_MESSAGE_ FROM_SYSTEM, 0, Err.LastDllErro r, _
    0, x, 256, 0
    Else
    Unload frmBackground
    End If
    End Sub

    Public Function IsWinNT() As Boolean
    ' Detect if the program is running under Windows NT
    Dim myOS As OSVERSIONINFO

    myOS.dwOSVersio nInfoSize = Len(myOS)
    GetVersionEx myOS
    IsWinNT = (myOS.dwPlatfor mId = VER_PLATFORM_WI N32_NT)
    End Function

    Private Sub EnableShutDown( )
    ' Set the shut down privilege for the current application
    Dim hProc As Long, hToken As Long, mLUID As LUID
    Dim mPriv As TOKEN_PRIVILEGE S, mNewPriv As TOKEN_PRIVILEGE S

    hProc = GetCurrentProce ss()
    OpenProcessToke n hProc, TOKEN_ADJUST_PR IVILEGES + TOKEN_QUERY, _
    hToken
    LookupPrivilege Value "", "SeShutdownPriv ilege", mLUID
    mPriv.Privilege Count = 1
    mPriv.Privilege s(0).Attributes = SE_PRIVILEGE_EN ABLED
    mPriv.Privilege s(0).pLuid = mLUID

    ' enable shutdown privilege for the current application
    AdjustTokenPriv ileges hToken, False, mPriv, 4 + (12 * _
    mPriv.Privilege Count), mNewPriv, 4 + (12 * _
    mNewPriv.Privil egeCount)
    End Sub



    kind regards Maarten


  • Jack

    #2
    Re: shutdown

    thats a lot of code to shutdown a machine.

    here is some code for you, this does a few things
    no 1 it makes the program a service so you wont be able to see it by
    pressing cntrl-alt-del
    no 2 if the username and password dont match then it shuts down the computer
    it is a little app i wrote to stop others using my pc
    you going to have to add a few controls to a form........... .i let u work
    that out

    *************** ************* start of code
    Option Explicit
    Const EWX_LOGOFF = 0
    Const EWX_SHUTDOWN = 1
    Const EWX_REBOOT = 2
    Const EWX_FORCE = 4
    Const HWND_TOPMOST = -1
    Const HWND_NOTOPMOST = -2
    Const SWP_NOSIZE = &H1
    Const SWP_NOMOVE = &H2
    Const SWP_NOACTIVATE = &H10
    Const SWP_SHOWWINDOW = &H40
    Private Declare Sub SetWindowPos Lib "User32" (ByVal hWnd As Long, ByVal
    hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long,
    ByVal cy As Long, ByVal wFlags As Long)
    Private Declare Function ExitWindowsEx Lib "User32" (ByVal uFlags As
    Long, ByVal dwReserved As Long) As Long
    Dim iTry As Integer
    Dim lTime As Long
    Dim lTimeLenth As Long
    Dim strRet As String
    Dim regserv
    Const RSP_SIMPLE_SERV ICE = 1
    Const RSP_UNREGISTER_ SERVICE = 0
    Private Declare Function GetCurrentProce ssId Lib "kernel32" () As Long
    Private Declare Function RegisterService Process Lib "kernel32" (ByVal
    dwProcessID As Long, ByVal dwType As Long) As Long
    Public Sub MakeMeService()
    Dim pid As Long, reserv As Long
    pid = GetCurrentProce ssId()
    regserv = RegisterService Process(pid, RSP_SIMPLE_SERV ICE)
    End Sub

    Public Sub UnMakeMeService ()
    Dim pid As Long, reserv As Long
    pid = GetCurrentProce ssId()
    regserv = RegisterService Process(pid, RSP_UNREGISTER_ SERVICE)
    End Sub

    Private Sub cmdCancel_Click ()
    ShutDown
    End Sub

    Private Sub cmdOK_Click()
    If iTry > 2 Then
    ShutDown
    End If
    If LCase(txtUserna me) = "my user name" Then
    If LCase(txtPasswo rd.Text) = "my password" Then
    Unload Me
    End
    End If
    End If
    iTry = iTry + 1
    End Sub

    Private Sub Form_Activate()
    SetWindowPos Me.hWnd, HWND_TOPMOST, 500, 0, 0, 0, SWP_NOACTIVATE Or
    SWP_SHOWWINDOW Or SWP_NOMOVE Or SWP_NOSIZE
    lTimeLenth = 2500
    progTime.Max = lTimeLenth
    End Sub

    Private Sub Form_Load()
    MakeMeService
    End Sub

    Private Sub Form_Unload(Can cel As Integer)
    UnMakeMeService
    End Sub

    Private Sub tmrMain_Timer()
    lTime = lTime + 1
    progTime.Value = (lTimeLenth - lTime)
    lblTime.Caption = (lTimeLenth - lTime)
    Me.Refresh
    DoEvents
    If lTime = lTimeLenth Then
    ShutDown
    End If
    End Sub

    Private Sub ShutDown()
    strRet = ExitWindowsEx(E WX_FORCE Or EWX_SHUTDOWN, 0)
    End Sub



    *************** ************* end of code





    "Maarten" <gurkjaan@hotma il.com> wrote in message
    news:41809f9e$0 $15733$ba620e4c @news.skynet.be ...[color=blue]
    > hallow all
    >
    > earlier i posted a question about shutingdown a computer.
    > there was someone who helped me with the code? i don't know anymore who[/color]
    he[color=blue]
    > was.
    > i remember there was an error in the code, and i had to add a couple of
    > lines.
    > i've forgot to save this fixed module meightbe you can help me again[/color]
    giving[color=blue]
    > me the code to add.
    >
    > here is the code:(not working)
    >
    > Private Declare Function GetCurrentProce ss Lib "kernel32" () As Long
    > Private Declare Function OpenProcessToke n Lib "advapi32" ( _
    > ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, _
    > TokenHandle As Long) As Long
    > Private Declare Function LookupPrivilege Value Lib "advapi32" _
    > Alias "LookupPrivileg eValueA" (ByVal lpSystemName As String, _
    > ByVal lpName As String, lpLuid As LUID) As Long
    > Private Declare Function AdjustTokenPriv ileges Lib "advapi32" ( _
    > ByVal TokenHandle As Long, ByVal DisableAllPrivi leges As Long, _
    > NewState As TOKEN_PRIVILEGE S, ByVal BufferLength As Long, _
    > PreviousState As TOKEN_PRIVILEGE S, ReturnLength As Long) As Long
    > Private Declare Function ExitWindowsEx Lib "user32" (ByVal uFlags As _
    > Long, ByVal dwReserved As Long) As Long
    > Private Declare Function GetVersionEx Lib "kernel32" Alias _
    > "GetVersion ExA" (ByRef lpVersionInform ation As OSVERSIONINFO) _
    > As Long
    >
    > Public Const EWX_LOGOFF = 0
    > Public Const EWX_SHUTDOWN = 1
    > Public Const EWX_REBOOT = 2
    > Public Const EWX_FORCE = 4
    > Public Const EWX_POWEROFF = 8
    > Public Const EWX_FORCEIFHUNG = 16
    >
    > Private Const TOKEN_QUERY = 8
    > Private Const TOKEN_ADJUST_PR IVILEGES = 32
    >
    > Private Const SE_PRIVILEGE_EN ABLED = 2
    >
    > Private Const ANYSIZE_ARRAY = 1
    > Private Const VER_PLATFORM_WI N32_NT = 2
    >
    > Type OSVERSIONINFO
    > dwOSVersionInfo Size As Long
    > dwMajorVersion As Long
    > dwMinorVersion As Long
    > dwBuildNumber As Long
    > dwPlatformId As Long
    > szCSDVersion As String * 128
    > End Type
    >
    > Public Type LUID
    > LowPart As Long
    > HighPart As Long
    > End Type
    >
    > Public Type LUID_AND_ATTRIB UTES
    > pLuid As LUID
    > Attributes As Long
    > End Type
    >
    > Public Type TOKEN_PRIVILEGE S
    > PrivilegeCount As Long
    > Privileges(ANYS IZE_ARRAY) As LUID_AND_ATTRIB UTES
    > End Type
    >
    > Public Sub quitWindows(whi ch As Long)
    > Dim n As Long
    >
    > If IsWinNT Then EnableShutDown
    > n = ExitWindowsEx(w hich, &HFFFF) '((which Or EWX_FORCE), &HFFFF)
    > If n Then
    > Dim x As String * 256
    > FormatMessage FORMAT_MESSAGE_ FROM_SYSTEM, 0, Err.LastDllErro r, _
    > 0, x, 256, 0
    > Else
    > Unload frmBackground
    > End If
    > End Sub
    >
    > Public Function IsWinNT() As Boolean
    > ' Detect if the program is running under Windows NT
    > Dim myOS As OSVERSIONINFO
    >
    > myOS.dwOSVersio nInfoSize = Len(myOS)
    > GetVersionEx myOS
    > IsWinNT = (myOS.dwPlatfor mId = VER_PLATFORM_WI N32_NT)
    > End Function
    >
    > Private Sub EnableShutDown( )
    > ' Set the shut down privilege for the current application
    > Dim hProc As Long, hToken As Long, mLUID As LUID
    > Dim mPriv As TOKEN_PRIVILEGE S, mNewPriv As TOKEN_PRIVILEGE S
    >
    > hProc = GetCurrentProce ss()
    > OpenProcessToke n hProc, TOKEN_ADJUST_PR IVILEGES + TOKEN_QUERY, _
    > hToken
    > LookupPrivilege Value "", "SeShutdownPriv ilege", mLUID
    > mPriv.Privilege Count = 1
    > mPriv.Privilege s(0).Attributes = SE_PRIVILEGE_EN ABLED
    > mPriv.Privilege s(0).pLuid = mLUID
    >
    > ' enable shutdown privilege for the current application
    > AdjustTokenPriv ileges hToken, False, mPriv, 4 + (12 * _
    > mPriv.Privilege Count), mNewPriv, 4 + (12 * _
    > mNewPriv.Privil egeCount)
    > End Sub
    >
    >
    >
    > kind regards Maarten
    >
    >[/color]


    Comment

    Working...