SetSecurityDescriptorOwner: Can't quite get it

  • Thread starter Thread starter Dave Coate
  • Start date Start date
D

Dave Coate

Hi again,

I am getting further in my goal to be able to use APIs from vb.net to
view and ultimately set file permssions. I have the veiwing part down,
now I am trying to set permissions. First up is the set the owner.
From my research, it appears that I am supposed to Initialize a new
Security Descriptor and then place it and a pointer to the SID of the
new owner into the SetSecurityDescriptorOwner Function. I am fairly
sure I have the owner SID pointer portion correct. It is the Security
Descriptor I am having difficulty with. Here is what I have so far:

'Declarations, Structures and Constants
<StructLayout(LayoutKind.Sequential, Pack:=1)> _
Public Structure SECURITY_DESCRIPTOR
Public Revision As Integer
Public Sbz1 As Integer
Public Control As Integer
Public Owner As IntPtr
Public Group As IntPtr
Public Sacl As IntPtr
Public Dacl As IntPtr
End Structure

'Function to set the owner of an object
'If the function succeeds, the return value is nonzero
Private Declare Function SetSecurityDescriptorOwner Lib "advapi32.dll"
( _
ByVal pSecurityDescriptor As SECURITY_DESCRIPTOR, _
ByVal pOwner As IntPtr, _
ByVal pOwnerDefaulted As Integer) As Boolean

'If the function succeeds, the return value is nonzero
Private Declare Function InitializeSecurityDescriptor Lib
"advapi32.dll" ( _
ByRef pSecurityDescriptor As SECURITY_DESCRIPTOR, _
ByVal dwRevision As Integer) As Boolean

Const SECURITY_DESCRIPTOR_REVISION As Integer = &H1&

'Code
Dim SecDesc As SECURITY_DESCRIPTOR

bSuccess = InitializeSecurityDescriptor(SecDesc,
SECURITY_DESCRIPTOR_REVISION)
If (bSuccess = 0) Then
MsgBox("InitializeSecurityDescriptor failed with error code " _
& Err.LastDllError)
Exit Function
End If

'Failure
bSuccess = SetSecurityDescriptorOwner(SecDesc, pOwner, 0)
'/Failure
If bSuccess = 0 Then
MsgBox("Set Owner Failed " & Err.LastDllError)
End If

This code fails on the SetSecurityDescriptorOwner line. Exception:
Object Reference is not set to an instance of an Object. I am fairly
confident that pOwner is correct as I can use it in other functions. I
think I am missing something in how I create the Security Descriptor.

Can anyone point me in the right direction?

Thanks,
Dave Coate
 
Back
Top