Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 29th, 2012  |  syntax: None  |  size: 2.47 KB  |  hits: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. How to perform late-bound invocation (via DispID) of COM object defined in .NET from .NET?
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Runtime.InteropServices;
  6.  
  7. namespace TestComObject
  8. {
  9.    [ComVisible(true)]
  10.    [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
  11.    [Guid("99476c04-574a-4271-abc8-f1d2615c3d93")]
  12.    public interface IKnowEverything
  13.    {
  14.       [DispId(1030)]
  15.       string GetTheTruth();
  16.    }
  17.  
  18.    [ComVisible(true)]
  19.    [Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541")]
  20.    [ClassInterface(ClassInterfaceType.None)]  
  21.    public class AllKnowing : IKnowEverything
  22.    {
  23.       public string GetTheTruth()
  24.       {
  25.          return "I am King!";
  26.       }
  27.    }
  28. }
  29.        
  30. static void Main(string[] args)
  31. {
  32.    Guid clsid = new Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541");
  33.    Type type = Type.GetTypeFromCLSID(clsid, true);
  34.    object instance = Activator.CreateInstance(type);
  35.  
  36.    string methodName = "GetTheTruth";
  37.    string methodDispIdName = "[DispID=1030]";
  38.  
  39.    string s1 = (string)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, null);
  40.    Console.WriteLine("Via name: {0}", s1);
  41.  
  42.    string s2 = (string)type.InvokeMember(methodDispIdName, BindingFlags.InvokeMethod, null, instance, null);
  43.    Console.WriteLine("Via DispID: {0}", s2);
  44. }
  45.        
  46. // Generated .IDL file (by the OLE/COM Object Viewer)
  47. //
  48. // typelib filename: TestComObject.tlb
  49.  
  50. [
  51.   uuid(139DAA99-BF76-4057-BCC8-DCB35C153920),
  52.   version(1.0),
  53.   custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "TestComObject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58b6bd22fbd98427")
  54.  
  55. ]
  56. library TestComObject
  57. {
  58.     // TLib :     // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
  59.     importlib("mscorlib.tlb");
  60.     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
  61.     importlib("stdole2.tlb");
  62.  
  63.     // Forward declare all types defined in this typelib
  64.     dispinterface IKnowEverything;
  65.  
  66.     [
  67.       uuid(99476C04-574A-4271-ABC8-F1D2615C3D93),
  68.       version(1.0),
  69.       custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.IKnowEverything")    
  70.  
  71.     ]
  72.     dispinterface IKnowEverything {
  73.         properties:
  74.         methods:
  75.             [id(0x00000406)]
  76.             BSTR GetTheTruth();
  77.     };
  78.  
  79.     [
  80.       uuid(9B869BAF-622A-458B-BF80-2CD6D8A3C541),
  81.       version(1.0),
  82.       custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.AllKnowing")
  83.     ]
  84.     coclass AllKnowing {
  85.         interface _Object;
  86.         [default] dispinterface IKnowEverything;
  87.     };
  88. };