#include <windows.h>
#include <sddl.h>
#include <iostream>
std::wstring GetCurrentSID()
{
DWORD sidSize = 0;
std::wstring sidStringW = LR"()";
// Get the required buffer sizes for the SID and domain name
if (!GetUserObjectInformation(GetProcessWindowStation(), UOI_USER_SID, NULL, 0, &sidSize))
{
OutputDebugString(L"Error getting user SID buffer size\n");
return sidStringW;
}
// Allocate the necessary buffer
PSID sid = (PSID)malloc(sidSize);
// Get the user SID
if (!GetUserObjectInformation(GetProcessWindowStation(), UOI_USER_SID, sid, sidSize, NULL))
{
OutputDebugString(L"Error getting user SID\n");
return sidStringW;
}
// Convert the SID to a string
LPWSTR pSidStringW = nullptr;
if (!ConvertSidToStringSidW(sid, &pSidStringW))
{
OutputDebugString(L"Error converting SID to string\n");
return sidStringW;
}
sidStringW = pSidStringW;
if (nullptr != pSidStringW)
{
LocalFree(pSidStringW);
}
pSidStringW = nullptr;
// Free the buffer
if (nullptr != sid)
{
free(sid);
}
sid = nullptr;
return sidStringW;
}
获取当前用户SID
最新推荐文章于 2023-04-15 23:09:26 发布
这段代码展示了如何使用WindowsAPI获取当前进程窗口站的用户安全标识符(SID),然后将其转换为字符串形式。主要涉及了`GetUserObjectInformation`,`ConvertSidToStringSidW`等函数。
4451

被折叠的 条评论
为什么被折叠?



