; _______________________________________________________________________________________
;|                                                                                       |
;| ..::FreshLib::..  Free, open source. Licensed under "BSD 2-clause" license."          |
;|_______________________________________________________________________________________|
;
;  Description: Win32 COM interface macros.
;
;  Target OS: Win32
;
;  Dependencies:
;
;  Notes:
;_________________________________________________________________________________________

macro cominvk ptr_object, method, [arg] {
common
      if ~ arg eq
reverse
        pushx arg
common
      end if

      if ~defined ptr_object#.com.object
        ERROR! `ptr_object is not a com object.
      end if

        mov     eax, [ptr_object]
        push    eax
        mov     eax,[eax]
        call    [eax+ptr_object#.#method]
}


macro comcall handle, interface, proc,[arg] {
common
      if ~ arg eq
reverse
        pushx arg
common
      end if

      if ~defined interface#.com.interface
        ERROR! `interface is not a com interface.
      end if

      if handle eqtype eax | handle eqtype 0    ; ???
        local ..handle
        label ..handle at handle
        push    handle
        mov     eax, [..handle]
      else
        mov     eax, handle
        push    eax
        mov     eax, [eax]
      end if

      call [eax+interface#.#proc]
}


macro com_interface name, [proc] {
common
    struc name \{
      match , @struct \\{ define field@struct .,name, \\}
      match no, @struct \\{
        . dd ?
        virtual at 0
forward
          .#proc dd ?
common
          .\\#\\.com.object = name#.com.interface
        end virtual
      \\}
    \}

   virtual at 0
forward
     name#.#proc dd ?
common
     name#.com.interface = $ shr 2
   end virtual
}


struc GUID def {
  if used .
  label . dword
  match d1-d2-d3-d4-d5, def \{
    .Data1 dd $\#d1
    .Data2 dw $\#d2
    .Data3 dw $\#d3
    .Data4 db $\#d4 shr 8, $\#d4 and 0FFh
    .Data5 db $\#d5 shr 40, $\#d5 shr 32 and 0FFh, $\#d5 shr 24 and 0FFh
           db $\#d5 shr 16 and 0FFh, $\#d5 shr 8 and 0FFh, $\#d5 and 0FFh
  \}
  end if
}


; ends with two word zero terminators.

struc BSTR [txt] {
common
  local ..len
  .length dd ..len
  . du txt
  ..len = $ - .
    dw 0, 0
}
