4

I have a COM server App that and need to link callbacks to specific events which are late bound.

My test VB script is as follows

Sub Main
  dim Frm
  Set Frm=NewForm("Form1")
  Frm.OnActivate = getRef("Frm_OnActivate")
  a= Frm.Showmodal
end Sub

sub Frm_OnActivate
  MsgBox("Activate")
end Sub

My com server has the onActivate property which is of type OleVariant.

function TALform.Get_OnActivate: OleVariant;
begin
  result:=FonActivate;
end;

procedure TALform.Set_OnActivate(Value: OleVariant);
begin
  FonActivate:=Value;
  Fform.OnActivate:=OnactivateEx
end;

My question is, having got that value, how do I call the VBscript function from the value stored in the Olevariant (which the debugger shows to be of type VarDispatch) ?

Andy k
  • 1,012
  • 1
  • 10
  • 19

1 Answers1

3

Try something like this:

var
    Param: TDispParams;
    MethodResult: OleVariant;
    Result: HRESULT;
begin
    Param.rgvarg := nil;
    Param.rgdispidNamedArgs := nil;
    Param.cArgs := 0;
    Param.cNamedArgs := 0;
    Result := IDispatch(FonActivate).Invoke(0, GUID_NULL, SysLocale.DefaultLCID, DISPATCH_METHOD, Param, @MethodResult, nil, nil);
end;
Olivier
  • 3,368
  • 1
  • 2
  • 13
  • Thanks for that but returns DISP_E_BADVARTYPE, I think it may be just a straight function address rather than an actual Idispatch because if I use Idispatch(Fonactivate).GetTypeInfoCount(i) it returns 0. – Andy k May 07 '20 at 13:33
  • @Andyk I changed the type of `Param`. Can you try again? – Olivier May 07 '20 at 13:48
  • still has the DISP_E_BADVARTYPE result. – Andy k May 07 '20 at 14:18
  • @Andyk I added the initialization of `Param` that was missing. Hopefully it will work this time. – Olivier May 07 '20 at 14:22
  • That did the trick, thank you so much. Been searching all around the web on how to do this without any luck. Def not obvious. – Andy k May 07 '20 at 14:37