Link to home
Start Free TrialLog in
Avatar of vbdev04
vbdev04

asked on

Convert C++ routine to VB6


I need help convert this routine to VB6:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secmgmt/security/managing_account_permissions.asp

void AddPrivileges(PSID AccountSID, LSA_HANDLE PolicyHandle)
{
  LSA_UNICODE_STRING lucPrivilege;
  NTSTATUS ntsResult;

  // Create an LSA_UNICODE_STRING for the privilege name(s).
  if (!InitLsaString(&lucPrivilege, L"SeServiceLogonRight"))
  {
         wprintf(L"Failed InitLsaString\n");
         return;
  }

  ntsResult = LsaAddAccountRights(
    PolicyHandle,  // An open policy handle.
    AccountSID,    // The target SID.
    &lucPrivilege, // The privilege(s).
    1              // Number of privileges.
  );                
  if (ntsResult == STATUS_SUCCESS)
  {
    wprintf(L"Privilege added.\n");
  }
  else
  {
    wprintf(L"Privilege was not added - %lu \n",
      LsaNtStatusToWinError(ntsResult));
  }
}

Avatar of Smallint
Smallint

Give it a shot.

Cheers


Option Explicit

Private Type LSA_UNICODE_STRING
    Length As Integer
    MaximumLength As Integer
    Buffer As String
End Type


Private Type LSA_OBJECT_ATTRIBUTES
    Length As Long
    RootDirectory As Long
    ObjectName As LSA_UNICODE_STRING
    Attributes As Long
    SecurityDescriptor As Long ' Points to type SECURITY_DESCRIPTOR
    SecurityQualityOfService As Long ' Points to type
                                     ' SECURITY_QUALITY_OF_SERVICE
End Type

Private Type PSID
    sidData(228) As Byte
End Type

Private Declare Function LsaAddAccountRights Lib "Advapi32.dll" _
   (ByVal PolicyHandle As Long, AccountSid As PSID, userRights As _
   LSA_UNICODE_STRING, ByVal CountOfRights As Long) As Long

Private Declare Function LsaNtStatusToWinError Lib "Advapi32.dll" _
        (ByVal Status As Long) As Long
       
Private Const STATUS_SUCCESS = 0
       
 Private Declare Function StrLenA Lib "Kernel32" _
        Alias "lstrlenA" _
        (ByVal lpString As String) As Long
       

Private Sub InitLsaString(LsaString As LSA_UNICODE_STRING, strString As String)
 
     With LsaString
         If strString = "" Then
             .Length = 0
             .MaximumLength = 0
             .Buffer = ""
         Else
             .Length = StrLenA(strString)
             .MaximumLength = StrLenA(strString) + 1
             .Buffer = strString
         End If
     End With
 End Sub


Private Sub AddPrivileges(AccountSid As PSID, PolicyHandle As Long)

  Dim lucPrivilege As LSA_UNICODE_STRING
  Dim ntsResult As Long

  'Create an LSA_UNICODE_STRING for the privilege name(s).
  Call InitLsaString(lucPrivilege, "SeServiceLogonRight")

  ntsResult = LsaAddAccountRights(PolicyHandle, AccountSid, lucPrivilege, 1)
 
  If (ntsResult = STATUS_SUCCESS) Then
    Debug.Print "Privilege added."
  Else
    Debug.Print "Privilege was not added - " & CStr(LsaNtStatusToWinError(ntsResult))
  End If


End Sub



Private Sub Form_Load()
    Dim AccountSid As PSID
    Dim PolicyHandle As Long
   
    Call AddPrivileges(AccountSid, PolicyHandle)
End Sub
Avatar of vbdev04

ASKER


Thanks. Looks like it will work and I will award those points to you.

However, I am looking for a code that will set the policy given a username. That probably involve getting the AccountSid and Policyhandle using:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secmgmt/security/opening_a_policy_object_handle.asp 

and
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/secmgmt/security/translating_between_names_and_sids.asp
ASKER CERTIFIED SOLUTION
Avatar of Smallint
Smallint

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of vbdev04

ASKER


Thanks for your efforts.