Solved

Using class module to reboot remote NT4 workstation

Posted on 2000-02-15
9
330 Views
Last Modified: 2012-06-21
I have the following class module code and form code, but can't make them work:


'CLASS MODULE CODE:

Option Explicit

Private Const ANYSIZE_ARRAY = 1

Private Type LUID
    lowpart As Long
    highpart As Long
End Type

Private Type LUID_AND_ATTRIBUTES
    pLuid As LUID
    Attributes As Long
End Type

Private Type TOKEN_PRIVILEGES
    PrivilegeCount As Long
    Privileges(ANYSIZE_ARRAY) As LUID_AND_ATTRIBUTES
End Type

Private Declare Function OpenProcessToken Lib "advapi32.dll" ( _
        ByVal ProcessHandle As Long, ByVal DesiredAccess As Long, TokenHandle As Long) _
        As Long

Private Declare Function GetCurrentProcess Lib "kernel32" () As Long

Private Declare Function LookupPrivilegeValue Lib "advapi32.dll" Alias _
        "LookupPrivilegeValueA" (ByVal lpSystemName As String, ByVal lpName As String, _
        lpLuid As LUID) As Long

Private Declare Function AdjustTokenPrivileges Lib "advapi32.dll" ( _
        ByVal TokenHandle As Long, ByVal DisableAllPrivileges As Long, _
        NewState As TOKEN_PRIVILEGES, ByVal BufferLength As Long, _
        PreviousState As TOKEN_PRIVILEGES, ReturnLength As Long) As Long

Private Declare Function GetLastError Lib "kernel32" () As Long

Private Const DELETE = &H10000
Private Const READ_CONTROL = &H20000
Private Const WRITE_DAC = &H40000
Private Const WRITE_OWNER = &H80000
Private Const SYNCHRONIZE = &H100000
Private Const STANDARD_RIGHTS_REQUIRED = &HF0000
Private Const STANDARD_RIGHTS_READ = READ_CONTROL
Private Const STANDARD_RIGHTS_WRITE = READ_CONTROL
Private Const STANDARD_RIGHTS_EXECUTE = READ_CONTROL
Private Const STANDARD_RIGHTS_ALL = &H1F0000
Private Const SPECIFIC_RIGHTS_ALL = &HFFFF&
Private Const TOKEN_ASSIGN_PRIMARY = &H1&
Private Const TOKEN_DUPLICATE = &H2&
Private Const TOKEN_IMPERSONATE = &H4&
Private Const TOKEN_QUERY = &H8&
Private Const TOKEN_QUERY_SOURCE = &H10&
Private Const TOKEN_ADJUST_PRIVILEGES = &H20&
Private Const TOKEN_ADJUST_GROUPS = &H40&
Private Const TOKEN_ADJUST_DEFAULT = &H80&
Private Const TOKEN_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED Or _
                                 TOKEN_ASSIGN_PRIMARY Or _
                                 TOKEN_DUPLICATE Or _
                                 TOKEN_IMPERSONATE Or _
                                 TOKEN_QUERY Or _
                                 TOKEN_QUERY_SOURCE Or _
                                 TOKEN_ADJUST_PRIVILEGES Or _
                                 TOKEN_ADJUST_GROUPS Or _
                                 TOKEN_ADJUST_DEFAULT
Private Const TOKEN_READ = STANDARD_RIGHTS_READ Or TOKEN_QUERY
Private Const TOKEN_WRITE = STANDARD_RIGHTS_WRITE Or _
                            TOKEN_ADJUST_PRIVILEGES Or _
                            TOKEN_ADJUST_GROUPS Or _
                            TOKEN_ADJUST_DEFAULT
Private Const TOKEN_EXECUTE = STANDARD_RIGHTS_EXECUTE
Private Const SE_CREATE_TOKEN_NAME = "SeCreateTokenPrivilege"
Private Const SE_ASSIGNPRIMARYTOKEN_NAME = "SeAssignPrimaryTokenPrivilege"
Private Const SE_LOCK_MEMORY_NAME = "SeLockMemoryPrivilege"
Private Const SE_INCREASE_QUOTA_NAME = "SeIncreaseQuotaPrivilege"
Private Const SE_UNSOLICITED_INPUT_NAME = "SeUnsolicitedInputPrivilege"
Private Const SE_MACHINE_ACCOUNT_NAME = "SeMachineAccountPrivilege"
Private Const SE_TCB_NAME = "SeTcbPrivilege"
Private Const SE_SECURITY_NAME = "SeSecurityPrivilege"
Private Const SE_TAKE_OWNERSHIP_NAME = "SeTakeOwnershipPrivilege"
Private Const SE_LOAD_DRIVER_NAME = "SeLoadDriverPrivilege"
Private Const SE_SYSTEM_PROFILE_NAME = "SeSystemProfilePrivilege"
Private Const SE_SYSTEMTIME_NAME = "SeSystemtimePrivilege"
Private Const SE_PROF_SINGLE_PROCESS_NAME = "SeProfileSingleProcessPrivilege"
Private Const SE_INC_BASE_PRIORITY_NAME = "SeIncreaseBasePriorityPrivilege"
Private Const SE_CREATE_PAGEFILE_NAME = "SeCreatePagefilePrivilege"
Private Const SE_CREATE_PERMANENT_NAME = "SeCreatePermanentPrivilege"
Private Const SE_BACKUP_NAME = "SeBackupPrivilege"
Private Const SE_RESTORE_NAME = "SeRestorePrivilege"
Private Const SE_SHUTDOWN_NAME = "SeShutdownPrivilege"
Private Const SE_DEBUG_NAME = "SeDebugPrivilege"
Private Const SE_AUDIT_NAME = "SeAuditPrivilege"
Private Const SE_SYSTEM_ENVIRONMENT_NAME = "SeSystemEnvironmentPrivilege"
Private Const SE_CHANGE_NOTIFY_NAME = "SeChangeNotifyPrivilege"
Private Const SE_REMOTE_SHUTDOWN_NAME = "SeRemoteShutdownPrivilege"
Private Const SE_PRIVILEGE_ENABLED_BY_DEFAULT = &H1&
Private Const SE_PRIVILEGE_ENABLED = &H2&
Private Const SE_PRIVILEGE_USED_FOR_ACCESS = &H80000000
Private Const ERROR_SUCCESS = 0&

Private Declare Function InitiateSystemShutdown Lib "advapi32.dll" _
                Alias "InitiateSystemShutdownA" ( _
                ByVal lpMachineName As String, ByVal lpMessage As String, _
                ByVal dwTimeout As Long, ByVal bForceAppsClosed As Long, _
                ByVal bRebootAfterShutdown As Long) As Boolean

Private Declare Function AbortSystemShutdown Lib "advapi32.dll" _
                Alias "AbortSystemShutdownA" (ByVal lpMachineName As String) As Long

Private tkpSaved As TOKEN_PRIVILEGES
Private hToken As Long

Public Sub AbortShutDown(ByVal cComputerName As String)

    Dim bDummy As Boolean

    If Len(cComputerName) = 0 Then
        bDummy = SetPrivilege(SE_SHUTDOWN_NAME)
    Else
        bDummy = SetPrivilege(SE_REMOTE_SHUTDOWN_NAME)
    End If
   
    If bDummy Then
        ' Display the shutdown dialog box and start the time-out countdown.
        If AbortSystemShutdown(cComputerName) Then
            ' Disable shutdown privilege.
            ResetPrivilege
        End If
    End If

End Sub


Public Sub ResetPrivilege()

    Dim tkp As TOKEN_PRIVILEGES
    Dim lDummy As Long
    Dim lRequired As Long

    lDummy = AdjustTokenPrivileges(hToken, False, tkpSaved, Len(tkp), tkp, lRequired)

End Sub
Public Function SetPrivilege(ByVal cPrivilegeName As String) As Boolean

    Dim tkp As TOKEN_PRIVILEGES
    Dim lDummy As Long
    Dim lRequired As Long
    Dim bRetVal As Boolean

    ' Get the current process token handle so we can get shutdown privilege.
    If OpenProcessToken(GetCurrentProcess(), _
                        TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
        ' Get the LUID for shutdown privilege.
        If LookupPrivilegeValue(vbNullString, cPrivilegeName, tkp.Privileges(0).pLuid) > 0 Then
            tkp.PrivilegeCount = 1      ' one privilege to set
            tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED
   
            ' Get shutdown privilege for this process.
            lDummy = AdjustTokenPrivileges(hToken, False, tkp, Len(tkpSaved), tkpSaved, lRequired)
            ' Cannot test the return value of AdjustTokenPrivileges.
            lDummy = GetLastError
            If lDummy = ERROR_SUCCESS Then
                bRetVal = True
            Else
                Debug.Print lDummy
            End If
        End If
    End If
    SetPrivilege = bRetVal

End Function
Public Sub ShutDown(ByVal cComputerName As String, ByVal cMessage As String, _
                    ByVal lTimeout As Long, ByVal bForceAppsClosed As Boolean, _
                    ByVal bRebootAfterShutdown As Boolean)

    Dim bDummy As Boolean

    If Len(cComputerName) = 0 Then
        bDummy = SetPrivilege(SE_SHUTDOWN_NAME)
    Else
        bDummy = SetPrivilege(SE_REMOTE_SHUTDOWN_NAME)
    End If
    If bDummy Then
        ' Display the shutdown dialog box and start the time-out countdown.
        If InitiateSystemShutdown(cComputerName, cMessage, lTimeout, _
                                  bForceAppsClosed, bRebootAfterShutdown) Then
            ' Disable shutdown privilege.
            ResetPrivilege
        End If
    End If

End Sub


'FORM CODE:


Option Explicit

'define Shutdown as an object of clsNTPrivileges
Private Shutdwn As clsNTPrivileges

Private Sub Command1_Click()
     
'This sub is supposed to call the class module clsNTPrivileges and
'initiate a shutdown and restart of a remote NT4 workstation.
     
     'Initialise the object
     Set Shutdwn = New clsNTPrivileges
     
     Dim dummy As Long
         
     'call the class module member "ShutDown" and pass it a computer name,
     'a message to display while shutting down, a count down value,
     'and two Boolean values to indicate a forceful restart.
     '"Shutdown" seems to return a value so "dummy" is used.
     dummy = Shutdwn.ShutDown("\\2002-n2", "Shutting Down", 1000, True, True)
         
End Sub




******************************************************
Any help would be greatly appreciated.  Thx.

0
Comment
Question by:boomshanker
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:Gordonp
Comment Utility
What happens when you try to shutdown the remote PC.

The most likely reason it doesnt work is that you have insufficient priveledges on the remote machine, or you do not have an account on the remote machine.

I'm using similar code in one of my own projects and it only works if I have an Account on the Remote PC I'm trying to shutdown, or its in the same NT Domain.

Gordon
0
 
LVL 1

Author Comment

by:boomshanker
Comment Utility
Adjusted points to 200
0
 
LVL 1

Author Comment

by:boomshanker
Comment Utility
I get an error message when I click the command button to execute the form code:

Compile Error.  Expected Function or variable.

The debugger points to the Command_Click() sub, specifically to the .Shutdown reference.

Thx.

0
 
LVL 3

Expert Comment

by:Gordonp
Comment Utility
look in the Object Browser, is the Shutdown method visible for the clsNTPriveleges class.

also try using the 'Compile and Run' option rather than the 'run' option to pick up these sort of errors before youre app actually runs.

Gordon

0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 43

Accepted Solution

by:
TimCottee earned 200 total points
Comment Utility
The line that you have the error on is expecting a function or variable, i.e. it is expecting to receive a return value from a function call. However the .ShutDown is a procedure NOT a function and does not return a value. If you replace this line with:

Shutdwn.ShutDown "\\2002-n2", "Shutting Down", 1000, True, True

Then no error will be generated and the shutdown should be completed.
0
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
I just tried your code and got the error you described and it is because the line.
dummy = Shutdwn.ShutDown("\\2002-n2", "Shutting Down", 1000, True, True)


Is expecting a function...
However  the method ShutDown of your class is a SUB and subs do not return values.....


either make it a function or use this instead....

          Shutdwn.ShutDown "\\james", "Shutting Down", 1000, True, True


Cheers
0
 
LVL 13

Expert Comment

by:crazyman
Comment Utility
Damn Beaten by the clock again :o(
0
 
LVL 3

Expert Comment

by:Gordonp
Comment Utility
Doh!! Missed the obvious one!!

0
 
LVL 1

Author Comment

by:boomshanker
Comment Utility
Man, you have no idea how badly that was driving me nuts.  Thanks a ton.

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now