JSAPI_CallbackParameters.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. #include "JSAPI_CallbackParameters.h"
  2. #include "JSAPI.h"
  3. JSAPI::CallbackParameters::CallbackParameters()
  4. {
  5. refCount = 1;
  6. }
  7. JSAPI::CallbackParameters::~CallbackParameters()
  8. {
  9. //for (size_t p=0;p!=params.size();p++)
  10. //{
  11. // ParameterList::value_type &property = params.at(p);
  12. // // some types need to be specifically destroyed or released
  13. // switch(property.second.vt)
  14. // {
  15. // case VT_DISPATCH: // add a reference if it's an IDispatch
  16. // property.second.pdispVal->Release();
  17. // break;
  18. // case VT_BSTR: // re-allocate
  19. // SysFreeString(property.second.bstrVal);
  20. // break;
  21. // }
  22. //}
  23. for (auto &param : params)
  24. {
  25. // some types need to be specifically destroyed or released
  26. switch(param.second.vt)
  27. {
  28. case VT_DISPATCH: // add a reference if it's an IDispatch
  29. param.second.pdispVal->Release();
  30. break;
  31. case VT_BSTR: // re-allocate
  32. SysFreeString(param.second.bstrVal);
  33. break;
  34. }
  35. }
  36. }
  37. HRESULT JSAPI::CallbackParameters::GetIDsOfNames(REFIID riid, OLECHAR FAR* FAR* rgszNames, unsigned int cNames, LCID lcid, DISPID FAR* rgdispid)
  38. {
  39. bool unknowns = false;
  40. for (unsigned int i = 0;i != cNames;i++)
  41. {
  42. rgdispid[i] = DISPID_UNKNOWN;
  43. const wchar_t *propertyName = rgszNames[i];
  44. bool found=false;
  45. //for (size_t p=0;p!=params.size();p++)
  46. size_t p = 0;
  47. for(auto it = params.begin(); it != params.end(); it++, p++)
  48. {
  49. //ParameterList::value_type &property = params.at(p);
  50. if (!wcscmp(it->first.c_str(), propertyName))
  51. {
  52. found=true;
  53. rgdispid[i] = (DISPID)p;
  54. break;
  55. }
  56. }
  57. if (!found)
  58. unknowns=true;
  59. }
  60. if (unknowns)
  61. return DISP_E_UNKNOWNNAME;
  62. else
  63. return S_OK;
  64. }
  65. HRESULT JSAPI::CallbackParameters::Invoke(DISPID dispid, REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS FAR *pdispparams, VARIANT FAR *pvarResult, EXCEPINFO FAR * pexecinfo, unsigned int FAR *puArgErr)
  66. {
  67. JSAPI_VERIFY_PARAMCOUNT(pdispparams, 0);
  68. size_t index = (size_t)dispid;
  69. if (index>=params.size())
  70. return DISP_E_MEMBERNOTFOUND;
  71. if (wFlags & DISPATCH_PROPERTYGET)
  72. {
  73. if (pvarResult)
  74. {
  75. //ParameterList::value_type &property = params.at(index);
  76. auto it = params.begin();
  77. while (index--)
  78. {
  79. it++;
  80. }
  81. *pvarResult = it->second;
  82. // do any type-specific allocations that are necessary
  83. switch(pvarResult->vt)
  84. {
  85. case VT_DISPATCH: // add a reference if it's an IDispatch
  86. pvarResult->pdispVal->AddRef();
  87. break;
  88. case VT_BSTR: // re-allocate
  89. pvarResult->bstrVal = SysAllocString(pvarResult->bstrVal);
  90. break;
  91. }
  92. }
  93. return S_OK;
  94. }
  95. else
  96. return DISP_E_MEMBERNOTFOUND;
  97. }
  98. HRESULT JSAPI::CallbackParameters::GetTypeInfo(unsigned int itinfo, LCID lcid, ITypeInfo FAR* FAR* pptinfo)
  99. {
  100. return E_NOTIMPL;
  101. }
  102. HRESULT JSAPI::CallbackParameters::GetTypeInfoCount(unsigned int FAR * pctinfo)
  103. {
  104. return E_NOTIMPL;
  105. }
  106. STDMETHODIMP JSAPI::CallbackParameters::QueryInterface(REFIID riid, PVOID *ppvObject)
  107. {
  108. if (!ppvObject)
  109. return E_POINTER;
  110. else if (IsEqualIID(riid, IID_IDispatch))
  111. *ppvObject = (IDispatch *)this;
  112. else if (IsEqualIID(riid, IID_IUnknown))
  113. *ppvObject = this;
  114. else if (IsEqualIID(riid, IID_IDispatchEx))
  115. *ppvObject = (IDispatchEx *)this;
  116. else
  117. {
  118. *ppvObject = NULL;
  119. return E_NOINTERFACE;
  120. }
  121. AddRef();
  122. return S_OK;
  123. }
  124. ULONG JSAPI::CallbackParameters::AddRef(void)
  125. {
  126. return InterlockedIncrement(&refCount);
  127. }
  128. ULONG JSAPI::CallbackParameters::Release(void)
  129. {
  130. LONG lRef = InterlockedDecrement(&refCount);
  131. if (lRef == 0) delete this;
  132. return lRef;
  133. }
  134. HRESULT JSAPI::CallbackParameters::GetDispID(BSTR bstrName, DWORD grfdex, DISPID *pid)
  135. {
  136. //for (size_t p=0;p!=params.size();p++)
  137. //{
  138. // ParameterList::value_type &property = params.at(p);
  139. // if (!wcscmp(property.first.c_str(), bstrName))
  140. // {
  141. // *pid= (DISPID)p;
  142. // return S_OK;
  143. // }
  144. //}
  145. size_t p = 0;
  146. for (auto it = params.begin(); it != params.end(); it++, p++)
  147. {
  148. if (!wcscmp(it->first.c_str(), bstrName))
  149. {
  150. *pid = (DISPID)p;
  151. return S_OK;
  152. }
  153. }
  154. return DISP_E_MEMBERNOTFOUND;
  155. }
  156. HRESULT JSAPI::CallbackParameters::InvokeEx(DISPID id, LCID lcid, WORD wFlags, DISPPARAMS *pdp, VARIANT *pvarRes, EXCEPINFO *pei, IServiceProvider *pspCaller)
  157. {
  158. JSAPI_VERIFY_PARAMCOUNT(pdp, 0);
  159. size_t index = (size_t)id;
  160. if (index>=params.size())
  161. return DISP_E_MEMBERNOTFOUND;
  162. if (wFlags & DISPATCH_PROPERTYGET)
  163. {
  164. if (pvarRes)
  165. {
  166. //ParameterList::value_type &property = params.at(index);
  167. auto it = params.begin();
  168. while (index--)
  169. {
  170. it++;
  171. }
  172. *pvarRes = it->second;
  173. // do any type-specific allocations that are necessary
  174. switch(pvarRes->vt)
  175. {
  176. case VT_DISPATCH: // add a reference if it's an IDispatch
  177. pvarRes->pdispVal->AddRef();
  178. break;
  179. case VT_BSTR: // re-allocate
  180. pvarRes->bstrVal = SysAllocString(pvarRes->bstrVal);
  181. break;
  182. }
  183. }
  184. return S_OK;
  185. }
  186. else
  187. return DISP_E_MEMBERNOTFOUND;
  188. }
  189. HRESULT JSAPI::CallbackParameters::DeleteMemberByName(BSTR bstrName, DWORD grfdex)
  190. {
  191. return E_NOTIMPL;
  192. }
  193. HRESULT JSAPI::CallbackParameters::DeleteMemberByDispID(DISPID id)
  194. {
  195. return E_NOTIMPL;
  196. }
  197. HRESULT JSAPI::CallbackParameters::GetMemberProperties(DISPID id, DWORD grfdexFetch, DWORD *pgrfdex)
  198. {
  199. return E_NOTIMPL;
  200. }
  201. HRESULT JSAPI::CallbackParameters::GetMemberName(DISPID id, BSTR *pbstrName)
  202. {
  203. if (id >= 0 && (size_t)id < params.size())
  204. {
  205. auto it = params.begin();
  206. while (id--)
  207. {
  208. it++;
  209. }
  210. *pbstrName = SysAllocString(it->first.c_str());
  211. return S_OK;
  212. }
  213. return E_NOTIMPL;
  214. }
  215. HRESULT JSAPI::CallbackParameters::GetNextDispID(DWORD grfdex, DISPID id, DISPID *pid)
  216. {
  217. if (grfdex == fdexEnumDefault)
  218. {
  219. if (id == DISPID_UNKNOWN)
  220. {
  221. if (params.size() == 0)
  222. return S_FALSE;
  223. else
  224. {
  225. *pid = 0;
  226. return S_OK;
  227. }
  228. }
  229. else
  230. {
  231. size_t index = id+1;
  232. if (index >= params.size())
  233. {
  234. return S_FALSE;
  235. }
  236. else
  237. {
  238. *pid = (DISPID)index;
  239. return S_OK;
  240. }
  241. }
  242. }
  243. return E_NOTIMPL;
  244. }
  245. HRESULT JSAPI::CallbackParameters::GetNameSpaceParent(IUnknown **ppunk)
  246. {
  247. return E_NOTIMPL;
  248. }
  249. void JSAPI::CallbackParameters::AddProperty(const wchar_t *name, const VARIANT &property)
  250. {
  251. params[name]=property;
  252. }
  253. void JSAPI::CallbackParameters::AddString(const wchar_t *name, const wchar_t *value)
  254. {
  255. VARIANT bstrVar;
  256. V_VT(&bstrVar) = VT_BSTR;
  257. V_BSTR(&bstrVar) = SysAllocString(value);
  258. AddProperty(name, bstrVar);
  259. }
  260. void JSAPI::CallbackParameters::AddDispatch(const wchar_t *name, IDispatch *disp)
  261. {
  262. VARIANT dispVar;
  263. V_VT(&dispVar) = VT_DISPATCH;
  264. V_DISPATCH(&dispVar) = disp;
  265. disp->AddRef();
  266. AddProperty(name, dispVar);
  267. }
  268. void JSAPI::CallbackParameters::AddLong(const wchar_t *name, LONG value)
  269. {
  270. VARIANT i4Var;
  271. V_VT(&i4Var) = VT_I4;
  272. V_I4(&i4Var) = value;
  273. AddProperty(name, i4Var);
  274. }
  275. void JSAPI::CallbackParameters::AddBoolean(const wchar_t *name, bool value)
  276. {
  277. VARIANT boolVar;
  278. V_VT(&boolVar) = VT_BOOL;
  279. V_BOOL(&boolVar) = value?VARIANT_TRUE:VARIANT_FALSE;
  280. AddProperty(name, boolVar);
  281. }
  282. size_t JSAPI::CallbackParameters::AddPropertyIndirect(const JSAPI::CallbackParameters::PropertyTemplate *entries, size_t count)
  283. {
  284. if (NULL == entries) return 0;
  285. size_t inserted = 0;
  286. VARIANT val;
  287. for (size_t i = 0; i < count; i++)
  288. {
  289. const PropertyTemplate *ppt = &entries[i];
  290. if (NULL == ppt->name)
  291. continue;
  292. switch(ppt->type)
  293. {
  294. case typeBool:
  295. V_VT(&val) = VT_BOOL;
  296. V_BOOL(&val) = (FALSE != ((BOOL)ppt->value)) ? VARIANT_TRUE : VARIANT_FALSE;
  297. break;
  298. case typeString:
  299. V_VT(&val) = VT_BSTR;
  300. V_BSTR(&val) = SysAllocString((LPCWSTR)ppt->value);
  301. break;
  302. case typeLong:
  303. V_VT(&val) = VT_I4;
  304. V_I4(&val) = (ULONG)ppt->value;
  305. break;
  306. case typeDispatch:
  307. V_VT(&val) = VT_DISPATCH;
  308. V_DISPATCH(&val) = (IDispatch*)ppt->value;
  309. if (NULL != val.pdispVal)
  310. val.pdispVal->AddRef();
  311. break;
  312. default:
  313. continue;
  314. break;
  315. }
  316. AddProperty(ppt->name, val);
  317. inserted++;
  318. }
  319. return inserted;
  320. }
  321. /* ---------------------------------------------------------------------------- */
  322. HRESULT JSAPI::InvokeEvent(JSAPI::CallbackParameters *parameters, IDispatch *invokee)
  323. {
  324. unsigned int ret;
  325. DISPPARAMS params;
  326. VARIANTARG arguments[1];
  327. VariantInit(&arguments[0]);
  328. V_VT(&arguments[0]) = VT_DISPATCH;
  329. V_DISPATCH(&arguments[0]) = parameters;
  330. parameters->AddRef();
  331. params.cArgs = 1;
  332. params.cNamedArgs = 0;
  333. params.rgdispidNamedArgs = NULL;
  334. params.rgvarg = arguments;
  335. HRESULT hr = invokee->Invoke(0, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_METHOD, &params, 0, 0, &ret);
  336. VariantClear(&arguments[0]);
  337. return hr;
  338. }