c++ - Can't get exact position of BSTR data in VARIANT struct -
i have strange problem reading bstr
value variant
struct.
i use following javascript code send data c++ code:
external.cppcall("zhttshow", 1, "y");
in invoke
function in c++ code, tried access data:
hresult stdmethodcalltype webbrowser::invoke(_in_ dispid dispidmember, _in_ refiid riid, _in_ lcid lcid, _in_ word wflags, _in_ dispparams *pp, _out_opt_ variant *pvarresult, _out_opt_ excepinfo *pexcepinfo, _out_opt_ uint *puargerr) { if (dispidmember == 100) { unsigned int k = pp->cargs; if (k > 0) { variant *vvariant = &pp->rgvarg[k - 1]; if ((vvariant->vt & vt_bstr) == vt_bstr && vvariant->pvarval->bstrval) { wchar_t *wide = (wchar_t*)(vvariant->pvarval->bstrval); // other code } } } }
in above code, wide
point undefined position. if pointer of vvariant->pvarval->bstrval
point position near bstr value:
variant *vvariant = &pp->rgvarg[k - 1]; if ((vvariant->vt & vt_bstr) == vt_bstr && vvariant->pvarval->bstrval) { wchar_t *wide = (wchar_t*)(&vvariant->pvarval->bstrval); // other code }
wide
point position behide bstr
data 8 bytes, instead "zhttshow" string, "show". if subtract wide
4 desired data.
bellow debug screen second c++ code:
question:
why wide
doesn't point bstr's exact position? error in code , how fix it?
a bstr
not wchar_t*
. may similar, not. , more importanlty, wchar_t*
not bstr
. bstrs have structure, header sits in front of pointed-to value, real bstr starts few bytes before address. see bstr data type:
a bstr composite data type consists of length prefix, data string, , terminator.
this why should use appropriate bstr specific apis. best, use compiler support _bstr_t
. extract bstr variant correctly use _variant_t
extractor. let compiler deal details.
Comments
Post a Comment