How To Get File Owner Under Winnt

I'm writing a windows console app and need to collect some file information for some files in a directory on the local hard drive ( date created, date modified and file owner).  Getting the basic file attributes and file dates is no problem.  But I haven't been able to figure out how to get the owner.  CFile doesn't seem to have a method.  I'm sure someone out there has the answer (probably a head slapper too).  Mind sharing??


sturgesAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nietodCommented:
What do you mean by "owner"  In windows a file doesn't have an "owner", at least not in any way I can think of.
0
jkrCommented:
Using NT, you can retrieve this kind of information by calling GetFileSecurity()' supplying OWNER_SECURITY_INFORMATION as the fisrst parameter. 'GetFileSecurity()' retrieves a SID, so if you want to know the username, you'll have to use 'LookupAccountSid()'.


Feel free to ask if you need more information or an example on how to do this!
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
Sorry, Todd, haven't seen your comment - 'owner information' is only available on NT...
0
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

nietodCommented:
Sorry?  You must have missunderstood me.  I didn't realize that it even stored that sort of info.  (I don't do any security-specific stuff.)
0
sturgesAuthor Commented:
This is for Windows NT running NTFS which is the only place real "owner" information is stored for files.  I could use an example, if you have the time.  I did stumble onto GetFileSecurity() -- after posing this question, but haven't had much luck getting it to give me what I want.
0
jkrCommented:
OK, here's an example:

#include <lmcons.h>

#pragma comment (   lib,    "advapi32.lib")

int GetFileOwner    (   char*   pszFile,    char*   acBuffer,   ULONG   ulSize)
{
    PSID            psid;
    DWORD           dwSidLength;
    DWORD           dwNeeded;

    DWORD           dwErr;

    DWORD           dwNameSize;

    char            acReferencedDomain  [   LM20_DNLEN  +   1];
    DWORD           dwDomainBufSize     =   sizeof  (   acReferencedDomain);

    SID_NAME_USE    eUse;

    dwNameSize  =   ulSize;

    if  (   !GetFileSecurity    (   pszFile,
                                    OWNER_SECURITY_INFORMATION,
                                    NULL,
                                    0,
                                    &dwNeeded
                                )
        )
        {
            dwErr   =   GetLastError    ();

            if  (   ERROR_INSUFFICIENT_BUFFER   !=  dwErr)
                {
                    return  (   dwErr);
                }

            dwSidLength =   dwNeeded;

            psid    =   ( PSID) LocalAlloc  (   LPTR,
                                                dwSidLength
                                            );

            if  (   !GetFileSecurity    (   pszFile,
                                            OWNER_SECURITY_INFORMATION,
                                            psid,
                                            dwSidLength,
                                            &dwNeeded
                                        )
                )
                {
                    return  (   GetLastError    ());
                }

            //  lookup clear text name of the owner
            if  (   !LookupAccountSid   (   NULL,
                                            psid,
                                            acBuffer,
                                            &dwNameSize,
                                            acReferencedDomain,
                                            &dwDomainBufSize,
                                            &eUse
                                        )
                )
                {
                    return  (   GetLastError    ());
                }

            LocalFree   (   psid);
        }

    return  (   0);
}

Simply supply a character buffer that receives the username...
0
sturgesAuthor Commented:
Adjusted points to 150
0
sturgesAuthor Commented:
Oops.  GetFileSecurity() gets a Security Descriptor.  I needed GetSecurityDescriptorOwner() to get the SID before the call to LookupAccountSid().  The new code looks like this:

int GetFileOwner (char *pszFile, char *acBuffer, ULONG ulSize)
{
      PSECURITY_DESCRIPTOR pSecurityDescriptor;
      BOOL            bOwnerDefaulted;
    PSID            psid;
    DWORD           dwSidLength = 0;
      DWORD           dwSdLength = 0;
    DWORD           dwNeeded = 0;
    DWORD           dwErr;
    DWORD           dwNameSize;
    char            acReferencedDomain[LM20_DNLEN+1];
    DWORD           dwDomainBufSize     =   sizeof  (   acReferencedDomain);
    SID_NAME_USE    eUse;
    dwNameSize  =   ulSize;
      
    if( !GetFileSecurity (pszFile,
                            OWNER_SECURITY_INFORMATION,
                            NULL,
                            0,
                            &dwNeeded) )
    {
        dwErr = GetLastError();
        if(ERROR_INSUFFICIENT_BUFFER != dwErr)
        {
            return(dwErr);
        }

        dwSdLength =   dwNeeded;
        pSecurityDescriptor = (PSECURITY_DESCRIPTOR) LocalAlloc (LPTR, dwSdLength);

        if( !GetFileSecurity( pszFile,
                              OWNER_SECURITY_INFORMATION,
                              pSecurityDescriptor,
                              dwSdLength,
                              &dwNeeded))
        {
                  dwErr = GetLastError();
            return(dwErr);
        }

            if( !IsValidSecurityDescriptor (pSecurityDescriptor) )
            {
                  dwErr = GetLastError();
                  return(dwErr);
            }

            if( !GetSecurityDescriptorOwner(pSecurityDescriptor, &psid, &bOwnerDefaulted) )
            {
                  dwErr = GetLastError();
                  return(dwErr);
            }

        //  check SID for validity
            if( !IsValidSid(psid) )
            {
                  dwErr = GetLastError();
                  return(dwErr);
            }

        //  lookup clear text name of the owner
        if( !LookupAccountSid(NULL,
                              psid,
                              acBuffer,
                              &dwNameSize,
                              acReferencedDomain,
                              &dwDomainBufSize,
                              &eUse) )
        {
                  dwErr = GetLastError();
            return(dwErr);
        }

        LocalFree(psid);
      }

    return(0);
}

I added some error checking -- just for grins.

Anyway, thanks for getting me there!

0
jkrCommented:
Hmm - 'LookupAccountSid()' should also work...
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.