How to implement IDispatch manually

M

Marvin Landman

Hi,

I have tried to declare IDispatch using its GUID and implement it, but
the runtime either seems to provide its own IDispatch implementation or
none at all depending on ClassInterface attribute of the class.

Is using my own IDispatch implementation possible?

Thanks.

Regards,
Marvin
 
G

Gregory A. Beamer

I have tried to declare IDispatch using its GUID and implement it, but
the runtime either seems to provide its own IDispatch implementation
or none at all depending on ClassInterface attribute of the class.

Is using my own IDispatch implementation possible?

Are you talking .NET or COM development?

With COM, the interfaces are written for you. You can circumvent this in
C++ by completely writing the COM interface (ie, not using the
templates), but I am not sure it can be done in any other language. If
you are C++, get into IDL, as you will have to define the interface.
This is not the right group for this type of question.

In .NET, I am not sure you can overwrite the COM callable wrapper
without editing the compiled wrapper, which I am not sure is possible.

When I say possible and impossible, I am not talking absolutes, as very
little is completely impossible in programming. But the number of hoops
necessary make any solution based on this type of code (other than
perhaps the C++ route) an exercise in futility and a maintenance
nightmare.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 
M

Marvin Landman

Gregory said:
Are you talking .NET or COM development?

I am working in .NET and would like to have a custom IDispatch
implementation instead of the one that .NET provides.

I just want to do a more intelligent dispatch using reflection so the
code has to be managed.
With COM, the interfaces are written for you. You can circumvent this in
C++ by completely writing the COM interface (ie, not using the
templates), but I am not sure it can be done in any other language. If
you are C++, get into IDL, as you will have to define the interface.
This is not the right group for this type of question.

In .NET, I am not sure you can overwrite the COM callable wrapper
without editing the compiled wrapper, which I am not sure is possible.

If I really have to use a wrapper I believe that aggregation is a better
approach than creating a custom CCW.
 
J

Jie Wang [MSFT]

Hi Marvin,

What you wanted won't work because IDispatch is treated specially, it can't
be implemented in managed code like what you did (and many others tried the
same thing before).

There is another option though. You can implement the
System.Reflection.IReflect interface on your object to be working with COM.

See http://msdn.microsoft.com/en-us/library/system.reflection.ireflect.aspx

And for .NET Framework 4.0 (the future), the new interface
ICustomQueryInterface will be helpful on this issue.

See
http://clrinterop.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=32350

Hope this helps.

Regards,
Jie Wang

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 
M

Marvin Landman

Hi,

Thank you very much for pointing out IReflect. It's perfect for me.

Regards,
Marvin
 
G

Gregory A. Beamer

I am working in .NET and would like to have a custom IDispatch
implementation instead of the one that .NET provides.

I know of no facility to do that in .NET, as the COM wrapper is a form
of marshalling from managed to unmanaged code.

You could, conceivably, make a custom COM "wrapper" in C++ and implement
a custom IDispatch. Realize, however, that you end up flipping back and
forth from COM to .NET a few times to accomplish exposing your .NET
assembly, with its custom IDispatch, for COM clients (assume that is the
goal, will have to reread the thread to be sure).

You have hit a fringe case here that affects too few to be high on the
radar (I have never personally customized a built in COM interface even
when I was writing COM components, for example).

If it were me, I would consider taking a step back and seeing if the
architectural direction is correct for this piece.

Peace and Grace,

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

Twitter: @gbworld
Blog: http://gregorybeamer.spaces.live.com

*******************************************
| Think outside the box! |
*******************************************
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top