IUnknownUnrestricted interface

Declares the three methods of the COM base interface IUnknown as ordinary members, so that code can call QueryInterface, AddRef and Release itself.

IUnknownUnrestricted has the interface identifier 00000000-0000-0000-C000-000000000046, which is the identifier of IUnknown. Microsoft Learn describes the Windows interface: IUnknown.

The interface is declared Extends Nothing and [OleAutomation(False)]. With no base interface, its three methods are the first three entries of the method table, the slots that IUnknown occupies. The stdole.IUnknown declaration, which every other interface of the package extends, exposes none of them: twinBASIC supplies the three methods for every object and hides them. Every method is marked [PreserveSig] and returns the raw result. A class cannot implement this interface; IOleInPlaceActiveObject extends it for calling.

See IUnknown for the contract of the three methods and for how twinBASIC uses them.

Methods

QueryInterface

[PreserveSig]
Function QueryInterface(ByVal priid As LongPtr, ByRef ppvObject As LongPtr) As Long

Asks the object for another interface. priid is the address of the interface identifier requested, such as the address of an OLECLSID variable. ppvObject is passed by reference and receives the interface pointer. The result is the HRESULT: 0 (S_OK) on success, &H80004002 (E_NOINTERFACE) when the interface is not supported.

A pointer that QueryInterface returns comes with a reference, which the caller gives back with Release.

Note

When the interface is not supported, the COM contract sets ppvObject to 0. In BETA 1005, an object of a class that the project compiles — a class, a form, a control — returns E_NOINTERFACE and leaves the variable as it was. The runtime’s own objects, such as a Collection, set it to 0, as every object of VB6 does. Set the variable to 0 before the call, and test the result rather than the pointer.

Dim u As IUnknownUnrestricted = New Collection

Dim iid As OLECLSID  ' an interface identifier that the object does not have
iid.Data1 = &H12345

Dim p As LongPtr = 0
Dim hr As Long = u.QueryInterface(VarPtr(iid), p)
Debug.Print Hex(hr)  ' 80004002
Debug.Print p        ' 0

AddRef

[PreserveSig]
Function AddRef() As Long

Adds a reference to the object and returns the new reference count.

Release

[PreserveSig]
Function Release() As Long

Removes a reference from the object and returns the new reference count. An object whose count reaches 0 may free itself.

Example

This function adds a reference and removes it again, and returns the object’s reference count at that moment. The count includes the references that the procedure’s own variables hold.

Private Function ReferenceCount(ByVal Obj As Object) As Long
    Dim u As IUnknownUnrestricted = Obj
    u.AddRef
    Return u.Release()
End Function

See Also