Team LiB
Previous Section Next Section

QueryInterface and Casting

QueryInterface is a method that is part of the IUnknown interface. In managed code, IUnknown is hidden from you, so you cannot call QueryInterface in C#. Instead of calling QueryInterface, you should simply cast the object that you have when you created the object to the appropriate interface, as shown in the following example.

Note 

It is very important to realize that the familiar interfaces of IUnknown and IDispatch are hidden and no longer available to you to use in managed code.

Code Example: QueryInterface and Casting
Start example
using System;
using System.Runtime.InteropServices;
using SpeechLib;

namespace Client.Chapter_10___COM_and_.NET_Interoperability
{
      class QI
      {
            [STAThread]
            static void Main(string[] args)
            {
                   SpVoiceClass X = new SpVoiceClass();
                   X.Speak("Hello World", 0);
                  //Casting does the work of QueryInterface
                   ISpVoice Y = (ISpVoice)X;
            }
      }
}
End example

Team LiB
Previous Section Next Section