force two managed components to communicate through the com interop layer

Unanswered force two managed components to communicate through the com interop layer

All Replies

  • Monday, April 16, 2012 9:27 AM
    Moderator
     
     

    Hi Jhail,

    Welcome to the MSDN Forum.

    What problem have you encountered?

    Would you like to post your project here?

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Thursday, April 26, 2012 6:10 PM
     
     
    the problem is that the linked sample doesn't work.
  • Friday, April 27, 2012 3:31 AM
    Moderator
     
     

    Hi Jhail,

    There is some code snippet in that blog, would you like to post your project here?

    I look forward you.

    Best regards,


    Mike Feng
    MSDN Community Support | Feedback to us
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

  • Friday, April 27, 2012 5:43 AM
     
      Has Code

    The blog ends with a complete sample application.  I'll reproduce it below:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication4
    {
        [ComImport, InterfaceType(1), ComConversionLoss, Guid("FC3E287D-D659-4E1D-81D5-9D29398C7237")]
        interface IFoo1
        {
            [PreserveSig]
            int Thing(int x);
        }
    
        [ComImport, InterfaceType(1), ComConversionLoss, Guid("FC3E287D-D659-4E1D-81D5-9D29398C7237")]
        interface IFoo2
        {        
            void Thing(int x);
        }
    
    
        class C1 : IFoo1, ICustomQueryInterface
        {
    
            static readonly Guid IID_IMarshal = new Guid("00000003-0000-0000-C000-000000000046");
            static readonly Guid IID_IManagedObject = new Guid("C3FCC19E-A970-11d2-8B5A-00A0C9B7C9C4");
    
            CustomQueryInterfaceResult ICustomQueryInterface.GetInterface(ref Guid iid, out IntPtr ppv)
            {
                if (iid == IID_IMarshal ||
                    iid == IID_IManagedObject
                    )
                {
                    ppv = IntPtr.Zero;
                    return CustomQueryInterfaceResult.Failed;
                }
    
                ppv = IntPtr.Zero;
                return CustomQueryInterfaceResult.NotHandled;
            }
    
    
    
            #region IFoo1 Members
    
            public int Thing(int x)
            {
                Console.WriteLine("Inside C1={0}", x);
                return 0;
            }
    
            #endregion
        }
    
    
       
        class Program
        {
            // Convert it to a RCW
            static object GetRCW(object o)
            {
                IntPtr ip = IntPtr.Zero;
                try
                {
                    ip = Marshal.GetIUnknownForObject(o);
                    return Marshal.GetObjectForIUnknown(ip);
                }
                finally
                {
                    Marshal.Release(ip);
                }
            }
    
            static void Main(string[] args)
            {
                Console.WriteLine("Hi!");
    
                object c1 = new C1();
                object obj = GetRCW(c1);
    
                Console.WriteLine(c1.GetType().FullName); // ConsoleApplication4.C1
                Console.WriteLine(obj.GetType().FullName); // System.__ComObject
    
                IFoo1 f1 = (IFoo1)obj;
                IFoo2 f2 = (IFoo2)obj;
                f1.Thing(5);
                f2.Thing(3);
            }
        }
    }


  • Thursday, May 17, 2012 5:27 AM
     
     
    The exception message?

    Ghost,
    Call me ghost for short, Thanks
    To get the better answer, it should be a better question.