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.

👉 The NOTHING keyword must be called for each created COM/ActiveX object otherwise memory leaks will occur!

Syntax:

COMObject = BCX_DISPATCHOBJECT(iunknown_ptr [,release_param])

Return Value:

Parameters:

  • Data type: IUnknown PTR
    iunknown_ptr is a pointer to the IUnknown interface.
  • Data type: BOOL
    release_param [OPTIONAL] specifies whether the IUnknown object is to be released.

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 manipulation of ActiveX objects. 
'*********************************************************************** 
'Robert Wishlaw revised the atl.dll access to accomodate 64 bit compiling. 
    
#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 Pth$
GLOBAL objWmp AS OBJECT
   
DIM RAW Style = WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN
   
SUB FORMLOAD

  BCX_SHOW_COM_ERRORS(TRUE)

  DECLARE FUNCTION AtlAxWinInit LIB "atl.dll" ALIAS "AtlAxWinInit" () AS LONG
  AtlAxWinInit()     '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)

  DECLARE FUNCTION AtlAxGetControl LIB "atl.dll" ALIAS "AtlAxGetControl" (BYVAL hWmp As HWND, BYVAL pUnk As IUnknown PTR PTR) AS ULONG
  AtlAxGetControl(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 more examples of the BCX COM functions see the COM directory at the https://bcxbasiccoders.com/archives/YahooGroups/Com/ website.

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