Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / General 2 / November 2004



Tip: Looking for answers? Try searching our database.

How do i set user priveleges with Visual Basic

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
hilla hartman - 28 Nov 2004 08:26 GMT
Hi,
Im trying to do RegSaveKey and found out that i need to set the user
privelege..
I have the code to do this with C++ -

BOOL SetCurrentPrivilege( LPCTSTR Privilege, BOOL bEnablePrivilege )
{
    HANDLE hToken;
    LUID luid;
    TOKEN_PRIVILEGES tp, tpPrevious;
    DWORD cbPrevious = sizeof( TOKEN_PRIVILEGES );
    BOOL bSuccess = FALSE;

    if ( ! LookupPrivilegeValue( NULL, Privilege, &luid ) )
        return FALSE;

    if( ! OpenProcessToken( GetCurrentProcess(), TOKEN_QUERY |
TOKEN_ADJUST_PRIVILEGES, &hToken ) )
        return FALSE;

    tp.PrivilegeCount = 1;
    tp.Privileges[0].Luid = luid;
    tp.Privileges[0].Attributes = 0;

    AdjustTokenPrivileges( hToken, FALSE, &tp, sizeof( TOKEN_PRIVILEGES
), &tpPrevious, &cbPrevious );

    if ( GetLastError() == ERROR_SUCCESS )
    {
        tpPrevious.PrivilegeCount = 1;
        tpPrevious.Privileges[0].Luid = luid;

        if ( bEnablePrivilege )
            tpPrevious.Privileges[0].Attributes |= ( SE_PRIVILEGE_ENABLED );
        else
            tpPrevious.Privileges[0].Attributes &= ~( SE_PRIVILEGE_ENABLED );

        AdjustTokenPrivileges( hToken, FALSE, &tpPrevious, cbPrevious, NULL,
NULL );

        if ( GetLastError() == ERROR_SUCCESS )
            bSuccess=TRUE;
    }

    CloseHandle( hToken );

    return bSuccess;
}

Does anyone have the code to do this with Visual basic?
thanks
Hilla
Frank Adam - 28 Nov 2004 09:10 GMT
I have this sitting here to set SE_DEBUG only, but it should be
trivial to alter it to pass in whatever you need.

Private Const SE_DEBUG_NAME = "SeDebugPrivilege"
Private Const SE_PRIVILEGE_ENABLED = &H2
Private Const TOKEN_ADJUST_PRIVILEGES = &H20
Private Const TOKEN_QUERY = &H8
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 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 GetCurrentProcess Lib "kernel32" () As Long

Public Sub NTSetDebugPrivilege()
   Dim hToken As Long
   Dim seDebug As LUID
   Dim tkp As TOKEN_PRIVILEGES, tkpPrev As TOKEN_PRIVILEGES
   
   Call OpenProcessToken(GetCurrentProcess(), _
       TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY, hToken)
   Call LookupPrivilegeValue(0&, SE_DEBUG_NAME, seDebug)
   tkp.PrivilegeCount = 1
   tkp.Privileges(0).pLuid = seDebug
   tkp.Privileges(0).Attributes = SE_PRIVILEGE_ENABLED

   Call AdjustTokenPrivileges(hToken, False, tkp, Len(tkp), tkpPrev,
Len(tkpPrev))

   Call CloseHandle(hToken)
End Sub

Signature

Regards, Frank

hilla hartman - 29 Nov 2004 13:19 GMT
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.