Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/130.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在Windows上使用CreateFile创建具有LPSECURITY_属性的文件?_C++_Windows_Winapi - Fatal编程技术网

C++ 如何在Windows上使用CreateFile创建具有LPSECURITY_属性的文件?

C++ 如何在Windows上使用CreateFile创建具有LPSECURITY_属性的文件?,c++,windows,winapi,C++,Windows,Winapi,给你 我想通过CreateFile创建一个具有SECURITY\u属性的文件,当我在windows帐户用户a下创建该文件时,其他windows用户B不应访问该文件 我找到了这个 但还是不知道如何与某个用户交往 但还是不知道如何与某个用户交往 您需要首先获取某个用户的SID 下面是一些步骤 验证输入参数 为可能较大的SID和域名创建缓冲区 够了 在循环中,调用LookupAccountName检索 提供了帐户名。如果SID的缓冲区或 域名不够大,需要的缓冲区大小为 分别在cbSid或cchDom

给你

我想通过
CreateFile
创建一个具有
SECURITY\u属性的文件,当我在windows帐户用户a下创建该文件时,其他windows用户B不应访问该文件

我找到了这个

但还是不知道如何与某个用户交往

但还是不知道如何与某个用户交往

您需要首先获取某个用户的SID

下面是一些步骤

  • 验证输入参数
  • 为可能较大的SID和域名创建缓冲区 够了
  • 在循环中,调用
    LookupAccountName
    检索 提供了帐户名。如果SID的缓冲区或 域名不够大,需要的缓冲区大小为 分别在
    cbSid
    cchDomainName
    中返回,并返回一个新的缓冲区 在下一次调用
    LookupAccountName
    之前分配。注意 当
    lpSystemName
    参数设置为空
  • 释放为域名缓冲区分配的内存
  • 然后将SID传递给函数

    setEntriesAcl函数创建一个新的访问控制列表(ACL) 通过将新的访问控制或审核控制信息合并到 现有ACL结构

    修改代码:

    #pragma comment(lib, "advapi32.lib")
    
    #include <windows.h>
    #include <stdio.h>
    #include <aclapi.h>
    #include <tchar.h>
    #include <mq.h.>
    
    HRESULT GetSid(
        LPCWSTR wszAccName,
        PSID* ppSid
    )
    {
    
        // Validate the input parameters.  
        if (wszAccName == NULL || ppSid == NULL)
        {
            return MQ_ERROR_INVALID_PARAMETER;
        }
    
        // Create buffers that may be large enough.  
        // If a buffer is too small, the count parameter will be set to the size needed.  
        const DWORD INITIAL_SIZE = 32;
        DWORD cbSid = 0;
        DWORD dwSidBufferSize = INITIAL_SIZE;
        DWORD cchDomainName = 0;
        DWORD dwDomainBufferSize = INITIAL_SIZE;
        WCHAR* wszDomainName = NULL;
        SID_NAME_USE eSidType;
        DWORD dwErrorCode = 0;
        HRESULT hr = MQ_OK;
    
        // Create buffers for the SID and the domain name.  
        *ppSid = (PSID) new BYTE[dwSidBufferSize];
        if (*ppSid == NULL)
        {
            return MQ_ERROR_INSUFFICIENT_RESOURCES;
        }
        memset(*ppSid, 0, dwSidBufferSize);
        wszDomainName = new WCHAR[dwDomainBufferSize];
        if (wszDomainName == NULL)
        {
            return MQ_ERROR_INSUFFICIENT_RESOURCES;
        }
        memset(wszDomainName, 0, dwDomainBufferSize * sizeof(WCHAR));
    
        // Obtain the SID for the account name passed.  
        for (; ; )
        {
    
            // Set the count variables to the buffer sizes and retrieve the SID.  
            cbSid = dwSidBufferSize;
            cchDomainName = dwDomainBufferSize;
            if (LookupAccountNameW(
                NULL,            // Computer name. NULL for the local computer  
                wszAccName,
                *ppSid,          // Pointer to the SID buffer. Use NULL to get the size needed,  
                &cbSid,          // Size of the SID buffer needed.  
                wszDomainName,   // wszDomainName,  
                &cchDomainName,
                &eSidType
            ))
            {
                if (IsValidSid(*ppSid) == FALSE)
                {
                    wprintf(L"The SID for %s is invalid.\n", wszAccName);
                    dwErrorCode = MQ_ERROR;
                }
                break;
            }
            dwErrorCode = GetLastError();
    
            // Check if one of the buffers was too small.  
            if (dwErrorCode == ERROR_INSUFFICIENT_BUFFER)
            {
                if (cbSid > dwSidBufferSize)
                {
    
                    // Reallocate memory for the SID buffer.  
                    wprintf(L"The SID buffer was too small. It will be reallocated.\n");
                    FreeSid(*ppSid);
                    *ppSid = (PSID) new BYTE[cbSid];
                    if (*ppSid == NULL)
                    {
                        return MQ_ERROR_INSUFFICIENT_RESOURCES;
                    }
                    memset(*ppSid, 0, cbSid);
                    dwSidBufferSize = cbSid;
                }
                if (cchDomainName > dwDomainBufferSize)
                {
    
                    // Reallocate memory for the domain name buffer.  
                    wprintf(L"The domain name buffer was too small. It will be reallocated.\n");
                    delete[] wszDomainName;
                    wszDomainName = new WCHAR[cchDomainName];
                    if (wszDomainName == NULL)
                    {
                        return MQ_ERROR_INSUFFICIENT_RESOURCES;
                    }
                    memset(wszDomainName, 0, cchDomainName * sizeof(WCHAR));
                    dwDomainBufferSize = cchDomainName;
                }
            }
            else
            {
                wprintf(L"LookupAccountNameW failed. GetLastError returned: %d\n", dwErrorCode);
                hr = HRESULT_FROM_WIN32(dwErrorCode);
                break;
            }
        }
    
        delete[] wszDomainName;
        return hr;
    }
    
    void main()
    {
        PSID sid;
        GetSid(L"strives", &sid); // enter a user name
        DWORD dwRes, dwDisposition;
        PACL pACL = NULL;
        PSECURITY_DESCRIPTOR pSD = NULL;
        EXPLICIT_ACCESS ea;
        SECURITY_ATTRIBUTES sa;
        HANDLE lRes = NULL;
        // Initialize an EXPLICIT_ACCESS structure for an ACE.
        // The ACE will allow Everyone read access to the key.
        ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
        ea.grfAccessPermissions = GENERIC_ALL;
        ea.grfAccessMode = SET_ACCESS;
        ea.grfInheritance = NO_INHERITANCE;
        ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
        ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
        ea.Trustee.ptstrName = (LPTSTR)sid;
    
        // Create a new ACL that contains the new ACEs.
        dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
        if (ERROR_SUCCESS != dwRes)
        {
            _tprintf(_T("SetEntriesInAcl Error %u\n"), GetLastError());
            goto Cleanup;
        }
    
        // Initialize a security descriptor.  
        pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
            SECURITY_DESCRIPTOR_MIN_LENGTH);
        if (NULL == pSD)
        {
            _tprintf(_T("LocalAlloc Error %u\n"), GetLastError());
            goto Cleanup;
        }
    
        if (!InitializeSecurityDescriptor(pSD,
            SECURITY_DESCRIPTOR_REVISION))
        {
            _tprintf(_T("InitializeSecurityDescriptor Error %u\n"),
                GetLastError());
            goto Cleanup;
        }
    
        // Add the ACL to the security descriptor. 
        if (!SetSecurityDescriptorDacl(pSD,
            TRUE,     // bDaclPresent flag   
            pACL,
            FALSE))   // not a default DACL 
        {
            _tprintf(_T("SetSecurityDescriptorDacl Error %u\n"),
                GetLastError());
            goto Cleanup;
        }
    
        // Initialize a security attributes structure.
        sa.nLength = sizeof(SECURITY_ATTRIBUTES);
        sa.lpSecurityDescriptor = pSD;
        sa.bInheritHandle = FALSE;
    
        // Use the security attributes to set the security descriptor 
        // when you create a key.
        lRes =  CreateFile(_T("D:\\File.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
            &sa, OPEN_ALWAYS, 0, NULL);
        if (lRes != NULL)
        {
            _tprintf(_T("Create file success\n"));
        }
       
    Cleanup:
    
        if (pACL)
            LocalFree(pACL);
        if (pSD)
            LocalFree(pSD);
        if (lRes)
           CloseHandle(lRes);
        return;
    
    }
    
    #pragma注释(lib,“advapi32.lib”)
    #包括
    #包括
    #包括

    但还是不知道如何与某个用户交往

    您需要首先获取某个用户的SID

    下面是一些步骤

  • 验证输入参数
  • 为可能较大的SID和域名创建缓冲区 够了
  • 在循环中,调用
    LookupAccountName
    检索 提供了帐户名。如果SID的缓冲区或 域名不够大,需要的缓冲区大小为 分别在
    cbSid
    cchDomainName
    中返回,并返回一个新的缓冲区 在下一次调用
    LookupAccountName
    之前分配。注意 当
    lpSystemName
    参数设置为空
  • 释放为域名缓冲区分配的内存
  • 然后将SID传递给函数

    setEntriesAcl函数创建一个新的访问控制列表(ACL) 通过将新的访问控制或审核控制信息合并到 现有ACL结构

    修改代码:

    #pragma comment(lib, "advapi32.lib")
    
    #include <windows.h>
    #include <stdio.h>
    #include <aclapi.h>
    #include <tchar.h>
    #include <mq.h.>
    
    HRESULT GetSid(
        LPCWSTR wszAccName,
        PSID* ppSid
    )
    {
    
        // Validate the input parameters.  
        if (wszAccName == NULL || ppSid == NULL)
        {
            return MQ_ERROR_INVALID_PARAMETER;
        }
    
        // Create buffers that may be large enough.  
        // If a buffer is too small, the count parameter will be set to the size needed.  
        const DWORD INITIAL_SIZE = 32;
        DWORD cbSid = 0;
        DWORD dwSidBufferSize = INITIAL_SIZE;
        DWORD cchDomainName = 0;
        DWORD dwDomainBufferSize = INITIAL_SIZE;
        WCHAR* wszDomainName = NULL;
        SID_NAME_USE eSidType;
        DWORD dwErrorCode = 0;
        HRESULT hr = MQ_OK;
    
        // Create buffers for the SID and the domain name.  
        *ppSid = (PSID) new BYTE[dwSidBufferSize];
        if (*ppSid == NULL)
        {
            return MQ_ERROR_INSUFFICIENT_RESOURCES;
        }
        memset(*ppSid, 0, dwSidBufferSize);
        wszDomainName = new WCHAR[dwDomainBufferSize];
        if (wszDomainName == NULL)
        {
            return MQ_ERROR_INSUFFICIENT_RESOURCES;
        }
        memset(wszDomainName, 0, dwDomainBufferSize * sizeof(WCHAR));
    
        // Obtain the SID for the account name passed.  
        for (; ; )
        {
    
            // Set the count variables to the buffer sizes and retrieve the SID.  
            cbSid = dwSidBufferSize;
            cchDomainName = dwDomainBufferSize;
            if (LookupAccountNameW(
                NULL,            // Computer name. NULL for the local computer  
                wszAccName,
                *ppSid,          // Pointer to the SID buffer. Use NULL to get the size needed,  
                &cbSid,          // Size of the SID buffer needed.  
                wszDomainName,   // wszDomainName,  
                &cchDomainName,
                &eSidType
            ))
            {
                if (IsValidSid(*ppSid) == FALSE)
                {
                    wprintf(L"The SID for %s is invalid.\n", wszAccName);
                    dwErrorCode = MQ_ERROR;
                }
                break;
            }
            dwErrorCode = GetLastError();
    
            // Check if one of the buffers was too small.  
            if (dwErrorCode == ERROR_INSUFFICIENT_BUFFER)
            {
                if (cbSid > dwSidBufferSize)
                {
    
                    // Reallocate memory for the SID buffer.  
                    wprintf(L"The SID buffer was too small. It will be reallocated.\n");
                    FreeSid(*ppSid);
                    *ppSid = (PSID) new BYTE[cbSid];
                    if (*ppSid == NULL)
                    {
                        return MQ_ERROR_INSUFFICIENT_RESOURCES;
                    }
                    memset(*ppSid, 0, cbSid);
                    dwSidBufferSize = cbSid;
                }
                if (cchDomainName > dwDomainBufferSize)
                {
    
                    // Reallocate memory for the domain name buffer.  
                    wprintf(L"The domain name buffer was too small. It will be reallocated.\n");
                    delete[] wszDomainName;
                    wszDomainName = new WCHAR[cchDomainName];
                    if (wszDomainName == NULL)
                    {
                        return MQ_ERROR_INSUFFICIENT_RESOURCES;
                    }
                    memset(wszDomainName, 0, cchDomainName * sizeof(WCHAR));
                    dwDomainBufferSize = cchDomainName;
                }
            }
            else
            {
                wprintf(L"LookupAccountNameW failed. GetLastError returned: %d\n", dwErrorCode);
                hr = HRESULT_FROM_WIN32(dwErrorCode);
                break;
            }
        }
    
        delete[] wszDomainName;
        return hr;
    }
    
    void main()
    {
        PSID sid;
        GetSid(L"strives", &sid); // enter a user name
        DWORD dwRes, dwDisposition;
        PACL pACL = NULL;
        PSECURITY_DESCRIPTOR pSD = NULL;
        EXPLICIT_ACCESS ea;
        SECURITY_ATTRIBUTES sa;
        HANDLE lRes = NULL;
        // Initialize an EXPLICIT_ACCESS structure for an ACE.
        // The ACE will allow Everyone read access to the key.
        ZeroMemory(&ea, sizeof(EXPLICIT_ACCESS));
        ea.grfAccessPermissions = GENERIC_ALL;
        ea.grfAccessMode = SET_ACCESS;
        ea.grfInheritance = NO_INHERITANCE;
        ea.Trustee.TrusteeForm = TRUSTEE_IS_SID;
        ea.Trustee.TrusteeType = TRUSTEE_IS_USER;
        ea.Trustee.ptstrName = (LPTSTR)sid;
    
        // Create a new ACL that contains the new ACEs.
        dwRes = SetEntriesInAcl(1, &ea, NULL, &pACL);
        if (ERROR_SUCCESS != dwRes)
        {
            _tprintf(_T("SetEntriesInAcl Error %u\n"), GetLastError());
            goto Cleanup;
        }
    
        // Initialize a security descriptor.  
        pSD = (PSECURITY_DESCRIPTOR)LocalAlloc(LPTR,
            SECURITY_DESCRIPTOR_MIN_LENGTH);
        if (NULL == pSD)
        {
            _tprintf(_T("LocalAlloc Error %u\n"), GetLastError());
            goto Cleanup;
        }
    
        if (!InitializeSecurityDescriptor(pSD,
            SECURITY_DESCRIPTOR_REVISION))
        {
            _tprintf(_T("InitializeSecurityDescriptor Error %u\n"),
                GetLastError());
            goto Cleanup;
        }
    
        // Add the ACL to the security descriptor. 
        if (!SetSecurityDescriptorDacl(pSD,
            TRUE,     // bDaclPresent flag   
            pACL,
            FALSE))   // not a default DACL 
        {
            _tprintf(_T("SetSecurityDescriptorDacl Error %u\n"),
                GetLastError());
            goto Cleanup;
        }
    
        // Initialize a security attributes structure.
        sa.nLength = sizeof(SECURITY_ATTRIBUTES);
        sa.lpSecurityDescriptor = pSD;
        sa.bInheritHandle = FALSE;
    
        // Use the security attributes to set the security descriptor 
        // when you create a key.
        lRes =  CreateFile(_T("D:\\File.txt"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ,
            &sa, OPEN_ALWAYS, 0, NULL);
        if (lRes != NULL)
        {
            _tprintf(_T("Create file success\n"));
        }
       
    Cleanup:
    
        if (pACL)
            LocalFree(pACL);
        if (pSD)
            LocalFree(pSD);
        if (lRes)
           CloseHandle(lRes);
        return;
    
    }
    
    #pragma注释(lib,“advapi32.lib”)
    #包括
    #包括
    #include

    您可能希望获取用户的SID。将其替换为示例中的
    AllocateAndInitializedSid
    调用。您可能希望获取用户的SID。将其替换为示例中的
    allocateAndInitializedId
    调用。