COM传递函数指针(委托)作为ActiveX对象的方法参数(在C#中实现) [英] COM pass function pointer (delegate) as a method parameter to ActiveX object (implemented in C#)

查看:578
本文介绍了COM传递函数指针(委托)作为ActiveX对象的方法参数(在C#中实现)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用一个浏览器插件使用ActiveX / COM,我试图传递一个Javascript 函数到方法调用,使其可以用作回调。我已经能够获得分配给ActiveX对象上的属性的函数,但由于该方法是异步的,所以对该方法的所有调用必须共享相同的回调。



我看到现有的SO问题,但我不确定这是否是完全相同的问题,因为我不直接处理函数指针。



现在 / p>

  var obj = new MyComObject(); 
obj.Callback = function(id){Console.log(id); }
obj.DoMethodCallAsync(someId);
obj.DoMethodCallAsync(someOtherId); //使用相同的回调。

所需 API示例:

  var obj = new MyComObject(); 
obj.DoMethodCallAsync(someId,function(id){Console.log(The first ID:+ id);}
obj.DoMethodCallAsync(someOtherId,function .log(The second ID:+ id);}


解决方案

假设ActiveX是用C ++编写的,一个JavaScript函数对象将作为 IDispatch 接口指针传递给一个ActiveX方法。要回调这个JavaScript函数,需要保持这种干扰并调用 IDispatch :: Invoke(DISPID_VALUE,...)当所有参数传递到 Invoke 将被传递给JavaScript函数。 / p>

EDITED :如果控件位于C#中,JavaScript函数将作为对象传递到C#方法,可以这样调用:

  callback.GetType()。InvokeMember = 0],
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null,callback,new object [] {});

AFAIR,成员名称也可以为空白:

  callback.GetType()。InvokeMember(String.Empty,
BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null,callback,new object [] {});


I'm working on a browser plugin using ActiveX/COM and I'm trying to pass a Javascript function to a method call so that it can be used as a callback. I've been able to get the function assigned to a property on the ActiveX object, but since the method is async all calls to the method must share the same callback.

I've seen this existing SO question but I'm not sure if this is the exact same problem since I'm not dealing directly with function pointers.

Example of what we have now:

var obj = new MyComObject();
obj.Callback = function(id) { Console.log(id); }
obj.DoMethodCallAsync("someId");
obj.DoMethodCallAsync("someOtherId");  // Uses the same callback.

Example of desired API:

var obj = new MyComObject();
obj.DoMethodCallAsync("someId", function(id) { Console.log("The first ID: " + id); }
obj.DoMethodCallAsync("someOtherId", function(id) { Console.log("The second ID: " + id); }

解决方案

Assuming the ActiveX is coded in C++, a JavaScript function object would be passed to an ActiveX method as IDispatch interface pointer. To call back this JavaScript function, you'd need to hold on to that interference and call IDispatch::Invoke(DISPID_VALUE, ...) when the job is done. All params passed to Invoke will be passed to the JavaScript function.

EDITED: If the control is in C#, a JavaScript function would be passed as an object to a C# method. It can be called back this way:

callback.GetType().InvokeMember("[DispID=0]",
  BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
  null, callback, new object[] { });

AFAIR, the member name can also be blank:

callback.GetType().InvokeMember(String.Empty,
  BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
  null, callback, new object[] { });

这篇关于COM传递函数指针(委托)作为ActiveX对象的方法参数(在C#中实现)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆