Microsoft DirectX 8.1 (Visual Basic) |
The IUnknown interface is exposed by all COM objects. The interface is used by C++ developers to gain access to an object's functionality and to manage the object's lifetime. Microsoft® Visual Basic® developers rarely have to make use of IUnknown. However, an object's IUnknown interface is represented in Visual Basic by the IUnknown data type.
A handful of Microsoft DirectX® 8.1 Visual Basic methods have return values or parameters that are declared as Unknown. The type of these values is undefined, and will be either an IUnknown type, or another object type. DirectX 8.1 uses Unknown declarations for two cases.
Note An Unknown declaration is similar to an Any declaration. When a parameter or return value is declared as Any, you can pass or set any data type. Similarly, when a parameter or return value is declared as Unknown, you can pass or set any object type. You must be careful to use the correct object type, however, or the results will be unpredictable.
An IUnknown variable does not provide direct access to an object's methods or to the QueryInterface, Addref, and Release methods that are used in C++. When you have an object reference in an IUnknown variable, you must set that variable to an appropriately declared class variable in order to use the object's methods.
The following code fragment illustrates how to use the IUnknown data type.
'dmSeg is a DirectMusicSegment8 object 'dmPerf is a DirectMusicPerformance8 object Dim config as IUnknown Dim audioPath As DirectMusicAudioPath Set config = dmSeg.GetAudioPathConfig Set audioPath = dmPerf.CreateAudioPath(config)
The value that is returned by DirectMusicSegment8.GetAudioPathConfig is a reference to a configuration object that is not represented by a class. All you need to do with this object reference is pass it to another method, in this case, DirectMusicPerformance8.CreateAudioPath.
The following use of the IUnknown data type is incorrect.
'objDiDev is a DirectInputDevice8 object Dim unk As IUnknown Set unk = objDiDev NumItems = unk.GetDeviceData(diDeviceData,0) 'Incorrect!
The attempt to call the DirectInputDevice8.GetDeviceData method will raise a "method not found" error.