Change Privilege?

oldman

New member
Joined
May 14, 2010
Messages
1
Programming Experience
10+
Language: VB NET
OS: Windows 7 or Windows XP Professional
Visual Studio 2008

1) sorry for english
2) during installation my os windows 7 i have installed one user and also administrator

i have the necessity to change the date and time with my program
if i lunch VS administrator (right mouse) the function SetSystemTime correctly change my time and date but if i run
my user the return error is (no privilege)
in the web i found how to change the privilege for shutdown and inserted it in my module
VB.NET:
Imports System.Runtime.InteropServices
Module Module2
    'This routine enables the Shutdown privilege for the current process,
    'which is necessary if you want to call ExitWindowsEx.

    Const ANYSIZE_ARRAY As Integer = 1
    Const TOKEN_QUERY As Integer = &H8
    Const TOKEN_ADJUST_PRIVILEGES As Integer = &H20
    'Const SE_SHUTDOWN_NAME As String = "SeShutdownPrivilege"
    Const SE_SYSTEMTIME_NAME As String = "SeSystemtimePrivilege"
    'Const SE_SYSTEM_ENVIRONMENT_NAME As String = "SeSystemEnvironmentPrivilege"
    Const SE_PRIVILEGE_ENABLED As Integer = &H2
    'Const SE_TIME_ZONE_NAME As String = "SeTimeZonePrivilege"


    'Dim bEnablePrivilege As Boolean = False

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure LUID
        Public LowPart As UInt32
        Public HighPart As UInt32
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure LUID_AND_ATTRIBUTES
        Public Luid As LUID
        Public Attributes As UInt32
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Private Structure TOKEN_PRIVILEGES
        Public PrivilegeCount As UInt32
        <MarshalAs(UnmanagedType.ByValArray, SizeConst:=ANYSIZE_ARRAY)> _
        Public Privileges() As LUID_AND_ATTRIBUTES
    End Structure

    <StructLayout(LayoutKind.Sequential)> _
    Public Structure SYSTEMTIME
        Public wYear As Integer
        Public wMonth As Integer
        Public wDayOfWeek As Integer
        Public wDay As Integer
        Public wHour As Integer
        Public wMinute As Integer
        Public wSecond As Integer
        Public wMilliseconds As Integer
    End Structure

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Public Function SetSystemTime(ByVal lpSystemTime As SYSTEMTIME) As Boolean
    End Function
    <DllImport("advapi32.dll", SetLastError:=True)> _
    Private Function LookupPrivilegeValue( _
     ByVal lpSystemName As String, _
     ByVal lpName As String, _
     ByRef lpLuid As LUID _
      ) As Boolean
    End Function

    <DllImport("advapi32.dll", SetLastError:=True)> _
    Private Function OpenProcessToken( _
     ByVal ProcessHandle As IntPtr, _
     ByVal DesiredAccess As Integer, _
     ByRef TokenHandle As IntPtr _
      ) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Function CloseHandle(ByVal hHandle As IntPtr) As Boolean
    End Function

    <DllImport("advapi32.dll", SetLastError:=True)> _
    Private Function AdjustTokenPrivileges( _
       ByVal TokenHandle As IntPtr, _
       ByVal DisableAllPrivileges As Boolean, _
       ByRef NewState As TOKEN_PRIVILEGES, _
       ByVal BufferLength As Integer, _
       ByRef PreviousState As TOKEN_PRIVILEGES, _
       ByRef ReturnLength As IntPtr _
     ) As Boolean
    End Function

    Public Sub AcquirePrivilege()
        Dim lastWin32Error As Integer = 0
        'Get the current process's token.
        Dim hProc As IntPtr = Process.GetCurrentProcess().Handle
        Dim hToken As IntPtr
        If Not OpenProcessToken(hProc, TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken) Then
            lastWin32Error = Marshal.GetLastWin32Error()
            Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
             "OpenProcessToken failed with error " & lastWin32Error.ToString & ".")
        End If
        'Dim a As Boolean = SetPrivilege(hToken, SE_SYSTEMTIME_NAME, bEnablePrivilege)

        'Get the LUID that corresponds to the Shutdown privilege, if it exists.
        Dim luid_Shutdown As LUID
        If Not LookupPrivilegeValue(Nothing, SE_SYSTEMTIME_NAME, luid_Shutdown) Then
            lastWin32Error = Marshal.GetLastWin32Error()
            Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
             "LookupPrivilegeValue failed with error " & lastWin32Error.ToString & ".")
        End If

        Try

            'Set up a LUID_AND_ATTRIBUTES structure containing the Shutdown privilege, marked as enabled.
            Dim luaAttr As New LUID_AND_ATTRIBUTES
            luaAttr.Luid = luid_Shutdown
            luaAttr.Attributes = SE_PRIVILEGE_ENABLED

            'Set up a TOKEN_PRIVILEGES structure containing only the shutdown privilege.
            Dim newState As New TOKEN_PRIVILEGES
            newState.PrivilegeCount = 1
            newState.Privileges = New LUID_AND_ATTRIBUTES() {luaAttr}

            'Set up a TOKEN_PRIVILEGES structure for the returned (modified) privileges.
            Dim prevState As TOKEN_PRIVILEGES = New TOKEN_PRIVILEGES
            ReDim prevState.Privileges(CInt(newState.PrivilegeCount))

            'Apply the TOKEN_PRIVILEGES structure to the current process's token.
            Dim returnLength As IntPtr
            If Not AdjustTokenPrivileges(hToken, False, newState, Marshal.SizeOf(prevState), prevState, returnLength) Then
                lastWin32Error = Marshal.GetLastWin32Error()
                Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
                 "AdjustTokenPrivileges failed with error " & lastWin32Error.ToString & ".")
            End If

        Finally
            CloseHandle(hToken)
        End Try

    End Sub

End Module
this function go very well if i have the necessity to shutdown my computer but don't work if i set the privileve for
Const SE_SYSTEMTIME_NAME As String = "SeSystemtimePrivilege"
in the function
VB.NET:
If Not LookupPrivilegeValue(Nothing, SE_SYSTEMTIME_NAME, luid_Shutdown) Then
            lastWin32Error = Marshal.GetLastWin32Error()
            Throw New System.ComponentModel.Win32Exception(lastWin32Error, _
             "LookupPrivilegeValue failed with error " & lastWin32Error.ToString & ".")
        End If

the function not return error but when call
SetSytemTime(myvariable SYSTEMTIME)
the funciont return 0 and the error with GetLastError is "NO privilege"

or in vb

Private Sub CmdSet_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles CmdSet.Click
this partial code
VB.NET:
        'Dim lpSystemTime As SYSTEMTIME
        Dim mdata As String
        Dim mydata As Date
        'Dim retval As Boolean
        mdata = LblGiorno.Text & "/" & LblMese.Text & "/" & LblAnno.Text & " " & LblOre.Text & ":" & LblMinuti.Text & ":" & LblSecondi.Text
        mydata = mdata

        AcquirePrivilege()
        TimeOfDay = mydata
or this
VB.NET:
        'lpSystemTime.wYear = CType(LblAnno.Text, Integer)
        'lpSystemTime.wMonth = CType(LblMese.Text, Integer)
        'lpSystemTime.wDay = CType(LblGiorno.Text, Integer)
        'lpSystemTime.wHour = CType(LblOre.Text, Integer)
        'lpSystemTime.wMinute = CType(LblMinuti.Text, Integer)
        'lpSystemTime.wSecond = CType(LblSecondi.Text, Integer)
        'lpSystemTime.wMilliseconds = 0
        'retval = SetSystemTime(lpSystemTime)
    End Sub
why i can change my sistem data and time????
thank's for all
 
Last edited by a moderator:
Top Bottom