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 9011479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T02:44:36+00:00 2026-06-16T02:44:36+00:00

I have a C++ COM module that accept IDispatch interfaces and on certain condition

  • 0

I have a C++ COM module that accept IDispatch interfaces and on certain condition invoke them using DISPID_VALUE. This method work very well in C++. Now I have a client in C# and I want to implement an object that implement IDispatch and have a method with DISPID = 0(DISPID_VALUE). I already tried this:

// This will generate invalid cast
[ComVisible(true)]
class Callback1
{
    [DispId(0)]
    void Execute(object arg) {...}
}

// This also generate invalid cast
[ComVisible(true)]
[Guid("163AC24E-90DB-47D4-8580-EBB21E981FBF"),
    InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
interface ICallback2
{
    [DispId(0)]
    void Execute(object arg) ;
}
[Guid("842A7754-7CE6-4991-9E12-3FAB2367591A"),
    ClassInterface(ClassInterfaceType.None),
    ComSourceInterfaces(typeof(ICallback2))]
class Callback2 : ICallback2
{
    public void Execute(object arg) {}
}

Also I don’t remember how, but I also write a code that cast successfully but call nothing at all. Now I want to know how should I write a class that implement IDispatch and on call an specific method when DISPID = 0.

Cast exception is:

System.InvalidCastException was unhandled
  Message=Specified cast is not valid.
  Source=mscorlib
  StackTrace:
   at System.StubHelpers.InterfaceMarshaler.ConvertToNative(Object objSrc, IntPtr itfMT, IntPtr classMT, Int32 flags)
   at nmclientLib.INMAsyncOperation.AddCallback(Object pCallback)
   at NMTools.RecorderRegistration.BeginConnection(OperationDoneHandler h) in D:\Programming\Version 0.9\A_Project\NMTools\RecorderRegistration.cs:line 166
   at NMTools.ConnectionManager.NoRequestRegisterConnection(RecorderRegistration r, Boolean bConnect) in D:\Programming\Version 0.9\A_Project\NMTools\ConnectionManager.cs:line 396
   at NMTools.ConnectionManager.<InitializeFromDatabase>b__0(Object s, EventArgs e) in D:\Programming\Version 0.9\A_Project\NMTools\ConnectionManager.cs:line 339
   at System.Windows.Forms.Timer.OnTick(EventArgs e)
   at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at TestApp1.Program.Main() in D:\Programming\Version 0.9\A_Project\TestApp1\Program.cs:line 18

InnerException:

  • 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-06-16T02:44:37+00:00Added an answer on June 16, 2026 at 2:44 am

    Okay, it is clear from the comments what the problem was. You must declare [ComVisible] interfaces and classes public. The CLR honors accessibility, a COM client doesn’t get to use internal types when a .NET program cannot do so either.

    A better exception message would have been nice, but that’s par for the course in COM error handling. Which doesn’t otherwise have anything similar to accessibility constraints so there isn’t a more specific error code than E_NOINTERFACE. Which gets translated to InvalidCastException.

    Notable is that this is very rare, using a [ComVisible] .NET class in a .NET application doesn’t make much sense. Just use the class directly by adding a reference to the assembly. You’ll get rid of the registration requirement, the ducky error messages and a chunk of overhead in a method call. Modulo some kind of COM layer you cannot get rid of, that happens.

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

Sidebar

Related Questions

I have a COM outproc server written in ATL that registers itself using _Module.RegisterClassObjects(CLSCTX_LOCAL_SERVER,
I have a COM inproc DLL that we are using in our product. Now
I have a COM component written in C# that makes calls to ConfigurationManager.AppSettings.Get(...). This
I have a COM Callable Wrapper that I'm using from a VB6 program, but
I have defined a GWT module that includes an external javascript file using tag.
I'm using the django_notification module. https://github.com/pinax/django-notification/blob/master/docs/usage.txt This is what I do in my code
I'm using MVC with /<module>/<controller>/<action>/ have a module at example.com/module/whatever, and I need to
I have been looking through this fantastic article: http://blogs.zynaptiq.com/bernsee/pitch-shifting-using-the-ft/ While being fantastic, it is
I am creating a module that will have its own page at mysite.com/memymodule/index/index I
I have an Zend Application that generate URL like that http://miapp.com/module/controller/action/params/values 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.