Visual Basic: Using reflection to check COM objects

Source: Internet
Author: User
Tags reference reflection wrappers

This article supporting source code

Content

Type libraries and runtime callable wrappers

When a type is missing an RCW

Using ITypeInfo

Find the Type reference

Get members

Primitive types and Synthetic types

COM representation of values

Dump Properties of COM objects

Using IDispatch.Invoke

Discuss

A lot of people feel frustrated when they try to make COM work. Of course, when you are successful, you will feel excited. When you understand how objects work, it's often a lot of trouble to check them using the Microsoft. NET Framework's reflection capabilities. In some cases,. NET reflection also works on COM objects. Look at the code below and you'll see what I mean. This code uses. NET reflection to get and display the list of members in that object

Dim b As New SpeechLib.SpVoice
Console.WriteLine("GETTYPE {0}", b.GetType())
For Each member In b.GetType().GetMembers()
  Console.WriteLine (member)
Next

and produces the following output in the console:

GETTYPE SpeechLib.SpVoiceClass
Void Speak(System.String, UInt32, UInt32 ByRef)
Void SetVoice(SpeechLib.ISpObjectToken)
Void GetVoice(SpeechLib.ISpObjectToken ByRef)
Int32 Volume
...

However, this code does not work for all COM objects. For some objects, you must use COM reflection. This column will give you an overview of why and how it is implemented.

Why do you want to use reflection on an object? I found that reflection is very useful for debugging and logging, and you can use it to write a generic "dump" routine to output all the content about an object. The code in this column is sufficient to allow you to write your own dump routines. Once written, you can even invoke it from the Immediate window while you are debugging. This is useful because the Visual Studio debugger does not always provide enough information about COM objects.

For production use, reflection is also useful if you write an application that uses plug-in components, and the user places their components in a directory or lists them in the registry, and your application must examine those components and find the classes and methods they expose. For example, Visual Studio uses reflection to populate IntelliSense in this way.

Type libraries and runtime callable wrappers

Let's build a project for illustration purposes. First, you create the project and add a COM reference through the Project > AddReference command. In this column, I'll use the Microsoft Speech Object Library speechlib. Figure 1 shows the related entities and files that need to be checked when you run the reflected code you saw earlier.

Fig. 1 Reflection on Speechlib

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.