Unable to cast COM Object - Exception RRS feed

  • Question

  • Hi everyone,

    Ive been trying to develop a COM Client application, but without success. My goal is to create an application that can open reports in Cognos Impromptu. Ive got Cognos Impromptu installed on this machine and im using Visual Studio 2008 to develop the client.

    I created my project as a consoleapplication in Visual Studio. Next, I added the Impromptu Client Reference via Project > Add Reference > COM. All works fine till now, Visual Studio creates my COM Wrapper dll and adds it to the bin/debug folder.

    But when I trry to run my program, it gives me the following Exception:

    Unable to cast COM object of type 'ImpromptuClient.ImpromptuApplicationClass' to
     interface type 'ImpromptuClient.IAppAuto'. This operation failed because the Qu
    eryInterface call on the COM component for the interface with IID '{2F835754-FB4
    F-11CF-8E5F-00401C60350D}' failed due to the following error: No such interface
    supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
    Press any key to continue . . .

    The code I use is as follows:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Reflection;
    using System.Diagnostics;
    using System.Runtime.InteropServices;
    using ImpromptuClient;
    namespace TestApp2
    	class Program
    		static void Main(string[] args)
    				ImpromptuApplicationClass app = new ImpromptuApplicationClass();
                      	app.Visible(true); //<-- This causes the Exception
    			catch (Exception e)

    I can create an instance of tje ImpromptuApplicationClass. I can also call methods like GetType() (Which is ImpromptuApplicationClass). But when I  call the method Visible, the mentioned Exception is thrown.

    Another thing I tried is using the Activator.CreateInstance() function, but also without succes:

    Type oType = Type.GetTypeFromProgID("CognosImpromptu.Application");
    Object obj = Activator.CreateInstance(oType);
    ImpromptuApplication app = obj as ImpromptuApplication //app = null
    ImpromptuApplication app2 = (ImpromptuApplication)obj;//Throws the previously mentioned exception

    The last thing I tried, is to call methods via Reflection. This kinda worked, but when I had opened a catalog, I needed to open a report after that. That worked too, but the function OpenReport() returns an ImpromptuReport object. I need to call the method ExportExcel() on the report object and therefore the returnvalue of the OpenCatalog method needs to be casted to an ImpromptuReport object first. Which, as you might expect, also throws the previously mentioned exception.

    ImpromptuApplication MyApplication = new ImpromptuApplication();
    MyApplication.GetType().InvokeMember("Visible", System.Reflection.BindingFlags.InvokeMethod, null, MyApplication, new object[] { "true" });
    object[] catArgs = { @"D:\", "catalogname", "cataloguser", "username", "pwd" };
    MyApplication.GetType().InvokeMember("OpenCatalog", System.Reflection.BindingFlags.InvokeMethod, null, MyApplication, catArgs);
    object[] reportArgs = { @"D:\reportfilename.imr" };
    ImpromptuReport report = null;
    report = (ImpromptuReport)MyApplication.GetType().InvokeMember("OpenReport", BindingFlags.InvokeMethod, null, MyApplication, reportArgs);
    report.GetType().InvokeMember("CloseReport", BindingFlags.InvokeMethod, null, null, null);	

    Ive been trying to get this to work for the past couple of days, but unfortunatly without any succes. Does any of you guys have an idea what Im doing wrong?

    Thanks in advance!

    Friday, October 15, 2010 12:04 PM