LookupAccountName始终失败,并显示错误122(ERROR_INSUFFICIENT_BUFFER)

时间:2020-04-09 15:34:21

标签: c++ c windows wifi sid

也许有人可以在这里启发我。

我正在尝试自动执行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...

}

1 个答案:

答案 0 :(得分:0)

非常感谢Georgy Firsov

我错过了文档中的声明。

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

dw_bufferSizeOfUserAccount = GetLengthSid(p_userSid);