Windows 平台 文件所有者 获取方法

#include <QCoreApplication>
#include <windows.h>
#include <aclapi.h>
#include <QDebug>
  
QString ownerOnWindowsPlatform(const QString& fpath)
{
    try{
        DWORD dwRtnCode = 0;
        PSID pSidOwner = NULL;
        BOOL bRtnBool = TRUE;
        LPTSTR AcctName = NULL;
        LPTSTR DomainName = NULL;
        DWORD dwAcctName = 1, dwDomainName = 1;
        SID_NAME_USE eUse = SidTypeUnknown;
        HANDLE hFile;
        PSECURITY_DESCRIPTOR pSD = NULL;
  
        // Get the handle of the file object.
        hFile = CreateFile(
                    fpath.toStdWString().data(),
                    GENERIC_READ,
                    FILE_SHARE_READ,
                    NULL,
                    OPEN_EXISTING,
                    FILE_ATTRIBUTE_NORMAL,
                    NULL);
  
        // Check GetLastError for CreateFile error code.
        if (hFile == INVALID_HANDLE_VALUE) {
                  DWORD dwErrorCode = 0;
                  dwErrorCode = GetLastError();
                  qDebug() << QString("CreateFile error = %1\n").arg(dwErrorCode);
                  return QString();
        }
  
        // Get the owner SID of the file.
        dwRtnCode = GetSecurityInfo(
                    hFile,
                    SE_FILE_OBJECT,
                    OWNER_SECURITY_INFORMATION,
                    &pSidOwner,
                    NULL,
                    NULL,
                    NULL,
                    &pSD);
  
        // Check GetLastError for GetSecurityInfo error condition.
        if (dwRtnCode != ERROR_SUCCESS) {
            DWORD dwErrorCode = 0;
            dwErrorCode = GetLastError();
            qDebug() << QString("GetSecurityInfo error = %1\n").arg(dwErrorCode);
            return QString();
        }
  
        // First call to LookupAccountSid to get the buffer sizes.
        bRtnBool = LookupAccountSid(
                    NULL,           // local computer
                    pSidOwner,
                    AcctName,
                    (LPDWORD)&dwAcctName,
                    DomainName,
                    (LPDWORD)&dwDomainName,
                    &eUse);
  
        // Reallocate memory for the buffers.
        AcctName = (LPTSTR)GlobalAlloc(
                  GMEM_FIXED,
                  dwAcctName);
  
        // Check GetLastError for GlobalAlloc error condition.
        if (AcctName == NULL) {
            DWORD dwErrorCode = 0;
            dwErrorCode = GetLastError();
            qDebug() << QString("GlobalAlloc error = %1\n").arg(dwErrorCode);
            return QString();
        }
  
        DomainName = (LPTSTR)GlobalAlloc(
                    GMEM_FIXED,
                    dwDomainName);
  
        // Check GetLastError for GlobalAlloc error condition.
        if (DomainName == NULL) {
            DWORD dwErrorCode = 0;
            dwErrorCode = GetLastError();
            qDebug() << QString("GlobalAlloc error = %1\n").arg(dwErrorCode);
            return QString();
        }
  
        // Second call to LookupAccountSid to get the account name.
        bRtnBool = LookupAccountSid(
                    NULL,                   // name of local or remote computer
                    pSidOwner,              // security identifier
                    AcctName,               // account name buffer
                    (LPDWORD)&dwAcctName,   // size of account name buffer
                    DomainName,             // domain name
                    (LPDWORD)&dwDomainName, // size of domain name buffer
                    &eUse
                    );                      // SID type
  
        // Check GetLastError for LookupAccountSid error condition.
        if (bRtnBool == FALSE) {
            DWORD dwErrorCode = 0;
            dwErrorCode = GetLastError();
            if (dwErrorCode == ERROR_NONE_MAPPED){
                qDebug() << QString("Account owner not found for specified SID.");
            }else{
                qDebug() << QString("Error in LookupAccountSid.");
            }
            return QString();
        } else if (bRtnBool == TRUE){
            // return the account name.
            return QString::fromWCharArray(AcctName);
        }
    }catch(...){
        // Don't throw any exception;
        qDebug() << "Catch some exception." ;
    }
  
    return QString();
}
  
int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
  
    qDebug() << ownerOnWindowsPlatform("D:/小工具/sxwnl.exe");
  
    return a.exec();
}

文件名称:main.cpp
使用工具:QtCreator 2.7.0
构建工程:控制台应用

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,908评论 18 139
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,213评论 25 708
  • Spring Boot 参考指南 介绍 转载自:https://www.gitbook.com/book/qbgb...
    毛宇鹏阅读 46,948评论 6 342
  • 界外空山寂寥雪 铮铮穷枝迎风跃 不问江南春几色 兀自笑开点绛唇
    忘无忧阅读 186评论 0 0
  • 网络上曾说一个习惯的养成,至少需要二十一天。 不知道这是指的个人习惯,还是指一个人的生活的生物钟。 我想说的是...
    马家驹阅读 708评论 1 0