【发布时间】:2020-04-09 15:34:21
【问题描述】:
也许有人可以在这里启发我。
我正在尝试自动化 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...
}
【问题讨论】:
-
Microdoft 文档:“
ReferencedDomainName... 如果此参数为 NULL,则函数返回所需的缓冲区大小”。您可以调用此函数两次 - 一次用于估计缓冲区大小,第二次将成功,如果这是一个问题,因为您将分配正确的内存量。 -
此外:“
cbSid指向变量的指针。在输入时,此值指定 Sid 缓冲区的大小(以字节为单位)。如果函数因缓冲区太小或 @ 987654328@ 为零,此变量接收所需的缓冲区大小。”看起来您在这里遇到了一些问题,因为您在此参数中传递了指向零值的指针(DWORD dw_bufferSizeOfUserAccount = NULL;及更高版本:LPDWORD buf = &dw_bufferSizeOfUserAccount;) -
我怎么能错过呢?谢谢!