locked
How to cast System.__COmObject to Interface/Class type RRS feed

  • Question

  • Hi to All,

    I'm working with ComAddin application with word, In word addin application I have an class as bellow

    [System.Runtime.InteropServices.

    ComVisibleAttribute(true)][System.Runtime.InteropServices.InterfaceTypeSystem.Runtime.InteropServices.ComInterfaceType.InterfaceIsIDispatch)]

     

     

    public interface IApplication{
    void showMessage();}

    [System.Runtime.InteropServices.

     

    ComVisibleAttribute(true)][System.Runtime.InteropServices.ClassInterfaceSystem.Runtime.InteropServices.ClassInterfaceType.AutoDispatch)]
    public class Application{
    public void showMessage()
    {
    MessageBox.Show("Show Messsage method is called !!!!");
    }}

    I'm returning the Object of Application class as bellow

     

    protected override object RequestComAddInAutomationService()
    {
    MessageBox.Show("RCA______AutomationService called !!!");
    Application wordApp = new Application();
    return wordApp;
    //return "aaaaabbbb" as object;
    }

     

     

    Now this object I'm in need to use it in other application(Word Application) .
    The code is as bellow
    private void button1_Click(object sender, EventArgs e)
    {
    Microsoft.Office.Interop.Word.
    Application WordApp = null;
    WordApp =
    new Microsoft.Office.Interop.Word.Application();
    Microsoft.Office.Core.
    COMAddIn DtsAddin = null;
    bool bFound = false;
    try
    {
    foreach (Microsoft.Office.Core.COMAddIn DtsAddin2 in WordApp.COMAddIns)
    {
    DtsAddin = DtsAddin2;
    if (DtsAddin.Description == "WordAddIn1")
    {
    DtsAddin.Connect =
    true;
    bFound =
    true;
    break;
    }
    }
    if (bFound == true)
    {
    // Getting Error at this point, as it was not able to convert System.__COmObject to IApplication interface type
    // or to Application class type

    IApplication Add = DtsAddin.Object as IApplication;
    Add.showMessage();
    }}
    catch (Exception e1)
    {
    string s = e1.Message;
    }
    }

    PLease help me out for casting of System.__ComObject to any interface type/ Class type.

     


    Thanking you...
    Sheshya

     

    • Moved by Harry Zhu Saturday, January 2, 2010 8:46 AM (From:Visual C# General)
    Wednesday, December 30, 2009 8:53 AM

All replies