[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Using class module to reboot remote NT4 workstation

Posted on 2000-02-15
9
Medium Priority
?
357 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 3

Expert Comment

by:Gordonp
ID: 2521735
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
ID: 2521782
Adjusted points to 200
0
 
LVL 1

Author Comment

by:boomshanker
ID: 2521783
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 3

Expert Comment

by:Gordonp
ID: 2521796
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
 
LVL 43

Accepted Solution

by:
TimCottee earned 800 total points
ID: 2521832
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
ID: 2521834
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
ID: 2521837
Damn Beaten by the clock again :o(
0
 
LVL 3

Expert Comment

by:Gordonp
ID: 2521858
Doh!! Missed the obvious one!!

0
 
LVL 1

Author Comment

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

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
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…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Suggested Courses

656 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