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:
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);
}
_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;
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;
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?
User contributions licensed under CC BY-SA 3.0