已解决 - LookupAccountName 总是失败并出现错误 122 (ERROR_INSUFFICIENT_BUFFER)

SOLVED - LookupAccountName Always Fails With Error 122 (ERROR_INSUFFICIENT_BUFFER)

这里也许有人可以开导我。

我正在尝试自动执行 WiFi 连接过程,其中 SSID 由序列号确定。因为这总是不同的,所以我想我每次想连接时都需要保存一个临时配置文件。

WlanSaveTemporaryProfile() 想要 LPCWSTR strAllUserProfileSecurity 定义此配置文件的权限。到目前为止,兔子洞让我尝试使用 LookupAccountNameW()。我试过 AllocateAndInitializeSid() 无济于事。我尝试插入一个空缓冲区,结果相同。在这两种情况下,我都会收到错误 122,表示缓冲区太小。

在此真诚感谢任何帮助。


这是相关代码。主要根据 Microsoft 文档中的示例构建。

DWORD GetStringSecurityDescriptor(
    PWCHAR ps_securityDescriptor, /* This needs to be populated when this function completes. */
    PULONG pul_securityDescriptorLen,
    LPWSTR ps_accountName
    )
{
    DWORD dw_result = NULL; 
    DWORD dw_lastError = NULL;
    DWORD dw_bufferSizeOfUserAccount = NULL;

    /* Create a security descriptor for the profile. */
    SECURITY_DESCRIPTOR secDesc;
    bool success = InitializeSecurityDescriptor(&secDesc, SECURITY_DESCRIPTOR_REVISION);
    if (!success)
    {
        wprintf(L"Security Descriptor Initialization Failed.\n");
    }

    PSID p_userSid = NULL;
    /* Attempt 2: Straight up malloc the memory. Doesn't work any better.*/
    //p_userSid = malloc(100);

    /* Attempt 1: Allocate and Initialize an SID for LookupAccountNameW(). */
    SID_IDENTIFIER_AUTHORITY auth = SECURITY_WORLD_SID_AUTHORITY;
    BOOL b_sidReady = AllocateAndInitializeSid(
        &auth,
        6,
        SECURITY_NULL_RID,
        SECURITY_WORLD_RID,
        SECURITY_LOCAL_RID,
        SECURITY_LOCAL_LOGON_RID,
        SECURITY_CREATOR_OWNER_RID,
        SECURITY_CREATOR_GROUP_RID,
        0, 0,
        &p_userSid
        );

    LPDWORD buf = &dw_bufferSizeOfUserAccount;
    WCHAR domainName[1000] = { 0 }; // Perhaps DNLEN + 1 was too small?
    DWORD domainNameLen = 1000;
    SID_NAME_USE use = SidTypeUser;

    // Currently failing. dw_bufferSizeOfUserAccount still recieves a 28, so that wasn't it.
    success = LookupAccountNameW(
        NULL, 
        ps_accountName, 
        p_userSid, 
        buf, 
        domainName, 
        &domainNameLen, 
        &use);
    if (!success) 
    {
        dw_lastError = GetLastError();
        switch (dw_lastError)
        {
        case ERROR_INSUFFICIENT_BUFFER: // LookupAccountNameW() always ends up here.
            wprintf(L"The data area passed to a system call is too small.\n");
            FreeSid(p_userSid);
            return dw_lastError;
        default:
            wprintf(L"Looking up Account Name failed. See Error 0x%x.\n", dw_lastError);
            FreeSid(p_userSid);
            return dw_lastError;
        }
    }

// ... more code irrelevant to this problem...

}

非常感谢Georgy Firsov

我遗漏了文档中的一个声明。

通过计算 SID 的大小并将其存储在 dw_bufferSizeOfUserAccount 中,函数 运行 成功。

dw_bufferSizeOfUserAccount = GetLengthSid(p_userSid);