Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7500173
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:05:32+00:00 2026-05-29T20:05:32+00:00

I want to invoke a method on a COM object that is defined in

  • 0

I want to invoke a method on a COM object that is defined in .NET from another .NET application using the DispID.

I have the following class and interface defined in a .NET project:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;

namespace TestComObject
{
   [ComVisible(true)]
   [InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
   [Guid("99476c04-574a-4271-abc8-f1d2615c3d93")]
   public interface IKnowEverything
   {
      [DispId(1030)]
      string GetTheTruth();
   }

   [ComVisible(true)] 
   [Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541")]
   [ClassInterface(ClassInterfaceType.None)]   
   public class AllKnowing : IKnowEverything
   {
      public string GetTheTruth()
      {
         return "I am King!";
      }
   }
}

I build it and it is set to register for COM interop, it is signed with a strong key.

Now I want to invoke this method using its DispID from another .NET application. I wrote the following test method:

static void Main(string[] args)
{
   Guid clsid = new Guid("9B869BAF-622A-458B-BF80-2CD6D8A3C541");
   Type type = Type.GetTypeFromCLSID(clsid, true);
   object instance = Activator.CreateInstance(type);

   string methodName = "GetTheTruth";
   string methodDispIdName = "[DispID=1030]";

   string s1 = (string)type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, instance, null);
   Console.WriteLine("Via name: {0}", s1);

   string s2 = (string)type.InvokeMember(methodDispIdName, BindingFlags.InvokeMethod, null, instance, null);
   Console.WriteLine("Via DispID: {0}", s2);
}

The first invocation is successful and the application prints “Via name: I am King!”.

But on the second invocation (using the DispID) I get an exception:

Unhandled Exception: System.MissingMethodException: Method ‘TestComObject.AllKnowing.[DispID=1030]’ not found.

The type library looks okay to me when looking at it in the OLE-COM Object Viewer:

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: TestComObject.tlb

[
  uuid(139DAA99-BF76-4057-BCC8-DCB35C153920),
  version(1.0),
  custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "TestComObject, Version=1.0.0.0, Culture=neutral, PublicKeyToken=58b6bd22fbd98427")

]
library TestComObject
{
    // TLib :     // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
    importlib("mscorlib.tlb");
    // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");

    // Forward declare all types defined in this typelib
    dispinterface IKnowEverything;

    [
      uuid(99476C04-574A-4271-ABC8-F1D2615C3D93),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.IKnowEverything")    

    ]
    dispinterface IKnowEverything {
        properties:
        methods:
            [id(0x00000406)]
            BSTR GetTheTruth();
    };

    [
      uuid(9B869BAF-622A-458B-BF80-2CD6D8A3C541),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "TestComObject.AllKnowing")
    ]
    coclass AllKnowing {
        interface _Object;
        [default] dispinterface IKnowEverything;
    };
};

What am I doing wrong here?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T20:05:34+00:00Added an answer on May 29, 2026 at 8:05 pm

    This doesn’t work because you implemented the COM server in .NET. The instance member is an actual .NET object, not an RCW. The can tell from the debugger, it is not a System.__ComObject but an object of the actual .NET type.

    So when you use InvokeMember(), you get the System.Reflection version of it, not the IDispatch version. Another way to see it is to use “getTheTruth”. Note how the first attempt fails now as well. IDispatch is case-insensitive, System.Reflection is not. The “[DispID=1030]” trick doesn’t work because System.Reflection doesn’t know anything about disp-ids.

    Feature, not a bug, it doesn’t make sense for the CLR to create the RCW when it can create the .NET object directly. Nor would that be easy to implement.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a WCF application that is using NetTcpBinding . I want to invoke
I want to invoke the main method which is static. I got the object
I want to invoke a selector of a method that has the usual NSError**
How can I invoke a method with parameters using reflection ? I want to
I have a problem that I want to append a string into StringBuffer object
I want to specifically invoke the base class method; what's the most concise way
Simple problem (I think): I want to be able to invoke a click method
I've an IBAction that calls up a panel. I want to invoke that action
I have few global methods declared in public class in my ASP.NET web application.
I have a class SomeTaskManager with an abstract method runATask . I want to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.