如何在 C++ 中将结构转换为 VARIANT
我想将 C++ 中的结构传递给 javascript 函数。我可以将 VARIANT 变量传递给 javascript,但我不知道如何将结构转换为 VARIANT。
例如,我可以将转换为 VARIANT 的字符串传递给 function f:
void f(VARIANT x);
f(_variant_t("hello!"));
现在我想传递这样的结构:
struct TMyStruct
{
int x;
int y;
};
------- 我的部分代码如下:
// Load Html on CHtmlView and after load is completed, get its document.
LPDISPATCH pDoc = ... // document of CHtmlView
CComQIPtr<IHTMLDocument2> m_pViewDoc;
pDoc->QueryInterface(IID_IHTMLDocument2, (void**)&m_pViewDoc);
CComPtr<IDispatch> m_pScript;
m_pViewDoc->get_Script(&m_pScript);
struct TMyStruct
{
int x;
int y;
// .... other fields...
} z;
//z = .... Initialize z.
VARIANT myVariant;
// myVariant = z ???? // How to pass z to variant.
DISPID dispid = NULL;
HRESULT hr = m_pScript->GetIDsOfNames(IID_NULL, &CComBSTR(myJavaScriptFunctionName), 1, LOCALE_SYSTEM_DEFAULT, &dispid);
ATLASSERT(SUCCEEDED(hr));
if(SUCCEEDED(hr))
{
COleDispatchDriver ocOleDispatchDriver(pScript, FALSE);
ocOleDispatchDriver.InvokeHelperV(dispid, DISPATCH_METHOD, VT_NONE, nullptr, (BYTE*)VTS_VARIANT, myVariant);
}
I want to pass a structure in C++ to a javascript function. I can pass VARIANT variable to javascript but I don't know how to convert a structure to VARIANT.
For example I can pass to function f
a string that converted to VARIANT:
void f(VARIANT x);
f(_variant_t("hello!"));
Now I want to pass a structure like this:
struct TMyStruct
{
int x;
int y;
};
------- Part of my code is below:
// Load Html on CHtmlView and after load is completed, get its document.
LPDISPATCH pDoc = ... // document of CHtmlView
CComQIPtr<IHTMLDocument2> m_pViewDoc;
pDoc->QueryInterface(IID_IHTMLDocument2, (void**)&m_pViewDoc);
CComPtr<IDispatch> m_pScript;
m_pViewDoc->get_Script(&m_pScript);
struct TMyStruct
{
int x;
int y;
// .... other fields...
} z;
//z = .... Initialize z.
VARIANT myVariant;
// myVariant = z ???? // How to pass z to variant.
DISPID dispid = NULL;
HRESULT hr = m_pScript->GetIDsOfNames(IID_NULL, &CComBSTR(myJavaScriptFunctionName), 1, LOCALE_SYSTEM_DEFAULT, &dispid);
ATLASSERT(SUCCEEDED(hr));
if(SUCCEEDED(hr))
{
COleDispatchDriver ocOleDispatchDriver(pScript, FALSE);
ocOleDispatchDriver.InvokeHelperV(dispid, DISPATCH_METHOD, VT_NONE, nullptr, (BYTE*)VTS_VARIANT, myVariant);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此页面似乎表明您无法执行您想要执行的操作。
重要提示:脚本客户端不能使用结构体!
参考: http://vcfaq.mvps.org/com/4.htm
看起来对我来说,就像您已经进入了 COM 的世界,但您仍然非常不确定它是如何工作的。从根本上说,COM 需要了解您传递的类型,因为它强制执行和管理称为 COM 单元的事物之间的转换。当跨越公寓边界时,COM 子系统将发挥一些作用,以确保遵循 COM 规则,并且不会发生任何(太)糟糕的情况。除非某处有可用的接口或类型的描述,否则它无法发挥这种魔力。因此,当将结构从一个 C++ 类传递到另一个 C++ 类时,您可以使用一些选项来促进这一点,但在尝试将数据传递到脚本客户端时,您可能没有这种级别的控制。
This page appears to indicate that you can't do what you're trying to do.
IMPORTANT: Structs cannot be used by scripting clients!
ref: http://vcfaq.mvps.org/com/4.htm
It looks to me like you've entered the world of COM and you're still pretty unsure how it all works. Fundamentally COM needs to know about the types you're passing around since it enforces and manages the transition between things called COM Apartments. When crossing apartment boundaries the COM subsystem will do some magic to ensure that the COM rules are followed and nothing (too) bad can happen. It can't do that magic unless there's a description of the interface or type available somewhere. So, when passing structs from one C++ class to another C++ class you have some options to facilitate this, but you may not have that level of control when trying to pass the data to a scripting client.