共用のコード
#pragma once
#include <string>
#include <format>
#include <vector>
std::vector<BYTE> GetTokenInformationInByteArray(
HANDLE TokenHandle,
TOKEN_INFORMATION_CLASS TokenInformationClass)
{
DWORD required;
if (!::GetTokenInformation(TokenHandle,
TOKEN_INFORMATION_CLASS::TokenUser,
nullptr, 0, &required))
{
auto err = ::GetLastError();
if (err != ERROR_INSUFFICIENT_BUFFER)
{
return {};
}
}
std::vector<BYTE> buffer(required, 0);
if (!::GetTokenInformation(TokenHandle,
TOKEN_INFORMATION_CLASS::TokenUser,
buffer.data(), required, &required))
{
return {};
}
if (buffer.size() != required)
{
return {};
}
return std::move(buffer);
}
constexpr ULONGLONG LUIDToUInt64(const LUID& luid) noexcept {
return ((ULONGLONG)luid.HighPart << 32) | luid.LowPart;
}
TOKEN_INFORMATION_CLASS::TokenUser
#include <iostream>
#define STRICT
#define NOMINMAX
#include <Windows.h>
#include <sddl.h>
#include "Utility.hpp"
int main()
{
auto tokenHandle{ ::GetCurrentProcessToken() };
auto infoData{ GetTokenInformationInByteArray(tokenHandle, TOKEN_INFORMATION_CLASS::TokenUser) };
auto& tokenUser{ *std::bit_cast<TOKEN_USER*>(infoData.data()) };
std::cout << std::format(
"SID:{}\nAttribute:{:08X}",
SIDToString(tokenUser.User.Sid),
tokenUser.User.Attributes) << std::endl;
return NOERROR;
}
TOKEN_INFORMATION_CLASS::TokenSource
#include <iostream>
#define STRICT
#define NOMINMAX
#include <Windows.h>
#include "Utility.hpp"
int main()
{
auto tokenHandle = ::GetCurrentProcessToken();
TOKEN_SOURCE tokenSource;
DWORD copied;
if (!::GetTokenInformation(tokenHandle,
TOKEN_INFORMATION_CLASS::TokenSource, &tokenSource, sizeof(tokenSource), &copied))
{
return ::GetLastError();
}
std::cout << std::format(
"{}\n{:016X}",
tokenSource.SourceName,
LUIDToUInt64(tokenSource.SourceIdentifier)) << std::endl;
return NOERROR;
}
TOKEN_INFORMATION_CLASS::TokenOrigin
#include <iostream>
#define STRICT
#define NOMINMAX
#include <Windows.h>
#include <sddl.h>
#include "Utility.hpp"
int main()
{
auto tokenHandle{ ::GetCurrentProcessToken() };
auto infoData{ GetTokenInformationInByteArray(tokenHandle, TOKEN_INFORMATION_CLASS::TokenOrigin) };
auto& tokenOrigin{ *std::bit_cast<TOKEN_ORIGIN*>(infoData.data()) };
std::cout
<< std::format("LUID:{:016X}",
LUIDToUInt64(tokenOrigin.OriginatingLogonSession))
<< std::endl;
return NOERROR;
}