Reputation: 854
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);
}
Upvotes: 3
Views: 2275
Reputation: 146
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.
Upvotes: 2