By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.
Hmmm... The process of casting the interface is equivalent to doing a QueryInterface for the particular interface on your COM server object. You'll get an exception if the interface is not present on the object. Before you can attempt to access the object, you need to create a wrapper for the object and cause the compiler to include the wrapper in your build. If you are using Visual Studio, this is handled transparently for you by adding a reference to the TLB file associated with your COM object to your project. When you do this, the tool creates the COM interop wrappers and includes the necessary compiler directives to cause the wrappers to be included in your project. You can also perform this operation "manually" by running the tlbimp.exe (Type Library Import) application on your COM dll. Doing this will create a COM interop dll for you. You include this in your project by specifying the dll with the /R option to the compiler. Either way, you build wrappers for the COM object that get included in your code. Now, in your code you can instantiate the COM server and cast (QueryInterface) for whatever interface you choose. The invalid cast exception is the mechanism to indicate that the requested interface is not available on the object. This is perfectly normal unless you expect that the interface is actually supported by the object (as I am assuming you are).
The only advice I can give here is to make sure that you are pointing at the right type library. If you think you are, then either manually generate the interop assembly with tlbimp or find the generated assembly and run ildasm (Intermediate Language Disassembler) on the object. Ildasm will give you a dump of the intermediate language code. You don't have to be a guru (unlike looking at x86 assembly) to understand IL. Running ildasm will show you the interfaces that are implemented in the interop object. Inspect the information provided by ildasm (it's a visual explorer type of tool, if you haven't seen it...) and determine whether your interface is there or not. As always, make sure that your compile is actually pointing at the objects that you think it is.
I expect you've looked here but Microsoft has a tutorial on COM interop here.
As far as good books are concerned, I like the O'Reilly books on the subject written by Jesse Liberty. Jesse's written three books on .NET technologies including one specifically devoted to ASP.NET.
Dig Deeper on Visual Basic .NET Web services
Have a question for an expert?
Please add a title for your question
Get answers from a TechTarget expert on whatever's puzzling you.