8000 Refactor LookupAccountSid P/Invoke in Process.cs by iSazonov · Pull Request #9197 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -779,29 +779,24 @@ private static string RetrieveProcessUserName(Process process)

var tokenUser = Marshal.PtrToStructure<Win32Native.TOKEN_USER>(tokenUserInfo);

// Set the default length to be 256, so it will be s CA48 ufficient for most cases.
int userNameLength = 256, domainNameLength = 256;
var userNameStr = new StringBuilder(userNameLength);
var domainNameStr = new StringBuilder(domainNameLength);
// Max username is defined as UNLEN = 256 in lmcons.h
// Max domainname is defined as DNLEN = CNLEN = 15 in lmcons.h
// The buffer length must be +1, last position is for a null string terminator.
int userNameLength = 257;
int domainNameLength = 16;
Span<char> userNameStr = stackalloc char[userNameLength];
Span<char> domainNameStr = stackalloc char[domainNameLength];
Win32Native.SID_NAME_USE accountType;

// userNameLength and domainNameLength will be set to actual lengths.
if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType))
{
error = Marshal.GetLastWin32Error();
if (error == Win32Native.ERROR_INSUFFICIENT_BUFFER)
{
userNameStr.EnsureCapacity(userNameLength);
domainNameStr.EnsureCapacity(domainNameLength);

if (!Win32Native.LookupAccountSid(null, tokenUser.User.Sid, userNameStr, ref userNameLength, domainNameStr, ref domainNameLength, out accountType)) { break; }
}
else
{
break;
}
break;
}

userName = domainNameStr + "\\" + userNameStr;
// TODO: Use Concat() from .Net Core 3.0
// public static string Concat(ReadOnlySpan<char> str0, ReadOnlySpan<char> str1, ReadOnlySpan<char> str2)
userName = string.Concat(domainNameStr.Slice(0, domainNameLength).ToString(), "\\", userNameStr.Slice(0, userNameLength).ToString());
} while (false);
}
catch (NotSupportedException)
Expand Down
Loading
0