0

I am working on a Outlook addin in which I want to send the data from one outlook addin project to another outlook project. But when I try call the function of another project with the object type data in the arguments it will throw "Unable to cast object of type 'System.Runtime.Remoting.Proxies.__TransparentProxy' to type at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo)" error.

Here is the code for your reference.

                 public IProfileAttribute[] profileAttributes= null;
                 Outlook.Application outlookApp = new Outlook.Application();
                 this.profileAttributes = new FilingNotifiableIpmlementation().FilingNotification(); // to fill the object 
                object destAddinName = "Tikit.CarpeDiem.AddIn.Outlook";
                Office.COMAddIn destAddIn = outlookApp.COMAddIns.Item(ref destAddinName)                                
                destAddIn.Object.FilingNotification(this.profileAttributes);

FilingNotification() is the method which we want to call of Tikit.CarpeDiem.AddIn.Outlook this project and this.profileAttributes is the object array.

The flow is going perfectly to the Outlook project if the parameter type is either string or int but it is throwing error if the parameter is object type.

Implementation of FilingNotification() method in Tikit.CarpeDiem.AddIn.Outlook project.

public void FilingNotification(IProfileAttribute[] profileAttributesList)
    {
        if (profileAttributesList != null)
        {
            var x = profileAttributesList;
        }
        else
        {
            string y = "Try again";
        }
    }

Can someone help me in this one. I am stuck in this for 2 days. It will be really helpful. Thanks in advance.

Ashwani Tandon
  • 129
  • 1
  • 5

2 Answers2

0

Instead of passing a .Net object, make it implement a COM interface and pass it as an interface:

var obj = new MyBlah();
destAddIn.Object.FilingNotification(obj as IBlah);
...
[ComVisible(true)]
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
public interface IBlah
{
    void DoBlah();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class MyBlah: StandardOleMarshalObject, IBlah
{
    public void DoBlah() 
    {
       //todo
    };
}
Dmitry Streblechenko
  • 62,942
  • 4
  • 53
  • 78
0

You may pass scalar data types following that way. You need to implement an interface known on both sides if you want to pass objects between two entities. Read more about that in the Walkthrough: Call code in a VSTO Add-in from VBA article. For example:

[ComVisible(true)]
public interface IAddInUtilities
{
    void ImportData();
}

[ComVisible(true)]
[ClassInterface(ClassInterfaceType.None)]
public class AddInUtilities : IAddInUtilities
{
    // This method tries to write a string to cell A1 in the active worksheet.
    public void ImportData()
    {
        // implementation
    }
}

Also you may consider using any standard mechanisms available for .net based applications such Remoting or WCF, see Basic WCF programming for more information.

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45