C# 模拟CTRL+ALT+DELETE按键失败是怎么回事,感觉代码没问题

VagGrant 2017-09-04 11:42:46
想要客户端通过TCP发送命令通知Server执行CTRL+ALT+DELETE命令。(并不是远程桌面,客户端不会先登录)
根据TightVLC的C++代码和其他一些资料转成C#了。所有的返回都是正确的。但是就是不会切换到登录界面。
服务器是以windowservice启动的

求大神解答!!

附上代码:

private static void SimalateCtrlAltDelete()
{
//查看当前weindowstation和desttop信息是否正确,
uint ld = 0;
IntPtr pt = NativeMethods.GetProcessWindowStation();
var sda = NativeMethods.GetUserObjectInformation(pt, 2, null, 0, ref ld);
byte[] a = new byte[ld];
sda = NativeMethods.GetUserObjectInformation(pt, 2, a, ld, ref ld);
string dsada = Encoding.Unicode.GetString(a);
//输出 Service-0x0-3e7$2
System.Diagnostics.Debug.Print($"1##{dsada}\n");
IntPtr oldDesk = NativeMethods.GetThreadDesktop(NativeMethods.GetCurrentThreadId());
sda = NativeMethods.GetUserObjectInformation(oldDesk, 2, null, 0, ref ld);
a = new byte[ld];
sda = NativeMethods.GetUserObjectInformation(oldDesk, 2, a, ld, ref ld);
dsada = Encoding.Unicode.GetString(a);
//输出 Default
System.Diagnostics.Debug.Print($"2##{dsada}\n");
IntPtr window = NativeMethods.OpenWindowStation("WinSta0", false, 0x0004 | 0x0020 | 0x0008 | 0x0001 | 0x0100 | 0x0040 | 0x0002 | 0x0200 | 0x0010);
var sdad = NativeMethods.SetProcessWindowStation(window);
sda = NativeMethods.GetUserObjectInformation(window, 2, null, 0, ref ld);
a = new byte[ld];
sda = NativeMethods.GetUserObjectInformation(window, 2, a, ld, ref ld);
dsada = Encoding.Unicode.GetString(a);
System.Diagnostics.Debug.Print($"3##{dsada}\n");
//能正确返回 Winlogon桌面
IntPtr newDesk = NativeMethods.OpenDesktop("WinLogon", 0, false, 0x0004 | 0x0002 | 0x0040 | 0x0008 | 0x0020 | 0x0010 | 0x0001 | 0x0100 | 0x0080);
var aaaa = NativeMethods.SetThreadDesktop(newDesk);
sda = NativeMethods.GetUserObjectInformation(newDesk, 2, null, 0, ref ld);
a = new byte[ld];
sda = NativeMethods.GetUserObjectInformation(newDesk, 2, a, ld, ref ld);
dsada = Encoding.Unicode.GetString(a);
System.Diagnostics.Debug.Print($"4##{dsada}\n");
//var bbbb = NativeMethods.CloseDesktop(oldDesk);
//IntPtr ss = NativeMethods.FindWindow("SAS window class", "SAS window");
//IntPtr ss = NativeMethods.FindWindow(null, "SAS window");
//System.Diagnostics.Debug.Print($"error: {NativeMethods.GetLastError()}");
//执行后不能切换到 登录界面。。。。
var adddd = NativeMethods.SendMessage(new IntPtr(0xffff), WM_HOTKEY, 0, MAKELPARAM(MOD_ALT | MOD_CONTROL, VK_DELETE));
NativeMethods.SetProcessWindowStation(pt);
NativeMethods.SetThreadDesktop(oldDesk);
NativeMethods.CloseDesktop(newDesk);
NativeMethods.CloseWindowStation(window);
}

...全文
306 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
VagGrant 2017-09-08
  • 打赏
  • 举报
回复
可能是上述方法用在旧系统或者服务器上或者是客户端软件上的 总之没有找到好的解决办法 但是在win10下,直接一步就行了,调用SendSAS接口就OK了。 希望对后来者有所帮助吧

110,533

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧