So why (does PB) dress it up as something it is not - either a specific (and by implication unique) object reference or a specific interface reference.
Code:
Sub Foo(ifx As I_X) Call ifx.Fx1(75) End Sub
Code:
//CAClient.cpp #include <windows.h> #include <stdio.h> const CLSID CLSID_CA={0x20000000,0x0000,0x0000,{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x4}}; const IID IID_I_X ={0x20000000,0x0000,0x0000,{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x5}}; const IID IID_I_Y ={0x20000000,0x0000,0x0000,{0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x6}}; interface I_X : IUnknown { virtual HRESULT __stdcall Fx1(int)=0; virtual HRESULT __stdcall Fx2(int)=0; }; interface I_Y : IUnknown { virtual HRESULT __stdcall Fy1(int)=0; virtual HRESULT __stdcall Fy2(int)=0; }; void Foo(IUnknown* pIUnk, int ifDesired) { if(ifDesired==1) //ifDesired = interface desired { I_X* pIX=NULL; pIUnk->QueryInterface(IID_I_X,(void**)&pIX); pIX->Fx1(1); pIX->Fx2(1); pIX->Release(); } if(ifDesired==2) //ifDesired = interface desired { I_Y* pIY=NULL; pIUnk->QueryInterface(IID_I_Y,(void**)&pIY); pIY->Fy1(2); pIY->Fy2(2); pIY->Release(); } } int main(int argc, char* argv[]) { IUnknown* pIUnknown=NULL; HRESULT hr; CoInitialize(NULL); hr=CoCreateInstance(CLSID_CA,NULL,CLSCTX_SERVER,IID_IUnknown,(void**)&pIUnknown); if(SUCCEEDED(hr)) { Foo(pIUnknown,1); Foo(pIUnknown,2); pIUnknown->Release(); } CoUninitialize(); return 0; } /* Called CA::QueryInterface() For IID_IUnknows Called CA::AddRef() Called CA::AddRef() Called CA::Release() Called CA::QueryInterface() For IID_IUnknows Called CA::AddRef() Called CA::Release() Called CA::QueryInterface() For IID_I_X Called CA::AddRef() Called Fx1() : iNum = 1 Called Fx2() : iNum = 1 Called CA::Release() Called CA::QueryInterface() For IID_I_Y Called CA::AddRef() Called Fy1() : iNum = 2 Called Fy2() : iNum = 2 Called CA::Release() Called CA::Release() */
Code:
#Compile Exe "CAClient.exe" #Dim All #Include "Win32Api.inc" $CLSID_CA =Guid$("{20000000-0000-0000-0000-000000000004}") 'Class ID of Class CA, ie., Class A $IID_IX =Guid$("{20000000-0000-0000-0000-000000000005}") 'Interface X $IID_IY =Guid$("{20000000-0000-0000-0000-000000000006}") 'Interface Y Interface I_X $IID_IX : Inherit IUnknown Method Fx1(ByVal iNum As Long) As Long Method Fx2(ByVal iNum As Long) As Long End Interface Interface I_Y $IID_IY : Inherit IUnknown Method Fy1(ByVal iNum As Long) As Long Method Fy2(ByVal iNum As Long) As Long End Interface Sub Foo(unk As IUnknown, ifDesired As Long) If ifDesired=1 Then Local ix As I_X ix=unk Call ix.Fx1(1) Call ix.Fx2(1) Set ix=Nothing End If If ifDesired=2 Then Local iy As I_Y iy=unk Call iy.Fy1(2) Call iy.Fy2(2) Set iy=Nothing End If End Sub Function PBMain() As Long Local IUnk As IUnknown IUnk=NewCom Clsid $CLSID_CA Call Foo(IUnk,1) Call Foo(IUnk,2) Set IUnk=Nothing Waitkey$ PBMain=0 End Function 'Called CA::QueryInterface() For IID_IUnknows 'Called CA::AddRef() 'Called CA::AddRef() 'Called CA::Release() 'Called CA::QueryInterface() For IID_IUnknows 'Called CA::AddRef() 'Called CA::Release() 'Called CA::QueryInterface() For IID_I_X 'Called CA::AddRef() 'Called Fx1() : iNum = 1 'Called Fx2() : iNum = 1 'Called CA::Release() 'Called CA::QueryInterface() For IID_I_Y 'Called CA::AddRef() 'Called Fy1() : iNum = 2 'Called Fy2() : iNum = 2 'Called CA::Release()
Notice in the C++ program that pIUnknown in the CreateInstance() call is an output parameter typed as a (void**)&pIUnknown. That is most definitely just a raw number with no inherent type as far as C++ is concerned, and by typeing it as such it doesn't leave much doubt as to what it is.
Comment