Question

Currently occupied making a little RDP client in WPF C# for VM's hosted by Hyper-V. This client should have no management at all so solely connect to it, and be able to use the machine. So no options of stopping/starting/etc the VM.

Control using: AxMSTSCLib.AxMsRdpClient7NotSafeForScripting initialized at runtime and being placed in a WindowsFormsHost

The problems are:

1) I lied, there is one feature which should be possible: Sending Ctrl + Alt + Del. I have made a button for this on the client of course. I have tried several possibilities but I failed in each one of them, I even can't send a single 'A' to the VM.

Possibilities I tried:

a) Keyboard simulation through the SendInput native call from user32.dll

  • When i did the SendKeystroke 'control, alt and something else' and then went to the VM and for example pressed F, the control + F command was triggered. Or i could start selecting multiple files (like shift).
  • other than that, i tried setting some properties on and off on the control's advanced settings/securedsettings

So unless I am doing something wrong that the keys are not being triggered, This method fails for me


b) Using InputManager

PresentationSource source = PresentationSource.FromVisual(this);
KeyEventArgs insertStuff = new KeyEventArgs(Keyboard.PrimaryDevice, source, 0, Key.D0);
insertStuff.RoutedEvent = UIElement.KeyDownEvent;
InputManager.Current.ProcessInput(insertStuff);

this is the usercontrol where the windowsformhost is situated, I tried replacing this by this.winformshost, but no luck there either


c) tried another way but failed and forgot, wasn't very pretty anyway.


2) The second problem is focus, to be more precise: focus when the application is launched within a VM.

I'll explain in detail: At developer machine nothing is wrong. But when I launch the application from within an RDP to a Virtual machine (A) to connect to another Virtual machine (B) with my tool there is a major issue. When you click once inside the RDP control where machine B is shown for the first time, the whole application gets stuck. The only way to unstuck it, is just click on the windows start key for example on machine B then back to the client tool and everything is OK, but this is rather unacceptable.

I found out that there are focus issues with the control, but the following Failed to resolve this (created new class which has the ax control as base class):

protected override void WndProc(ref System.Windows.Forms.Message m)
{
    //Fix for the missing focus issue on the rdp client component
    if (m.Msg == 0x0021)
    {
        this.Focus();
    }
    base.WndProc(ref m);
}

If anyone would be able to help with this, I would definitely appreciate it!

Kind Regards!

Was it helpful?

Solution

 protected override void WndProc(ref Message m)
         {
             switch (m.Msg)
             {
                 case 0x021:
                     {
                         Message mm = new Message();
                         mm.Msg = 0x007;
                         base.WndProc(ref mm);
                     }
                     break;
             }
             base.WndProc(ref m);
         }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top