Qt QML 杂记

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

2017-11-20  本文已影响14人  赵者也
#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
构建工程:控制台应用

上一篇下一篇

猜你喜欢

热点阅读