ファイル所有者の取得          <TOP>


GetFileSecurity ファイルのセキュリティ情報取得
GetSecurityDescriptorOwner 指定されたセキュリティ記述子からオーナーを表すSID(セキュリティ識別子)を取得
LookupAccountSid SID(セキュリティ識別子)に対応する名前の取得
IsValidSid リビジョン番号が既知の範囲内にあるか、副権限の数が最大値以下であるかどうかを検証することにより、SID構造体を確認
 

 

'================================================================
'= ファイル所有者の取得
'=    (GetFileSecurity.bas)
'================================================================
#include "Windows.bi"

' ファイルのセキュリティ情報取得
Declare Function Api_GetFileSecurity& Lib "advapi32" Alias "GetFileSecurityA" (ByVal lpFileName$, ByVal RequestedInformation&, pSecurityDescriptor As Byte, ByVal nLength&, lpnLengthNeeded&)

' 指定されたセキュリティ記述子からオーナーを表すSID(セキュリティ識別子)を取得
Declare Function Api_GetSecurityDescriptorOwner& Lib "advapi32" Alias "GetSecurityDescriptorOwner" (pSecurityDescriptor As Any, pOwner&, lpbOwnerDefaulted&)

' SID(セキュリティ識別子)に対応する名前の取得
Declare Function Api_LookupAccountSid& Lib "advapi32" Alias "LookupAccountSidA" (ByVal lpSystemName$, ByVal lpSid&, ByVal Name$, cbName&, ByVal ReferencedDomainName$, cbReferencedDomainName&, peUse&)

' リビジョン番号が既知の範囲内にあるか、副権限の数が最大値以下であるかどうかを検証することにより、SID構造体を確認
Declare Function Api_IsValidSid& Lib "advapi32" Alias "IsValidSid" (ByVal pSid&)

#define OWNER_SECURITY_INFORMATION &H1

Var Shared Edit1 As Object
Var Shared Button1 As Object

Edit1.Attach GetDlgItem("Edit1") : Edit1.SetFontSize 14
Button1.Attach GetDlgItem("Button1") : Button1.SetFontSize 14

Var Shared FileName As String

'================================================================
'=
'================================================================
Declare Function SidToString(ByVal Sid As Long) As String
Function SidToString(ByVal Sid As Long) As String
    Var cbAccountName As Long
    Var cbDomainName As Long
    Var strAccountName As String
    Var strDomainName As String
    Var ComputerName As String
    Var Use As Long
    Var Ret As Long

    If Api_IsValidSid(Sid) <> 0 Then
        Ret = Api_LookupAccountSid(ComputerName, Sid, ByVal 0, cbAccountName, ByVal 0, cbDomainName, Use)
        strAccountName = Space$(cbAccountName + 1)
        strDomainName = Space$(cbDomainName + 1)
        Ret = Api_LookupAccountSid(ComputerName, Sid, strAccountName, cbAccountName, strDomainName, cbDomainName, Use)

        If Ret <> 0 Then
            SidToString = Left$(strDomainName, cbDomainName) & "\" & Left$(strAccountName, cbAccountName)
        Else
            SidToString = "<UNKNOWN SID>"
        End If
    Else
        SidToString = "<INVALID SID>"
    End If
End Function

'================================================================
'=
'================================================================
Declare Function GetFileOwner(FullFileName As String) As String
Function GetFileOwner(FullFileName As String) As String
    Var Size As Long
    Var Sid As Long
    Var Dummy As Long
    Var Ret As Long
    
    Ret = Api_GetFileSecurity(FullFileName, OWNER_SECURITY_INFORMATION, ByVal 0, 0, Size)

    If Size <> 0 Then
        Var SecurityDescr(Size - 1) As Byte

        If Api_GetFileSecurity(FullFileName, OWNER_SECURITY_INFORMATION, SecurityDescr(0), Size, Size) <> 0 Then
            Ret = Api_GetSecurityDescriptorOwner(SecurityDescr(0), Sid, Dummy)

            If Sid <> 0 Then
                GetFileOwner = SidToString(Sid)
            Else
                A% = MessageBox(GetWindowtext, "SIDを取得できません!", 0, 2)
            End If
        Else
            A% = MessageBox(GetWindowtext, "ファイルセキュリティ情報を取得できません!", 0, 2)
        End If
     Else
        A% = MessageBox(GetWindowtext, "ファイルセキュリティ情報を取得できません!", 0, 2)
    End If
End Function

'================================================================
'= シェルドロップされたファイル名を取得
'================================================================
Declare Sub Edit1_DropFiles edecl (ByVal DF As Long)
Sub Edit1_DropFiles(ByVal DF As Long)
    Var CN As Long

    CN = GetDropFileCount(DF)
    FileName = GetDropFileName(DF, 0)
    Edit1.SetWindowText FileName
End Sub

'================================================================
'=
'================================================================
Declare Sub Button1_on edecl ()
Sub Button1_on()
    A% = MessageBox(GetWindowtext, GetFileOwner(FileName), 0, 2)
End Sub

'================================================================
'=
'================================================================
While 1
    WaitEvent
Wend
Stop
End