我怎样才能创建窗口站和使用C#的Windows桌面 [英] how can i creating window station and windows desktop using c#

查看:233
本文介绍了我怎样才能创建窗口站和使用C#的Windows桌面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创造新的窗口站放; Windows桌面操作系统和我的过程附加到它。我怎么能做到这一点。



我需要知道




  1. 创建窗口站和连接我的桌​​面


  2. &创建放大器;桌面有效


  3. 如何安装过程中Winlogon桌面(如果可能的话)


  4. <李之间切换>

    销毁创建的桌面并返回到Windows桌面



解决方案

由于每个会话只有一个交互式窗口站,但你可以有多个会话。



http://blogs.technet.com/markrussinovich/archive/2010/02/24/3315174.aspx



我不知道一个API的直接创建一个登录会话,但如果你用的是Windows Server版本,你可以使用远程桌面创建一个本地会话,自动运行您的程序存在,那么一旦该程序已经结束再次退出(你在远程桌面会话中运行该程序完成后可以注销)。



下面的代码将使用MSTSC ActiveX控件编程创建RDP会话。您将需要手动生成的ActiveX存根并将其添加到您的项目。




  1. 从Visual Studio命令提示符处键入以下内容:

     aximp.exe%WINDIR%\system32 \mstscax.dll 


  2. 生成的文件(MSTSCLib.dll和AxMSTSCLib.dll)复制到该项目目录。


  3. 这两个文件添加到项目引用。




 
使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data这;
使用System.Drawing中;
使用System.Linq的;
使用System.Text;使用System.Windows.Forms的
;
使用AxMSTSCLib;
使用MSTSCLib;使用System.Runtime.InteropServices
;



命名空间自动登录
{
公共部分Form1类:表格
{
私人AxMSTSCLib.AxMsRdpClient5 rdpClient;
公共Form1中()
{
的InitializeComponent();
rdpClient =新AxMSTSCLib.AxMsRdpClient5();
((ISupportInitialize接口)rdpClient).BeginInit();
rdpClient.Enabled = TRUE;
rdpClient.Location =新System.Drawing.Point(0,0);
rdpClient.Name =MsRdpClient;
rdpClient.Size = ClientSize;
rdpClient.TabIndex = 1;
rdpClient.Anchor =(AnchorStyles)
(AnchorStyles.Top | AnchorStyles.Bottom |
AnchorStyles.Left | AnchorStyles.Right);
Controls.Add被(rdpClient);
((ISupportInitialize接口)rdpClient).EndInit();
}



无效axRemoteDesktop_OnDisconnected
(对象发件人,IMsTscAxEvents_OnDisconnectedEvent E)
{
Application.Idle + = ExitTimerEvent;
}

公共无效ExitTimerEvent(对象源,EventArgs五)
{
Application.Idle - = ExitTimerEvent;
//试图关闭我们只是连接到会话(有$ //似乎没有办法得到这个session id B $ B,所以我们只是关闭所有
//断开的会话。
如果(rdpClient.Connected == 1){
rdpClient.Disconnect();
}
LogoffDisconnectedSessions();
关闭();
$} b
$ b私人定时器logoffTimer;
私人无效Form1_Load的(对象发件人,EventArgs五)
{
//关闭任何现有的已断开的会话,
的数量//可用的会话限制
LogoffDisconnectedSessions();

字符串username =用户名;
字符串密码=密码;
rdpClient.Server = localhost的;
rdpClient.UserName =用户名;
rdpClient.AdvancedSettings2.ClearTextPassword =密码;
rdpClient.Domain =;
rdpClient.FullScreen = FALSE;
rdpClient.AdvancedSettings2.RedirectDrives = FALSE;
rdpClient.AdvancedSettings2.RedirectPrinters = FALSE;
rdpClient.AdvancedSettings2.RedirectPorts = FALSE;
rdpClient.AdvancedSettings2.RedirectSmartCards = FALSE;
rdpClient.AdvancedSettings6.RedirectClipboard = FALSE;
rdpClient.AdvancedSettings6.MinutesToIdleTimeout = 1;

rdpClient.OnDisconnected + =新的
AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler
(axRemoteDesktop_OnDisconnected);

rdpClient.Connect();

logoffTimer =新的Timer();
logoffTimer.Tick + =新的EventHandler(LogoutTimerEvent);
logoffTimer.Interval = 150000;
logoffTimer.Start();
}

私人无效Form1_Close(对象发件人,FormClosedEventArgs E)
{
Application.Idle - = ExitTimerEvent;
如果(rdpClient.Connected == 1){
rdpClient.Disconnect();
}
}

公共无效LogoutTimerEvent(对象源,EventArgs五)
{
logoffTimer.Stop();
rdpClient.Disconnect();
}

枚举WTS_CONNECTSTATE_CLASS
{
WTSActive,
WTSConnected,
WTSConnectQuery,
WTSShadow,
WTSDisconnected,
WTSIdle,
WTSListen,
WTSReset,
WTSDown,
WTSInit
};

[StructLayout(LayoutKind.Sequential,字符集= CharSet.Auto)]
结构WTS_SESSION_INFO
{
公众诠释的SessionID;
公共字符串pWinStationName;
公共WTS_CONNECTSTATE_CLASS国家;
}

函数[DllImport(wtsapi32.dll)]
私人静态外部布尔WTSLogoffSession(IntPtr的hServer,诠释的SessionID,布尔bWait);
私有静态的IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero;

函数[DllImport(wtsapi32.dll,字符集= CharSet.Auto)]
私人静态外部布尔WTSEnumerateSessions(
IntPtr的hServer,
[的MarshalAs(UnmanagedType。 U4)]
int保留,
[的MarshalAs(UnmanagedType.U4)
INT版本,
文献的IntPtr ppSessionInfo,
[的MarshalAs(UnmanagedType.U4)]
REF INT pCount);

函数[DllImport(wtsapi32.dll)]
私人静态外部无效WTSFreeMemory(IntPtr的pMemory);

私人无效LogoffDisconnectedSessions()
{
IntPtr的缓冲区= IntPtr.Zero;
诠释计数= 0;
如果(WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE,0,1,
REF缓冲,引用计数)){
WTS_SESSION_INFO sessionInfo =新WTS_SESSION_INFO();

为(INT指数= 0;指数 - LT;计数;指数++){
sessionInfo =(WTS_SESSION_INFO)Marshal.PtrToStructure(
新的IntPtr(buffer.ToInt32()+
(Marshal.SizeOf(sessionInfo)*指数)),
typeof运算(WTS_SESSION_INFO));

WTS_CONNECTSTATE_CLASS状态= sessionInfo.State;
如果(状态== WTS_CONNECTSTATE_CLASS.WTSDisconnected)
{
WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE,
sessionInfo.SessionId,真正的);
}
}
}
WTSFreeMemory(缓冲);
}
}



}


i would like create new window station & windows desktop and attach my process to it. how can i do that

i need to know

  1. Creating Window station and attach my desktop

  2. Creating & switching between Desktop effectively

  3. how do i attaching process to winlogon desktop(if it is possible )

  4. Destroy created desktop and return back to windows desktop

解决方案

There is only one interactive window station per 'session' but you can have multiple sessions.

http://blogs.technet.com/markrussinovich/archive/2010/02/24/3315174.aspx

I'm not aware of an API to directly create a login session but if you are using a Windows Server version you could use Remote Desktop to create a local session, autorun your program there, then logout again once the program has ended (the program you are running in the Remote Desktop Session could logout when finished).

The code below will use the MSTSC ActiveX control to programmatically create an RDP session. You will need to manually generate the ActiveX stubs and add them to your project.

  1. From the Visual Studio Command Prompt type the following:

    aximp.exe %windir%\system32\mstscax.dll

  2. Copy the generated files (MSTSCLib.dll and AxMSTSCLib.dll) to the project directory.

  3. Add both files to the project references.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AxMSTSCLib;
using MSTSCLib;
using System.Runtime.InteropServices;

namespace AutoLogin { public partial class Form1 : Form { private AxMSTSCLib.AxMsRdpClient5 rdpClient; public Form1() { InitializeComponent(); rdpClient = new AxMSTSCLib.AxMsRdpClient5(); ((ISupportInitialize)rdpClient).BeginInit(); rdpClient.Enabled = true; rdpClient.Location = new System.Drawing.Point(0, 0); rdpClient.Name = "MsRdpClient"; rdpClient.Size = ClientSize; rdpClient.TabIndex = 1; rdpClient.Anchor = (AnchorStyles) (AnchorStyles.Top | AnchorStyles.Bottom | AnchorStyles.Left | AnchorStyles.Right); Controls.Add(rdpClient); ((ISupportInitialize)rdpClient).EndInit(); }

void axRemoteDesktop_OnDisconnected (object sender, IMsTscAxEvents_OnDisconnectedEvent e) { Application.Idle += ExitTimerEvent; } public void ExitTimerEvent(object source, EventArgs e) { Application.Idle -= ExitTimerEvent; // Attempt to close down the session we just connected to (there // appears to be no way to get the session id, so we just close all // disconnected sessions. if (rdpClient.Connected == 1) { rdpClient.Disconnect(); } LogoffDisconnectedSessions(); Close(); } private Timer logoffTimer; private void Form1_Load(object sender, EventArgs e) { // Close down any existing disconnected sessions, the number of // available sessions is limited. LogoffDisconnectedSessions(); String username = "username"; String password = "password"; rdpClient.Server = "localhost"; rdpClient.UserName = username; rdpClient.AdvancedSettings2.ClearTextPassword = password; rdpClient.Domain = ""; rdpClient.FullScreen = false; rdpClient.AdvancedSettings2.RedirectDrives = false; rdpClient.AdvancedSettings2.RedirectPrinters = false; rdpClient.AdvancedSettings2.RedirectPorts = false; rdpClient.AdvancedSettings2.RedirectSmartCards = false; rdpClient.AdvancedSettings6.RedirectClipboard = false; rdpClient.AdvancedSettings6.MinutesToIdleTimeout = 1; rdpClient.OnDisconnected += new AxMSTSCLib.IMsTscAxEvents_OnDisconnectedEventHandler (axRemoteDesktop_OnDisconnected); rdpClient.Connect(); logoffTimer = new Timer(); logoffTimer.Tick += new EventHandler(LogoutTimerEvent); logoffTimer.Interval = 150000; logoffTimer.Start(); } private void Form1_Close(object sender, FormClosedEventArgs e) { Application.Idle -= ExitTimerEvent; if (rdpClient.Connected == 1) { rdpClient.Disconnect(); } } public void LogoutTimerEvent(object source, EventArgs e) { logoffTimer.Stop(); rdpClient.Disconnect(); } enum WTS_CONNECTSTATE_CLASS { WTSActive, WTSConnected, WTSConnectQuery, WTSShadow, WTSDisconnected, WTSIdle, WTSListen, WTSReset, WTSDown, WTSInit }; [StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)] struct WTS_SESSION_INFO { public int SessionId; public string pWinStationName; public WTS_CONNECTSTATE_CLASS State; } [DllImport("wtsapi32.dll")] private static extern bool WTSLogoffSession(IntPtr hServer, int SessionId, bool bWait); private static IntPtr WTS_CURRENT_SERVER_HANDLE = IntPtr.Zero; [DllImport("wtsapi32.dll", CharSet = CharSet.Auto)] private static extern bool WTSEnumerateSessions( IntPtr hServer, [MarshalAs(UnmanagedType.U4)] int Reserved, [MarshalAs(UnmanagedType.U4)] int Version, ref IntPtr ppSessionInfo, [MarshalAs(UnmanagedType.U4)] ref int pCount); [DllImport("wtsapi32.dll")] private static extern void WTSFreeMemory(IntPtr pMemory); private void LogoffDisconnectedSessions() { IntPtr buffer = IntPtr.Zero; int count = 0; if (WTSEnumerateSessions(WTS_CURRENT_SERVER_HANDLE, 0, 1, ref buffer, ref count)) { WTS_SESSION_INFO sessionInfo = new WTS_SESSION_INFO(); for (int index = 0; index < count; index++) { sessionInfo = (WTS_SESSION_INFO)Marshal.PtrToStructure( new IntPtr(buffer.ToInt32() + (Marshal.SizeOf(sessionInfo) * index)), typeof(WTS_SESSION_INFO)); WTS_CONNECTSTATE_CLASS state = sessionInfo.State; if (state == WTS_CONNECTSTATE_CLASS.WTSDisconnected) { WTSLogoffSession(WTS_CURRENT_SERVER_HANDLE, sessionInfo.SessionId, true); } } } WTSFreeMemory(buffer); } }

}

这篇关于我怎样才能创建窗口站和使用C#的Windows桌面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆