将对象从 Javascript 传递到 C++
我找到了 将数组从 Javascript 传递到 C++ 解决方案,但我还有另一个任务:将一个对象从Javascript传递到C++(如果我将IWebBrowser2与IDispatch一起使用)
我的意思是我需要通过 window.external.method 使用 JavaScript 对象参数调用 C++ 方法
var obj = {name: "Petr", group: "Friend"};
window.external.myMethod(obj);
如何访问对象成员“名称”、“组”等?
I have found Passing an array from Javascript to C++ solution, but I have another task: Passing an object from Javascript to C++ (if I use IWebBrowser2 with IDispatch)
I mean that I need call C++ method via window.external.method with JavaScript object argument
var obj = {name: "Petr", group: "Friend"};
window.external.myMethod(obj);
How to get access to object member "name", "group", etc. ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过
IDispatch
接口及其方法GetIDsOfNames
和调用
。根据您对
myMethod
的定义,您应该接收obj
作为VARIANT
或 C++ 代码中的IDispatch *
。如果是VARIANT
,则vt
应为VT_DISPACTH
,在这种情况下,您可以安全地取消引用pdispval
。一旦获得了 IDispatch 指针,就可以使用 GetIDsOfNames 来获取
DISPID
对于您感兴趣的属性,如下所示:成功收到
DISPID
后,您必须称呼根据您是否想要获取值、设置值或调用方法,调用
有所不同。例如,要获取一个值:
请参阅
Invoke
的文档,了解有关调用它时不同排列的更多信息。You can access the object's properties via the
IDispatch
interface and its methodsGetIDsOfNames
andInvoke
.Depending on your definition of
myMethod
, you should be receivingobj
as either aVARIANT
or anIDispatch *
in your C++ code. If aVARIANT
,vt
should beVT_DISPACTH
, in which case you can safely dereferencepdispval
.Once you have an
IDispatch
pointer, you can useGetIDsOfNames
to get theDISPID
for a property that you're interested in like so:Once you have successfully received your
DISPID
, you must callInvoke
differently according to whether you would like to get a value, set a value or call a method.For instance, to get a value:
See the documentation for
Invoke
for more information about the different permutations when calling it.如果您只需要能够读取对象字段,最灵活的方法是使用 JSON。
在您的网页端使用:
var obj = {name: "Petr", group: "Friend"};
window.external.myMethod(JSON.stringify(obj));
在 C++ 端定义 myMethod() 以接受单个字符串参数。然后使用 C++ JSON 解析器 将此参数解析为可读对象。
If you just need to be able to read the object fields, the most flexible way is to use JSON.
On your web page side use:
var obj = {name: "Petr", group: "Friend"};
window.external.myMethod(JSON.stringify(obj));
On your C++ side define myMethod() to accept a single string argument. Then use a C++ JSON parser to parse this argument into a readable object.
……
……
示例
以及jave代码
...
...
and jave code sample...
...