force two managed components to communicate through the com interop layer
-
Sunday, April 15, 2012 6:15 PM
hi all.
i have two .net components that need to communicate using a com interface. each component has its own declaration of that interface.
this seemed to be exactly what i needed:
http://blogs.msdn.com/b/jmstall/archive/2009/07/09/icustomqueryinterface-and-clr-v4.aspx
...but i can't get this sample to work. does anyone know how to fix this problem?
All Replies
-
Monday, April 16, 2012 9:27 AMModerator
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 PMthe problem is that the linked sample doesn't work.
-
Friday, April 27, 2012 3:31 AMModerator
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
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 AMThe exception message?
Ghost,
Call me ghost for short, Thanks
To get the better answer, it should be a better question.

