2
votes

I am trying to create some RDP client with Windows form anf AxMsTscAxNotSafeForScripting

I have the following method:

_rdpList = new List<AxMsTscAxNotSafeForScripting>();


public bool addRDP(string ip, string username, string pass)
    {
        for (int i = 0; i < number ; i++)
        {

            if (_rdpList[i].Connected.ToString() != "1")
            {
                try
                {
                    _rdpList[i].Server = ip;
                    _rdpList[i].UserName = username;



                    IMsTscNonScriptable secured = (IMsTscNonScriptable) _rdpList[i].GetOcx());
                    secured.ClearTextPassword = pass;
                    _rdpList[i].Connect();
                    _picList[int.Parse(_rdpList[i].Name)].ImageLocation = greenPath;
                    return true;

                }
                catch (Exception Ex)
                {
                    MessageBox.Show("Error Connecting", "Error connecting to remote desktop " + ip + " Error:  " + Ex.Message, MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }

I call this method from thread and when it try to do :IMsTscNonScriptable secured = (IMsTscNonScriptable) _rdpList[i].GetOcx());

It fails with the following error :

Unable to cast COM object of type 'System.__ComObject' to interface type 'MSTSCLib.IMsTscNonScriptable'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{C1E6743A-41C1-4A74-832A-0DD06C1C7A0E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).

I working on it all day long but I couldn't understand the issue?

The weird thing is if I invoke this method from windows form event ( such as button click) EVERY this work fine the only issue occur when i invoke this method from my WCF service.

Please help.

2

2 Answers

2
votes

Replace in your code:

IMsTscNonScriptable secured = (IMsTscNonScriptable) _rdpList[i].GetOcx());
secured.ClearTextPassword = pass;

With:

_rdpList[i].AdvancedSettings2.ClearTextPassword = pass;

Note: if AdvancedSettings2 is not available on object _rdpList[i], remove reference to MSTSCLib and add again.

1
votes

There are probably a bunch of solutions, but the easiest in my opinion would be to use dynamic here. As far as I understand, that is part of the reason that they created the dynamic keyword. As long as you know the interface, then you can use this without having to worry about casting. You will lose intellisense, but save yourself the hassle of pulling out your hair on COM

Some documentation on dynamic

Your code would look like this:

dynamic secured = _rdpList[i].GetOcx());