473,799 Members | 3,114 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Error casting from Object* to interface

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::Creat eInstance 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::Addi ns::Stop(int addinIndex)
{
try
{
IAddin* addin =
dynamic_cast<IA ddin*>(addinLis t->get_Item(addin Index));
addin->Dispose();
return TRUE;
}
catch(System::E xception* ex)
{
Logger::LogExce ption(ex);
return FALSE;
}
};

The above results in addin being a null pointer (or throws an
InvalidCastExce ption 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
0 925

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1568
by: Andy Franks | last post by:
Hi All, This is driving me nuts, especially since I have this working for my main application with no problem. I suspect it might be due to Namespace conflicts, but I'm not positive. Any help is vastly appreciated. This is part of a routine for switching between using local and remote business logic libraries. Essentially, it's a fairly simple late-binding routine. However, When I try to cast oLocalObject to
4
1871
by: Chris | last post by:
I created a control that derives from the System.Windows.Forms.Control namespace. I then created an interface to which this new control must adhere, so that I could require future controls to provide certain functionality. In my form code, I'm not going to be sure of what types of controls the form will be using until run-time. So, I created an array of controls, all cast as Control. This all works fine, so far. Now, in the form code,...
4
4966
by: Val | last post by:
Hi I am using a DynamicProxy (class that inherits from RealProxy). I don't know the type of my Transparant Proxy at compile time. Is there a possibility to have a dynamic casting object correctParams = new object for(int i... Type t = Type.GetType(tiB.GetTypeName()) DynamicProxy proxy = new DynamicProxy(t, orderedParams, tiA) correctParams = /*TODO: CAST into type t*/ proxy.GetTransparentProxy()
9
5298
by: Tyler | last post by:
I am attempting to extend a legacy VB6 application by making it use a .NET component written in C# exposed through COM interop. Everything appeared to be going well (VB application creates the .NET component instead of the legacy VB6 component and invokes some methods successfully) until I hit a snag. For one method that is being invoked by the VB6 application, the method takes 2 parameters (string values) and a custom interface. ...
2
6380
by: cv | last post by:
Hi, I want to create an object from a class I load from an assembly. Then I want to cast that object to a class (an interface) 'known' to the program in order to pass it to other methods that work with that interface. The casting is invalid but I can't see why, as the class I load from the assembly really is of the type I cast it to. Command a = (Command) t.GetConstructor(new...
3
1916
by: thomson | last post by:
Hi all, Can any one tell me what happens when an object is casted to an interface, somthing bit deeper, the case is i do have an interface Ifunction which has got one method display(), and i have implemented the interface in one class and i have implemented the display function in the class, So when i say ((Ifunction)object).display(), it calls the function
3
1654
by: JimM | last post by:
I am trying to create a method in VS 2003 that validates an object argument is of the proper type and within a range of values. I am trying to use a Type to define the casting and object type for the validation. But I get and error of "The type or namespace name 't' could not be found (are you missing a using directive or and assembly reference?) on the "is" operator and the casting operation. An example of the method follows. The...
2
2153
by: harvie wang | last post by:
Hi, I want to implement a common Form with special interface, such as MovePoint(double,double). I create a interface first: namespace ABC.Test { public Interface IMyWindowInterface { void MovePoint(double,double); } }
3
2772
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some dead ends. I'm posting this to get some feedback on wether I'm going in the right direction, and at the same time hopefully save others from going through the process.
2
8747
by: stunt016 | last post by:
I have a program written in C# that handles communication between two pieces of software. My problem only deals with getting a text array from one program to this C# "Bridge". I can get the text array to the bridge, where it is received as a system.object. My problem is casting this system.object to a string array. The code I'm using is below. System.Array advArgs = args as System.Array; //creates array for parameters being...
0
9538
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10214
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10023
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7561
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6803
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4135
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2935
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.