gpt4 book ai didi

c# - 程序化 RDP 登录和 SendMessage

转载 作者:行者123 更新时间:2023-11-30 18:44:15 57 4
gpt4 key购买 nike

我需要以编程方式将 RDP 连接到虚拟机 (XP SP3/.NET3.5/VS 2008),(凭据已保存在 .rdp 文件中)并进行 UI 自动化测试。由于我们的域安全性,我需要以编程方式对交互式登录回答“确定”。登录后,我可以访问其他对话窗口和 SendMessages 按钮等,但我无法让我的 SendMessage 在此初始屏幕上工作。我使用 spy++ 来捕获当我按下 enter 时实际发送的内容,当我在运行我的程序时查看 spy++ 日志中的响应时,我似乎能够复制这些消息,但无论我在消息中使用什么变体,都没有任何反应. 我想知道是否有可能以编程方式执行此操作,或者操作系统是否会因安全问题而阻止这种自动化?

当我按下回车键时,我在 spy++ 中看到的消息(在初始屏幕上似乎任何键都可以)我看到:

WM_KEYDOWN nVirtKey:00FF cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:0 Up:0
WM_KEYUP nVirtKey:00FF cRepeat:1 ScanCode:00 fExtended:0 fAltDown:0 fRepeat:1 Up:1

当我运行下面的代码并观察发送到 IHWindowClass(下面的 hwnd6)的消息时,我看到我向该窗口生成了上述消息。任何帮助将不胜感激!

以下是代码的相关部分:

'UIntPtr ip = new UIntPtr(0x0D); //ENTER
UIntPtr ip2 = new UIntPtr(0xFF); //00FF
UIntPtr kyDwnlParam = new UIntPtr(0x001);
UIntPtr kyUplParam = new UIntPtr(0xc0000001);

// used UISpy to get these class names...
string lpszParentClass = "TscShellContainerClass";
string lpszParentWindow = "test2 - test2 - Remote Desktop Connection";
string lpszClass2 = "TscShellAxHostClass";
string lpszClass3 = "ATL:2D33D580";
string lpszClass4 = "UIMainClass";
string lpszClass5 = "UIContainerClass";
string lpszClass6 = "IHWindowClass";


hWnd2 = FindWindowEx(ParenthWnd, IntPtr.Zero, lpszClass2, IntPtr.Zero);
hWnd3 = FindWindowEx(hWnd2, IntPtr.Zero, lpszClass3, IntPtr.Zero);
hWnd4 = FindWindowEx(hWnd3, IntPtr.Zero, lpszClass4, IntPtr.Zero);
hWnd5 = FindWindowEx(hWnd4, IntPtr.Zero, lpszClass5, IntPtr.Zero);
hWnd6 = FindWindowEx(hWnd5, IntPtr.Zero, lpszClass6, IntPtr.Zero);

string hexValue = hWnd6.ToString("X"); //Convert to hex to use find in spy++

SetForegroundWindow(hWnd6); // for good measure....

// tried this....

SendMessage(hWnd6, (uint)WindowsUtilities.WindowsMessages.WM_KEYDOWN, ip2, kyDwnlParam);
SendMessage(hWnd6, (uint)WindowsUtilities.WindowsMessages.WM_KEYUP, ip2, kyUplParam);

// tried this....

SendMessage(hWnd6, (uint)WindowsUtilities.WindowsMessages.WM_KEYDOWN, ip, kyDwnlParam);
SendMessage(hWnd6, (uint)WindowsUtilities.WindowsMessages.WM_KEYUP, ip, kyUplParam);

// tried this...
SendMessage(hWnd6, (uint)WindowsUtilities.WindowsMessages.WM_CHAR, ip, UIntPtr.Zero);
SendMessage(hWnd6, (uint)WindowsUtilities.WindowsMessages.WM_CHAR, ip, UIntPtr.Zero);'

最佳答案

您快完成了,您还需要发送一些其他消息以使 RDP session 准备好接受键盘输入。使用 Spy++,您应该看到发送了更多消息。

我能够使用以下代码将字母“a”输入记事本窗口(或者理论上 RDP session 中的事件窗口是什么):

SendMessage(cWind, (int)WM.IME_SETCONTEXT, new UIntPtr(0x00000001), new UIntPtr(0xC000000F));
SendMessage(cWind, (int)WM.IME_NOTIFY, new IntPtr(0x00000002), new IntPtr(0x00000000));
Thread.Sleep(1);

SendMessage(cWind, (int)WM.SETFOCUS, new UIntPtr(0x00203794), new UIntPtr(0x00000000));
Thread.Sleep(1);
//Random keypress
SendMessage(cWind, (int)WM.KEYDOWN, new UIntPtr(0x000000FF), new UIntPtr(0x00000001));
SendMessage(cWind, (int)WM.KEYUP, new UIntPtr(0x00000041), new UIntPtr(0xC0000001));
Thread.Sleep(1);
//A key presses
SendMessage(cWind, (int)WM.KEYDOWN, new IntPtr(0x00000041), new IntPtr(0x001E0001));
SendMessage(cWind, (int)WM.KEYUP, new UIntPtr(0x00000041), new UIntPtr(0xC01E0001));

可能需要也可能不需要 sleep ,但 Spy++ 显示调用返回,所以我在继续之前稍等片刻。您可能还应该“撤消”setfocus/setcontext 操作,但根据我的需要,这很好。 (Spy++ 将揭示细节)

关于c# - 程序化 RDP 登录和 SendMessage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3094789/

57 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com