2

I am pretty new to C# and .net. and I have been tasked with creating a WPF application POC to read a QTP object repository in a .tsr file and populate a TreeView with all the objects in that repository and their child objects.

I was provided with QTP's RepositoryUtil.dll and the following sample code to adapt to C#:

    Dim i
    Dim ORFile = "D:\TAF\Size.tsr"
    Dim ORObj = CreateObject("Mercury.ObjectRepositoryUtil")

    ORObj.Load(ORFile)

    Dim ObjectCollection = ORObj.GetChildren

    For i = 0 To ObjectCollection.Count - 1

        Dim Obj = ObjectCollection.Item(i)

        Dim temp1 = "" & Obj.GetTOProperty("micclass") & "(""" & ORObj.GetLogicalName(Obj) & """)"


        Dim treeItem = New TreeViewItem

        Dim tNode = New TreeViewItem() With {.Header = temp1}

        treeview1.Items.Add(tNode)

        tNode = treeview1.Items(i)

        Add(ORObj, Obj, tNode)

    Next

Now I am not very familiar with Visual Basic however I am trying to adapt it as follows in a Console application to check whether the string "title" gets populated.

    static void Main(string[] args)
    {
        string ORFilePath = @"D:\TAF\Size.tsr";
        ObjectRepositoryUtil ORUtil = new ObjectRepositoryUtil();
        ORUtil.Load(ORFilePath);
        var ChildObjects = ORUtil.GetChildren();
        for (int i = 0; i < ChildObjects.Count(); i++ )
        {
            var ChildObject = ChildObjects.Item(i);
            string title = ChildObject.GetTOProperty("micclass") + "(\"" + ORUtil.GetLogicalName(ChildObject) + "\")";
            Console.WriteLine(title);
        }
    }

But when I run this, I get an InvalidCastException: {"Return argument has an invalid type."} on the line "var ChildObjects = ORUtil.GetChildren();"

I cannot figure out what is wrong or what type is being returned by ORUtil.GetChildren() since there is no documentation provided with the library and I can't find any online.

So, can anyone tell what exactly am I doing wrong here and what is the proper way to go about this?

Edit:

Here's the stack trace for the above exception:

System.InvalidCastException was unhandled
HResult=-2147467262
Message=Return argument has an invalid type.
Source=mscorlib
StackTrace:
   at System.Runtime.Remoting.Proxies.RealProxy.ValidateReturnArg(Object arg, Type paramType)
   at System.Runtime.Remoting.Proxies.RealProxy.PropagateOutParameters(IMessage msg, Object[] outArgs, Object returnValue)
   at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
   at REPOSITORYUTILLib.DispIObjectRepositoryUtil.GetChildren(Object Parent)
   at ORReader.Program.Main(String[] args) in c:\Users\DDDAVID.DDDAVID-IN\Documents\Visual Studio 2013\Projects\ORReader\ORReader\Program.cs:line 17
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
InnerException: 
Desmond27
  • 173
  • 1
  • 4
  • 17
  • If you tried to search for exception you probably find discussions about assembly incorrect paths or few versions loaded. [Here](http://stackoverflow.com/questions/7040355/system-invalidcastexception-when-creating-a-client-activated-object-using-an-old) for the start. – Renatas M. Apr 30 '15 at 08:41

1 Answers1

2

With some help, I've solved my own problem.

What I was trying to do was trying to add the RepositoryUtil.dll as a reference and calling its methods. What I should have been doing was to get the Program Identifier for QTP's registered ObjectRepositoryUtil using the following:

Type.GetTypeFromProgID("Mercury.ObjectRepositoryUtil");
dynamic ORUtil = Activator.CreateInstance(ORType);

I've modified the code as follows and it worked.

static void Main(string[] args)
{
    Type ORType = Type.GetTypeFromProgID("Mercury.ObjectRepositoryUtil"); 
    dynamic ORUtil = Activator.CreateInstance(ORType);

    string ORFilePath = @"D:\TAF\Size.tsr";
    //ObjectRepositoryUtil ORUtil = new ObjectRepositoryUtil();
    ORUtil.Load(ORFilePath);
    var ChildObjects = ORUtil.GetChildren();
    for (int i = 0; i < ChildObjects.Count(); i++ )
    {
        var ChildObject = ChildObjects.Item(i);
        string title = ChildObject.GetTOProperty("micclass") + "(\"" + ORUtil.GetLogicalName(ChildObject) + "\")";
        Console.WriteLine(title);
    }
}
Desmond27
  • 173
  • 1
  • 4
  • 17