Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1454979
  • 博文数量: 596
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 173
  • 用 户 组: 普通用户
  • 注册时间: 2016-07-06 15:50
个人简介

在线笔记

文章分类

全部博文(596)

文章存档

2016年(1)

2015年(104)

2014年(228)

2013年(226)

2012年(26)

2011年(11)

分类: Windows平台

2013-12-30 17:57:39

    在windows底层的api中,提供了以下的方法来满足需求:

    WTSGetActiveConsoleSessionId: 获取当前active User的 session ID。

    CreateProcessAsUser:用当前用户创建一个新的进程。


  1. public static string GetCurrentActiveUser()
  2.         {
  3.             IntPtr hServer = IntPtr.Zero, state = IntPtr.Zero;
  4.             uint bCount = 0;

  5.             // obtain the currently active session id; every logged on user in the system has a unique session id
  6.             uint dwSessionId = WTSGetActiveConsoleSessionId();
  7.             string domain = string.Empty, userName = string.Empty;

  8.             if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.DomainName, out state, out bCount))
  9.             {
  10.                 domain = Marshal.PtrToStringAuto(state);
  11.             }

  12.             if (WTSQuerySessionInformation(hServer, (int)dwSessionId, WTSInfoClass.UserName, out state, out bCount))
  13.             {
  14.                 userName = Marshal.PtrToStringAuto(state);
  15.             }

  16.             return string.Format("{0}\\{1}", domain, userName);
  17.         }

阅读(1752) | 评论(0) | 转发(0) |
0

上一篇:穿透Session 0 隔离

下一篇:Windows 使用事件

给主人留下些什么吧!~~