Q

How can I resolve problems accessing COM components while migrating to ASP.NET?

I am currently migrating our ASP applicatuions to ASP.NET, but we still want to use our existing COM components. I am currently having problems with accessing the functions in the COM component. It is giving an invalid cast exception: invalid queryinterface. How would I resolve this? Also, could you also give me advice on which book would be a good source of info regarding the use of ASP.NET and COMcomponents?
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.
This was first published in January 2003

Dig deeper on Visual Basic .NET Web services

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchSoftwareQuality

SearchCloudApplications

SearchAWS

TheServerSide

SearchWinDevelopment

Close