BCX COM Interface

BCX_DISPATCHOBJECT function

Purpose: BCX_DISPATCHOBJECT creates an instance of specified COM object based on IUnknown interface passed as a parameter. It is similar to the CREATEOBJECT function which creates an instance of COM object based on COM object name (registered progid).

It can be used to manipulate properties/methods of ActiveX objects given ActiveX object IUnknown interface.

Note well that Nothing must be called for each created COM/ActiveX object otherwise memory leaks will occur!


 Syntax:

 oBj = BCX_DISPATCHOBJECT(iunknown_ptr, release_param = FALSE)

 Parameters:

  • ComObjectName$ Specifies the name of registered COM object. 

Example:


 '************************************************************************
 ' BCX Media Player (0.3) by Kevin Diggins and Mike Henning
 ' Based on original PBWIN 7.0 code by Jose Roca
 ' Requirements: Windows Media Player, Automatic Template Libraries (ATL)
 '************************************************************************
 ' Embeds the Windows Media PLayer ACTIVEX control within a BCX Form!
 '************************************************************************
 ' Ljubisa Knezevic Added new late binding COM code that demonstrates
 ' use of BCX_DispatchObject. It allows easier manipultaion of ActiveX objects.
  
 $LIBRARY <oleaut32.lib>
 $LIBRARY <uuid.lib>
 #include <oaidl.h>
 
 GUI "BCX Media Player"
 
 CONST IDC_CTRL  = 1001
 CONST IDC_BUTT  = 1002
 
 GLOBAL hWmp  AS  HWND
 GLOBAL Form  AS  HWND
 GLOBAL Butt  AS  HWND
 GLOBAL pUnk  AS  IUnknown PTR
 'GLOBAL hr    AS  HRESULT
 GLOBAL Pth$
 GLOBAL objWmp as Object
 
 'DIM RAW mydispid = DISPID_PROPERTYPUT AS DISPID
 DIM RAW Style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN
 
 SUB FORMLOAD
   BCX_SHOW_COM_ERRORS(TRUE)
   Pth$ = SYSDIR$ & "\atl.dll" ' Make it work on \WINDOWS\ and \WINNT\ systems
   AtlAxWinInit(lib Pth$)      ' Initialize ATL lib
 
   Form = BCX_FORM   ("BCX Media Player", 0, 0, 300, 200, Style, WS_EX_CONTROLPARENT)
   hWmp = BCX_CONTROL("AtlAxWin", Form, "WMPlayer.OCX", IDC_CTRL, 0, 0, 0, 0)
   Butt = BCX_BUTTON ("Load", Form, IDC_BUTT)
   
   AtlAxGetControl (lib Pth$, hWmp, &pUnk)
   objWmp = BCX_DispatchObject(pUnk, TRUE) ' pass IUnknown interface of object, 
                                           ' TRUE - means release pUnk because 
                                           ' we are not going to use it anymore.
 
   CENTER (Form)
   SHOW   (Form)
   IF COMMAND$ > "" THEN LoadWmp (COMMAND$)
 END SUB
 
 BEGIN EVENTS
 DIM RAW rc AS RECT
 SELECT CASE CBMSG
   CASE WM_SYSCOMMAND
 '*****************************************************
 ' Capture this message and send a WM_DESTROY
 ' message or the program will remain in memory
 '*****************************************************
   IF (wParam BAND 0xFFF0) = SC_CLOSE THEN
     SendMessage (hWnd, WM_DESTROY, wParam, lParam)
     EXIT FUNCTION
   END IF
 '*****************************************************
   CASE WM_COMMAND
   SELECT CASE LOWORD(wParam)
 
     CASE IDC_BUTT
     DIM A$
     A$ = GETFILENAME$("Open Media File", "*.*", 0, Form, 0, "C:\Recordings")
 
     IF A$ > "" THEN
       LoadWmp (A$)
       A$ = ""
     END IF
 
     CASE IDOK
     IF HIWORD(wParam) = BN_CLICKED THEN
     END IF
 
     CASE IDCANCEL
     IF HIWORD(wParam) = BN_CLICKED THEN
       SendMessage (hWnd, WM_DESTROY, wParam, lParam)
       EXIT FUNCTION
     END IF
 
   END SELECT
 '*****************************************************************************************
   CASE WM_SIZE
   IF wParam <> SIZE_MINIMIZED THEN
     GetClientRect (hWnd, &rc)
     MoveWindow(GetDlgItem(hWnd,IDC_CTRL), 0, 25, rc.right-rc.left, rc.bottom-rc.top-25, TRUE)
   END IF
 '*****************************************************************************************
   CASE WM_DESTROY
   Set objWmp = Nothing
   PostQuitMessage(0)
   EXIT FUNCTION
 '*****************************************************
 END SELECT
 END EVENTS
 
 
 SUB LoadWmp (A$)
   objWmp.url = A$ 
 END SUB

For examples of the BCX_DISPATCHOBJECT function see the COM folder in the Files section of the BCX Group on Yahoo.

Related topics: Object data type definition | CreateObject List of all COM Interface Functions