Programmatic RDP Login and SendMessage

3

I have the need to programmatically RDP to a virtual machine(XP SP3 / .NET3.5 / VS 2008), (credentials have been saved in .rdp file) and do UI automation testing. Because of our domain security, I need to programmatically answer 'ok' to the interactive logon. I am able to access other dialogue Windows and SendMessages to buttons, etc, after login, but I have not been able to get my SendMessage to work on this initial screen. I used spy++ to capture what actually gets sent when I press enter and I seem to be able to duplicate those messages as I view the response in the spy++ log as I run my program, but no matter what variant I use in the message nothing happens. I would like to know if it is even possible to programmatically do this or does the OS prevent this kind of automation due to security issues?

The messages I see in spy++ when I hit the enter button( on that initial screen it seems any key will do) I see:

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

When I exercise the code below and watch the messages sent to IHWindowClass(hwnd6 below) I see I generate the above messages to that window. Any help would be appreciated!

Here are the pertinent sections of the code:

'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);'
c#
sendmessage
rdp
asked on Stack Overflow Jun 22, 2010 by Carl • edited Jun 15, 2011 by Peter Olsson

2 Answers

0

You are nearly there, there are a couple of other messages that you need to send to get the RDP session ready to accept keyboard input. With Spy++ you should have seen some more messages being sent.

I was able to get the the letter 'a' entered into a notepad window (or in theory what ever the active window in the RDP session is) with the below code:

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));

Sleeps may or may not be required, but Spy++ showed the calls returning so I wait briefly before continuing. You should probably also "undo" the setfocus/setcontext operations, but for my needs this did the job fine. (Spy++ will reveal the details)

answered on Stack Overflow Jan 17, 2017 by Mike Wade
0

As @Spike says , you need to send IME_SETCONTEXT and IME_NOTIFY. I have tested the code , and it grabs the input , however , it only sends letter 'a', even if I send VK_A - VK_Z.

                SendMessageKey.SimulateKey.SendMessage((IntPtr)hwnd, SendMessageKey.MessageCode.WM_IME_SETCONTEXT, 0x00000001, 0xc000000f);
                SendMessageKey.SimulateKey.SendMessage((IntPtr)hwnd, SendMessageKey.MessageCode.WM_IME_NOTIFY, 0x00000002, 0x00000000);
                SendMessageKey.SimulateKey.SendMessage((IntPtr)hwnd, SendMessageKey.MessageCode.WM_SETFOCUS, 0x00000000, 0x00000000);

Before sending VK_A (for example) , Winspector also detects WM_KEYDOWN + WM_KEYUP with an unknown value/keycode of 255. I'm stuck here , I'm sorry. Maybe you will find what's wrong.

answered on Stack Overflow Nov 5, 2017 by Stoica Nicusor

User contributions licensed under CC BY-SA 3.0