By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,368 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,368 IT Pros & Developers. It's quick & easy.

Error casting from Object* to interface

P: n/a
I'm probably missing something horribly obvious but this is the first time
I've used the managed extensions for C++ and the documentation seems to
indicate that what I am doing should work.

I am developing a shim to allow users to develop managed add-ins for an
unmanaged product. I have developed a library in MC++ with a public interface
that all add-ins must implement.

At start up I read a config file to find all the registered assemblies and
the classes that implement the interface. I then use reflection to load the
appropriate assemblies, to create instances of the registered classes and to
add them to a collection of add-ins. These classes are created via
Assembly::CreateInstance and I check the Types of the returned objects to
ensure that they do indeed implement the required interface before adding
them to my collection.

However, when the unmanaged application generates an event that an add-in
has registered for and I retrieve the appropriate object from the collection
when I attempt to cast it to an interface pointer using dynamic_cast (or
__try_cast) the cast fails.

A simplified example is as follows:

int AddinShim::Addins::Stop(int addinIndex)
IAddin* addin =
return TRUE;
catch(System::Exception* ex)
return FALSE;

The above results in addin being a null pointer (or throws an
InvalidCastException if I use __try_cast).

I have created shim apps in VB.Net and C# that follow identical steps and
the objects stored in the collection all cast as expected using DirectCast or
the C style casts of C#. Also the objects all check out in the debugger as
being of the correct type right before they fail to cast.

So, I'm missing something obvious about casts in MC++, right?

Thanks for any and all help.
Nov 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.