在Windows操作系统中,当以管理员身份运行应用程序时,获取当前登录的非管理员用户名可以通过以下几种方法实现:
以下是一个使用C#编写的示例代码,展示如何在以管理员身份运行的应用程序中获取当前登录的非管理员用户名:
using System;
using System.Runtime.InteropServices;
class Program
{
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
[DllImport("kernel32.dll")]
static extern IntPtr GetCurrentProcess();
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool OpenProcessToken(IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern bool GetTokenInformation(IntPtr hToken, TOKEN_INFORMATION_CLASS TokenInformationClass, IntPtr TokenInformation, UInt32 TokenInformationLength, out UInt32 ReturnLength);
[DllImport("kernel32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool CloseHandle(IntPtr hObject);
[StructLayout(LayoutKind.Sequential)]
public struct TOKEN_USER
{
public _SID_AND_ATTRIBUTES User;
}
[StructLayout(LayoutKind.Sequential)]
public struct _SID_AND_ATTRIBUTES
{
public IntPtr Sid;
public uint Attributes;
}
public enum TOKEN_INFORMATION_CLASS
{
TokenUser = 1,
TokenGroups,
TokenPrivileges,
TokenOwner,
TokenPrimaryGroup,
TokenDefaultDacl,
TokenSource,
TokenType,
TokenImpersonationLevel,
TokenStatistics,
TokenRestrictedSids,
TokenSessionId,
TokenGroupsAndPrivileges,
TokenSessionReference,
TokenSandBoxInert,
TokenAuditPolicy,
TokenOrigin
}
static void Main()
{
IntPtr hwnd = GetForegroundWindow();
int pid;
GetWindowThreadProcessId(hwnd, out pid);
IntPtr processHandle = OpenProcess(ProcessAccessFlags.QueryLimitedInformation, false, pid);
if (processHandle != IntPtr.Zero)
{
IntPtr tokenHandle;
if (OpenProcessToken(processHandle, 0x00000008, out tokenHandle))
{
uint returnLength;
GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenUser, IntPtr.Zero, 0, out returnLength);
IntPtr tokenInformation = Marshal.AllocHGlobal((int)returnLength);
if (GetTokenInformation(tokenHandle, TOKEN_INFORMATION_CLASS.TokenUser, tokenInformation, returnLength, out returnLength))
{
TOKEN_USER tokenUser = (TOKEN_USER)Marshal.PtrToStructure(tokenInformation, typeof(TOKEN_USER));
string userName = GetUserNameFromSid(tokenUser.User.Sid);
Console.WriteLine("Current user: " + userName);
}
Marshal.FreeHGlobal(tokenInformation);
CloseHandle(tokenHandle);
}
CloseHandle(processHandle);
}
}
static string GetUserNameFromSid(IntPtr sid)
{
int length = 0;
LookupAccountName(null, sid, null, ref length, null, ref length, null);
IntPtr namePtr = Marshal.AllocHGlobal(length);
IntPtr domainPtr = Marshal.AllocHGlobal(length);
if (LookupAccountName(null, sid, namePtr, ref length, domainPtr, ref length, null))
{
string name = Marshal.PtrToStringAnsi(namePtr);
Marshal.FreeHGlobal(namePtr);
Marshal.FreeHGlobal(domainPtr);
return name;
}
Marshal.FreeHGlobal(namePtr);
Marshal.FreeHGlobal(domainPtr);
return null;
}
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LookupAccountName(string lpSystemName, IntPtr Sid, StringBuilder lpName, ref int cchName, StringBuilder lpReferenceDomainName, ref int cchReferenceDomainName, out int peUse);
[DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr OpenProcess(ProcessAccessFlags processAccess, bool bInheritHandle, int processId);
[Flags]
public enum ProcessAccessFlags : uint
{
QueryLimitedInformation = 0x00001000
}
}
WTSQuerySessionInformation
函数来获取会话信息。通过上述方法和代码示例,可以在以管理员身份运行的应用程序中获取当前登录的非管理员用户名。
领取专属 10元无门槛券
手把手带您无忧上云