host.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  1. /*
  2. * Copyright 2010 Jacek Caban for CodeWeavers
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  17. */
  18. #include <stdarg.h>
  19. #define COBJMACROS
  20. #define CONST_VTABLE
  21. #include <windef.h>
  22. #include <winbase.h>
  23. #include <ole2.h>
  24. #include "wscript.h"
  25. #include <wine/debug.h>
  26. #include <wine/heap.h>
  27. WINE_DEFAULT_DEBUG_CHANNEL(wscript);
  28. #define BUILDVERSION 16535
  29. static const WCHAR wshVersionW[] = L"5.8";
  30. VARIANT_BOOL wshInteractive =
  31. #ifndef CSCRIPT_BUILD
  32. VARIANT_TRUE;
  33. #else
  34. VARIANT_FALSE;
  35. #endif
  36. static HRESULT to_string(VARIANT *src, BSTR *dst)
  37. {
  38. VARIANT v;
  39. HRESULT hres;
  40. if(V_VT(src) == VT_NULL) {
  41. *dst = SysAllocString(L"null");
  42. return *dst ? S_OK : E_OUTOFMEMORY;
  43. }
  44. V_VT(&v) = VT_EMPTY;
  45. hres = VariantChangeType(&v, src, 0, VT_BSTR);
  46. if(FAILED(hres)) {
  47. WARN("Could not convert argument %s to string\n", debugstr_variant(src));
  48. return hres;
  49. }
  50. *dst = V_BSTR(&v);
  51. return S_OK;
  52. }
  53. static void print_string(const WCHAR *string)
  54. {
  55. DWORD count, ret, len, lena;
  56. char *buf;
  57. if(wshInteractive) {
  58. MessageBoxW(NULL, string, L"Windows Script Host", MB_OK);
  59. return;
  60. }
  61. len = lstrlenW(string);
  62. ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string, len, &count, NULL);
  63. if(ret) {
  64. WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"\r\n", lstrlenW(L"\r\n"), &count, NULL);
  65. return;
  66. }
  67. lena = WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, NULL, 0, NULL, NULL);
  68. buf = heap_alloc(len);
  69. if(!buf)
  70. return;
  71. WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, buf, lena, NULL, NULL);
  72. WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE);
  73. heap_free(buf);
  74. WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE);
  75. }
  76. static HRESULT WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
  77. {
  78. WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
  79. if(IsEqualGUID(&IID_IUnknown, riid)
  80. || IsEqualGUID(&IID_IDispatch, riid)
  81. || IsEqualGUID(&IID_IHost, riid)) {
  82. *ppv = iface;
  83. return S_OK;
  84. }
  85. *ppv = NULL;
  86. return E_NOINTERFACE;
  87. }
  88. static ULONG WINAPI Host_AddRef(IHost *iface)
  89. {
  90. return 2;
  91. }
  92. static ULONG WINAPI Host_Release(IHost *iface)
  93. {
  94. return 1;
  95. }
  96. static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
  97. {
  98. WINE_TRACE("(%p)\n", pctinfo);
  99. *pctinfo = 1;
  100. return S_OK;
  101. }
  102. static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
  103. ITypeInfo **ppTInfo)
  104. {
  105. WINE_TRACE("(%x %x %p\n", iTInfo, lcid, ppTInfo);
  106. ITypeInfo_AddRef(host_ti);
  107. *ppTInfo = host_ti;
  108. return S_OK;
  109. }
  110. static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
  111. UINT cNames, LCID lcid, DISPID *rgDispId)
  112. {
  113. WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid), rgszNames,
  114. cNames, lcid, rgDispId);
  115. return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
  116. }
  117. static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
  118. LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
  119. EXCEPINFO *pExcepInfo, UINT *puArgErr)
  120. {
  121. WINE_TRACE("(%d %p %p)\n", dispIdMember, pDispParams, pVarResult);
  122. return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
  123. pVarResult, pExcepInfo, puArgErr);
  124. }
  125. static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
  126. {
  127. WINE_TRACE("(%p)\n", out_Name);
  128. if(!(*out_Name = SysAllocString(L"Windows Script Host")))
  129. return E_OUTOFMEMORY;
  130. return S_OK;
  131. }
  132. static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
  133. {
  134. WINE_TRACE("(%p)\n", out_Dispatch);
  135. *out_Dispatch = (IDispatch*)&host_obj;
  136. return S_OK;
  137. }
  138. static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
  139. {
  140. WCHAR fullPath[MAX_PATH];
  141. WINE_TRACE("(%p)\n", out_Path);
  142. if(GetModuleFileNameW(NULL, fullPath, ARRAY_SIZE(fullPath)) == 0)
  143. return E_FAIL;
  144. if(!(*out_Path = SysAllocString(fullPath)))
  145. return E_OUTOFMEMORY;
  146. return S_OK;
  147. }
  148. static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
  149. {
  150. WCHAR path[MAX_PATH];
  151. int howMany;
  152. WCHAR *pos;
  153. WINE_TRACE("(%p)\n", out_Path);
  154. if(GetModuleFileNameW(NULL, path, ARRAY_SIZE(path)) == 0)
  155. return E_FAIL;
  156. pos = wcsrchr(path, '\\');
  157. howMany = pos - path;
  158. if(!(*out_Path = SysAllocStringLen(path, howMany)))
  159. return E_OUTOFMEMORY;
  160. return S_OK;
  161. }
  162. static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
  163. {
  164. WINE_TRACE("(%p)\n", out_Interactive);
  165. *out_Interactive = wshInteractive;
  166. return S_OK;
  167. }
  168. static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
  169. {
  170. WINE_TRACE("(%x)\n", v);
  171. wshInteractive = v;
  172. return S_OK;
  173. }
  174. static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
  175. {
  176. FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode);
  177. ExitProcess(ExitCode);
  178. return S_OK;
  179. }
  180. static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
  181. {
  182. WCHAR *scriptName;
  183. WINE_TRACE("(%p)\n", out_ScriptName);
  184. scriptName = wcsrchr(scriptFullName, '\\');
  185. ++scriptName;
  186. if(!(*out_ScriptName = SysAllocString(scriptName)))
  187. return E_OUTOFMEMORY;
  188. return S_OK;
  189. }
  190. static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
  191. {
  192. WINE_TRACE("(%p)\n", out_ScriptFullName);
  193. if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
  194. return E_OUTOFMEMORY;
  195. return S_OK;
  196. }
  197. static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
  198. {
  199. WINE_TRACE("(%p)\n", out_Arguments);
  200. *out_Arguments = &arguments_obj;
  201. return S_OK;
  202. }
  203. static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
  204. {
  205. WINE_TRACE("(%p)\n", out_Version);
  206. if(!(*out_Version = SysAllocString(wshVersionW)))
  207. return E_OUTOFMEMORY;
  208. return S_OK;
  209. }
  210. static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
  211. {
  212. WINE_TRACE("(%p)\n", out_Build);
  213. *out_Build = BUILDVERSION;
  214. return S_OK;
  215. }
  216. static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
  217. {
  218. WINE_FIXME("(%p)\n", out_Timeout);
  219. return E_NOTIMPL;
  220. }
  221. static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
  222. {
  223. WINE_FIXME("(%d)\n", v);
  224. return E_NOTIMPL;
  225. }
  226. static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
  227. IDispatch **out_Dispatch)
  228. {
  229. IUnknown *unk;
  230. GUID guid;
  231. HRESULT hres;
  232. TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
  233. if(Prefix && *Prefix) {
  234. FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
  235. return E_NOTIMPL;
  236. }
  237. hres = CLSIDFromProgID(ProgID, &guid);
  238. if(FAILED(hres))
  239. return hres;
  240. hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
  241. &IID_IUnknown, (void**)&unk);
  242. if(FAILED(hres))
  243. return hres;
  244. hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
  245. IUnknown_Release(unk);
  246. return hres;
  247. }
  248. static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
  249. {
  250. WCHAR *output = NULL, *ptr;
  251. unsigned argc, i, len;
  252. int ubound, lbound;
  253. VARIANT *argv;
  254. BSTR *strs;
  255. HRESULT hres;
  256. TRACE("(%p)\n", args);
  257. if(SafeArrayGetDim(args) != 1) {
  258. FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args));
  259. return E_NOTIMPL;
  260. }
  261. SafeArrayGetLBound(args, 1, &lbound);
  262. SafeArrayGetUBound(args, 1, &ubound);
  263. hres = SafeArrayAccessData(args, (void**)&argv);
  264. if(FAILED(hres))
  265. return hres;
  266. argc = ubound-lbound+1;
  267. strs = heap_alloc_zero(argc*sizeof(*strs));
  268. if(!strs) {
  269. SafeArrayUnaccessData(args);
  270. return E_OUTOFMEMORY;
  271. }
  272. /* Len of spaces between arguments. */
  273. len = argc-1;
  274. for(i=0; i < argc; i++) {
  275. hres = to_string(argv+i, strs+i);
  276. if(FAILED(hres))
  277. break;
  278. len += SysStringLen(strs[i]);
  279. }
  280. SafeArrayUnaccessData(args);
  281. if(SUCCEEDED(hres)) {
  282. ptr = output = heap_alloc((len+1)*sizeof(WCHAR));
  283. if(output) {
  284. for(i=0; i < argc; i++) {
  285. if(i)
  286. *ptr++ = ' ';
  287. len = SysStringLen(strs[i]);
  288. memcpy(ptr, strs[i], len*sizeof(WCHAR));
  289. ptr += len;
  290. }
  291. *ptr = 0;
  292. }else {
  293. hres = E_OUTOFMEMORY;
  294. }
  295. }
  296. for(i=0; i < argc; i++)
  297. SysFreeString(strs[i]);
  298. heap_free(strs);
  299. if(FAILED(hres))
  300. return hres;
  301. print_string(output);
  302. heap_free(output);
  303. return S_OK;
  304. }
  305. static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
  306. BSTR Prefix, IDispatch **out_Dispatch)
  307. {
  308. WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
  309. wine_dbgstr_w(Prefix), out_Dispatch);
  310. return E_NOTIMPL;
  311. }
  312. static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
  313. {
  314. WINE_FIXME("(%p)\n", Object);
  315. return E_NOTIMPL;
  316. }
  317. static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
  318. {
  319. WINE_FIXME("(%d)\n", Time);
  320. return E_NOTIMPL;
  321. }
  322. static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
  323. {
  324. WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
  325. return E_NOTIMPL;
  326. }
  327. static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
  328. {
  329. WINE_FIXME("(%p)\n", ppts);
  330. return E_NOTIMPL;
  331. }
  332. static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
  333. {
  334. WINE_FIXME("(%p)\n", ppts);
  335. return E_NOTIMPL;
  336. }
  337. static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
  338. {
  339. WINE_FIXME("(%p)\n", ppts);
  340. return E_NOTIMPL;
  341. }
  342. static const IHostVtbl HostVtbl = {
  343. Host_QueryInterface,
  344. Host_AddRef,
  345. Host_Release,
  346. Host_GetTypeInfoCount,
  347. Host_GetTypeInfo,
  348. Host_GetIDsOfNames,
  349. Host_Invoke,
  350. Host_get_Name,
  351. Host_get_Application,
  352. Host_get_FullName,
  353. Host_get_Path,
  354. Host_get_Interactive,
  355. Host_put_Interactive,
  356. Host_Quit,
  357. Host_get_ScriptName,
  358. Host_get_ScriptFullName,
  359. Host_get_Arguments,
  360. Host_get_Version,
  361. Host_get_BuildVersion,
  362. Host_get_Timeout,
  363. Host_put_Timeout,
  364. Host_CreateObject,
  365. Host_Echo,
  366. Host_GetObject,
  367. Host_DisconnectObject,
  368. Host_Sleep,
  369. Host_ConnectObject,
  370. Host_get_StdIn,
  371. Host_get_StdOut,
  372. Host_get_StdErr
  373. };
  374. IHost host_obj = { &HostVtbl };