3
我使用SystemEvents.SessionSwitch事件来确定是否运行我的过程用户被锁,但事件不会让你知道哪些用户得到锁定/解锁。 我怎样才能获得这些信息(由低权限的用户所拥有的过程)SystemEvents.SessionSwitch哪个用户被锁定/解锁
我使用SystemEvents.SessionSwitch事件来确定是否运行我的过程用户被锁,但事件不会让你知道哪些用户得到锁定/解锁。 我怎样才能获得这些信息(由低权限的用户所拥有的过程)SystemEvents.SessionSwitch哪个用户被锁定/解锁
我不认为你可以为部分信任的代码。如果您的应用程序或它的一部分可以被制作成一个完全信任的服务,如the answer指定一个related question可以检索的会话ID。
随后给出的会话ID,你可以找到与该会话ID的过程中获得的实际用户(从Getting Windows Process Owner Name抽象):
[DllImport ("advapi32.dll", SetLastError = true)]
static extern bool OpenProcessToken (IntPtr ProcessHandle, UInt32 DesiredAccess, out IntPtr TokenHandle);
[DllImport ("kernel32.dll", SetLastError = true)]
[return: MarshalAs (UnmanagedType.Bool)]
static extern bool CloseHandle (IntPtr hObject);
static uint TOKEN_QUERY = 0x0008;
// ...
public static WindowsIdentity GetUserFromSession(int sessionId)
{
return Process.GetProcesses()
.Where(p => p.SessionId == sessionId)
.Select(GetUserFromProcess)
.FirstOrDefault();
}
public static WindowsIdentity GetUserFromProcess(Process p)
{
IntPtr ph;
try
{
OpenProcessToken (p.Handle, TOKEN_QUERY, out ph);
return new WindowsIdentity(ph);
}
catch (Exception e)
{
return null;
}
finally
{
if (ph != IntPtr.Zero) CloseHandle(ph);
}
}