Jump to content

Sky Slate Blueberry Blackcurrant Watermelon Strawberry Orange Banana Apple Emerald Chocolate
Photo

Get File Owner


  • Please log in to reply
4 replies to this topic
panofish
  • Members
  • 179 posts
  • Last active: Apr 24 2014 03:24 PM
  • Joined: 05 Feb 2007

I would like to use a windows dll (perhaps advapi32.dll) to get the owner of a given file.  I do not know how.  This post at http://www.autohotke...-and-stringsid/  looks like it demonstrates enough understanding to make the dll call to get the owner of a file.  I could be wrong, but I believe the function is GetSecurityDescriptorOwner.

 

Does anyone know how to get the owner of a file in ahk without using xcacls.exe.... preferably with a system dll call?



TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006
File = %A_WinDir%\win.ini ; change this to any file or folder
Owner := GetOwner( File ) ; assigns owner to var Owner

Msgbox % Owner

GetOwner(p) {
    colObj 	:= ComObjGet("winmgmts:").ExecQuery("Associators Of {"
		. "Win32_LogicalFileSecuritySetting='" p "'} Where As"
		. "socClass= Win32_LogicalFileOwner ResultRole=Owner")

    For itm in colObj
        Return itm.ReferencedDomainName "\" itm.AccountName
}

Posted Image

don't duplicate, iterate!


uname
  • Members
  • 194 posts
  • Last active: Oct 17 2015 08:46 AM
  • Joined: 13 Jun 2012
GetFileOwner(ByRef path) {
    FolderSecuritySettings := ComObjGet("winmgmts:").Get("Win32_LogicalFileSecuritySetting='" . path . "'")

    return 0 = FolderSecuritySettings.GetSecurityDescriptor(SD)
        ? SD.Owner.Domain . "\" . SD.Owner.Name
        : "Couldn't retrieve security descriptor."
}

MsgBox % GetFileOwner(A_ScriptFullPath)

 This code does not work on autohotkey for some reason.

 

Edit:

 

GetFileOwner(ByRef path) {
    FolderSecuritySettings := ComObjGet("winmgmts:").Get("Win32_LogicalFileSecuritySetting='" . path . "'")
    VarSetCapacity(objRef, A_PtrSize, 0)

    if !FolderSecuritySettings.GetSecurityDescriptor(ComObjParameter(0x4009, &objRef, 1)) {
        objRef := NumGet(objRef)
        SD := ComObj(objRef)
        owner := SD.Owner.Domain . "\" . SD.Owner.Name
        ObjRelease(objRef)
        return owner
    }
}
MsgBox % GetFileOwner(A_ScriptFullPath)


TLM
  • Administrators
  • 3864 posts
  • Last active:
  • Joined: 21 Aug 2006

uname you have to use Win32_LogicalFileSecuritySetting with Win32_LogicalFileOwner association.

LogicalFileSecuritySetting gets the security data but can’t get paths owner.

You have to use LogicalFileOwner to get the SID 1st then the Win32 class can get owner and domain.

ExecQuery() can still be used, but the WMI call is 2 stage.

 

See my above example.


Posted Image

don't duplicate, iterate!


uname
  • Members
  • 194 posts
  • Last active: Oct 17 2015 08:46 AM
  • Joined: 13 Jun 2012

TLM thank you your example works fine. My code is just simple translation of autoit script:

 

Func _fileOwner($varName)
    Dim $objSD
    Local $oWMIService = ObjGet("winmgmts:")
    Local $oFolderSecuritySettings = $oWMIService.Get("Win32_LogicalFileSecuritySetting='" & $varName & "'")
    Local $intRetVal = $oFolderSecuritySettings.GetSecurityDescriptor($objSD)

    If $intRetVal = 0 Then
        Local $objStr = $objSD.Owner.Domain & "\" & $objSD.Owner.Name;
        Return($objStr)
    Else
        Return("Couldn't retrieve security descriptor.")
    EndIf
EndFunc

MsgBox('', '', _fileOwner(@ScriptFullPath))