Calling Delphi Code from script running in a TWebBrowser with object and function as parameters

1

Based on this article on DelphiDabbler, is it possible to include a pointer and/or function as calling parameter for callback purposes?

Assuming i have declarations as follows:

  1. Type Library (.idl file)

    interface IMyExternal: IDispatch
    {
       [id(0x000000C9)]
       HRESULT _stdcall RequestData([in] variant lparam, [in] variant wparam, [in] bstr strparam, [out, retval] long Result);
    }
    
  2. _tlb declaration

    IMyExternal = interface(IDispatch)
      ['{E297C586-25F4-450A-A0F1-78D760506202}']
      function RequestData(const userdata, callback:variant; const strparam: WideString): longint; safecall;
    end;
    
    IMyExternalDisp = dispinterface
      ['{E297C586-25F4-450A-A0F1-78D760506202}']
      function RequestData(const userdata, callback:variant; const strparam: WideString): longint; dispid 201;
    end;
    
  3. Usage

    interface
    
    type
      IMycallback = function (userdata:variant; strdata:WideString):boolean;
    
      TMyExternal = class(TAutoIntfObject, IMyExternal, IDispatch)
        function RequestData(const userdata, callback:variant; const strparam: WideString): longint; safecall;
      end;
    
    implementation
    
    function TMyExternal.RequestData(const userdata, callback:variant; const strparam: WideString): longint;
    var
      cb  : IMycallback;
      n:integer;
    begin
      cb := callback;  //<<-- Main problem
      if assigned(cb) then
      begin
        n:=1;
        while cb(userdata,  format('Test line %d',[n])) do
        inc(n)
      end;
    end;
    
  4. HTML/Javascrip :

    <html>
    
    <head>
      <script>
      function TestCallback(userdata, strdata) {
        external.RequestData(userdata, function(userdata, strdata) {
          userdata.innerHTML = userdata.innerHTML + "<br>" + strdata;
          return false;
        }, strdata);
      }
      </script>
    </head>
    
    <body>
      <a href="javascript:void(0);" onClick="TestCallback(this,'hello world')">Test Callback</a>
    </body>
    
    </html>
    

Is the problem related to assigning the callback to cb, or to using the wrong type for the input parameters?

delphi
ole
typelib
asked on Stack Overflow Sep 20, 2016 by theodorusap • edited Sep 20, 2016 by Remy Lebeau

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0