mfplat: Add support for GUID attributes.
[wine.git] / programs / wscript / host.c
blobfb1c86fc620160bcdbd18766f3831104dc03aac9
1 /*
2 * Copyright 2010 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <stdarg.h>
21 #define COBJMACROS
22 #define CONST_VTABLE
24 #include <windef.h>
25 #include <winbase.h>
26 #include <ole2.h>
28 #include "wscript.h"
30 #include <wine/debug.h>
31 #include <wine/heap.h>
32 #include <wine/unicode.h>
34 WINE_DEFAULT_DEBUG_CHANNEL(wscript);
36 #define BUILDVERSION 16535
38 static const WCHAR wshNameW[] = {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0};
39 static const WCHAR wshVersionW[] = {'5','.','8'};
41 VARIANT_BOOL wshInteractive =
42 #ifndef CSCRIPT_BUILD
43 VARIANT_TRUE;
44 #else
45 VARIANT_FALSE;
46 #endif
48 static HRESULT to_string(VARIANT *src, BSTR *dst)
50 VARIANT v;
51 HRESULT hres;
53 static const WCHAR nullW[] = {'n','u','l','l',0};
55 if(V_VT(src) == VT_NULL) {
56 *dst = SysAllocString(nullW);
57 return *dst ? S_OK : E_OUTOFMEMORY;
60 V_VT(&v) = VT_EMPTY;
61 hres = VariantChangeType(&v, src, 0, VT_BSTR);
62 if(FAILED(hres)) {
63 WARN("Could not convert argument %s to string\n", debugstr_variant(src));
64 return hres;
67 *dst = V_BSTR(&v);
68 return S_OK;
71 static void print_string(const WCHAR *string)
73 DWORD count, ret, len, lena;
74 char *buf;
76 if(wshInteractive) {
77 static const WCHAR windows_script_hostW[] =
78 {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0};
79 MessageBoxW(NULL, string, windows_script_hostW, MB_OK);
80 return;
83 len = strlenW(string);
84 ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string, len, &count, NULL);
85 if(ret) {
86 static const WCHAR crnlW[] = {'\r','\n'};
87 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), crnlW, ARRAY_SIZE(crnlW), &count, NULL);
88 return;
91 lena = WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, NULL, 0, NULL, NULL);
92 buf = heap_alloc(len);
93 if(!buf)
94 return;
96 WideCharToMultiByte(GetConsoleOutputCP(), 0, string, len, buf, lena, NULL, NULL);
97 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE);
98 heap_free(buf);
99 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE);
102 static HRESULT WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
104 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
106 if(IsEqualGUID(&IID_IUnknown, riid)
107 || IsEqualGUID(&IID_IDispatch, riid)
108 || IsEqualGUID(&IID_IHost, riid)) {
109 *ppv = iface;
110 return S_OK;
113 *ppv = NULL;
114 return E_NOINTERFACE;
117 static ULONG WINAPI Host_AddRef(IHost *iface)
119 return 2;
122 static ULONG WINAPI Host_Release(IHost *iface)
124 return 1;
127 static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
129 WINE_TRACE("(%p)\n", pctinfo);
130 *pctinfo = 1;
131 return S_OK;
134 static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
135 ITypeInfo **ppTInfo)
137 WINE_TRACE("(%x %x %p\n", iTInfo, lcid, ppTInfo);
139 ITypeInfo_AddRef(host_ti);
140 *ppTInfo = host_ti;
141 return S_OK;
144 static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
145 UINT cNames, LCID lcid, DISPID *rgDispId)
147 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid), rgszNames,
148 cNames, lcid, rgDispId);
150 return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
153 static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
154 LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
155 EXCEPINFO *pExcepInfo, UINT *puArgErr)
157 WINE_TRACE("(%d %p %p)\n", dispIdMember, pDispParams, pVarResult);
159 return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
160 pVarResult, pExcepInfo, puArgErr);
163 static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
165 WINE_TRACE("(%p)\n", out_Name);
167 if(!(*out_Name = SysAllocString(wshNameW)))
168 return E_OUTOFMEMORY;
169 return S_OK;
172 static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
174 WINE_TRACE("(%p)\n", out_Dispatch);
176 *out_Dispatch = (IDispatch*)&host_obj;
177 return S_OK;
180 static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
182 WCHAR fullPath[MAX_PATH];
184 WINE_TRACE("(%p)\n", out_Path);
186 if(GetModuleFileNameW(NULL, fullPath, ARRAY_SIZE(fullPath)) == 0)
187 return E_FAIL;
188 if(!(*out_Path = SysAllocString(fullPath)))
189 return E_OUTOFMEMORY;
190 return S_OK;
193 static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
195 WCHAR path[MAX_PATH];
196 int howMany;
197 WCHAR *pos;
199 WINE_TRACE("(%p)\n", out_Path);
201 if(GetModuleFileNameW(NULL, path, ARRAY_SIZE(path)) == 0)
202 return E_FAIL;
203 pos = strrchrW(path, '\\');
204 howMany = pos - path;
205 if(!(*out_Path = SysAllocStringLen(path, howMany)))
206 return E_OUTOFMEMORY;
207 return S_OK;
210 static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
212 WINE_TRACE("(%p)\n", out_Interactive);
214 *out_Interactive = wshInteractive;
215 return S_OK;
218 static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
220 WINE_TRACE("(%x)\n", v);
222 wshInteractive = v;
223 return S_OK;
226 static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
228 FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode);
230 ExitProcess(ExitCode);
231 return S_OK;
234 static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
236 WCHAR *scriptName;
238 WINE_TRACE("(%p)\n", out_ScriptName);
240 scriptName = strrchrW(scriptFullName, '\\');
241 ++scriptName;
242 if(!(*out_ScriptName = SysAllocString(scriptName)))
243 return E_OUTOFMEMORY;
244 return S_OK;
247 static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
249 WINE_TRACE("(%p)\n", out_ScriptFullName);
251 if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
252 return E_OUTOFMEMORY;
253 return S_OK;
256 static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
258 WINE_TRACE("(%p)\n", out_Arguments);
260 *out_Arguments = &arguments_obj;
261 return S_OK;
264 static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
266 WINE_TRACE("(%p)\n", out_Version);
268 if(!(*out_Version = SysAllocString(wshVersionW)))
269 return E_OUTOFMEMORY;
270 return S_OK;
273 static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
275 WINE_TRACE("(%p)\n", out_Build);
277 *out_Build = BUILDVERSION;
278 return S_OK;
281 static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
283 WINE_FIXME("(%p)\n", out_Timeout);
284 return E_NOTIMPL;
287 static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
289 WINE_FIXME("(%d)\n", v);
290 return E_NOTIMPL;
293 static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
294 IDispatch **out_Dispatch)
296 IUnknown *unk;
297 GUID guid;
298 HRESULT hres;
300 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
302 if(Prefix && *Prefix) {
303 FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
304 return E_NOTIMPL;
307 hres = CLSIDFromProgID(ProgID, &guid);
308 if(FAILED(hres))
309 return hres;
311 hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
312 &IID_IUnknown, (void**)&unk);
313 if(FAILED(hres))
314 return hres;
316 hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
317 IUnknown_Release(unk);
318 return hres;
321 static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
323 WCHAR *output = NULL, *ptr;
324 unsigned argc, i, len;
325 int ubound, lbound;
326 VARIANT *argv;
327 BSTR *strs;
328 HRESULT hres;
330 TRACE("(%p)\n", args);
332 if(SafeArrayGetDim(args) != 1) {
333 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args));
334 return E_NOTIMPL;
337 SafeArrayGetLBound(args, 1, &lbound);
338 SafeArrayGetUBound(args, 1, &ubound);
340 hres = SafeArrayAccessData(args, (void**)&argv);
341 if(FAILED(hres))
342 return hres;
344 argc = ubound-lbound+1;
345 strs = heap_alloc_zero(argc*sizeof(*strs));
346 if(!strs) {
347 SafeArrayUnaccessData(args);
348 return E_OUTOFMEMORY;
351 /* Len of spaces between arguments. */
352 len = argc-1;
354 for(i=0; i < argc; i++) {
355 hres = to_string(argv+i, strs+i);
356 if(FAILED(hres))
357 break;
359 len += SysStringLen(strs[i]);
362 SafeArrayUnaccessData(args);
363 if(SUCCEEDED(hres)) {
364 ptr = output = heap_alloc((len+1)*sizeof(WCHAR));
365 if(output) {
366 for(i=0; i < argc; i++) {
367 if(i)
368 *ptr++ = ' ';
369 len = SysStringLen(strs[i]);
370 memcpy(ptr, strs[i], len*sizeof(WCHAR));
371 ptr += len;
373 *ptr = 0;
374 }else {
375 hres = E_OUTOFMEMORY;
379 for(i=0; i < argc; i++)
380 SysFreeString(strs[i]);
381 heap_free(strs);
382 if(FAILED(hres))
383 return hres;
385 print_string(output);
387 heap_free(output);
388 return S_OK;
391 static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
392 BSTR Prefix, IDispatch **out_Dispatch)
394 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
395 wine_dbgstr_w(Prefix), out_Dispatch);
396 return E_NOTIMPL;
399 static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
401 WINE_FIXME("(%p)\n", Object);
402 return E_NOTIMPL;
405 static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
407 WINE_FIXME("(%d)\n", Time);
408 return E_NOTIMPL;
411 static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
413 WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
414 return E_NOTIMPL;
417 static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
419 WINE_FIXME("(%p)\n", ppts);
420 return E_NOTIMPL;
423 static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
425 WINE_FIXME("(%p)\n", ppts);
426 return E_NOTIMPL;
429 static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
431 WINE_FIXME("(%p)\n", ppts);
432 return E_NOTIMPL;
435 static const IHostVtbl HostVtbl = {
436 Host_QueryInterface,
437 Host_AddRef,
438 Host_Release,
439 Host_GetTypeInfoCount,
440 Host_GetTypeInfo,
441 Host_GetIDsOfNames,
442 Host_Invoke,
443 Host_get_Name,
444 Host_get_Application,
445 Host_get_FullName,
446 Host_get_Path,
447 Host_get_Interactive,
448 Host_put_Interactive,
449 Host_Quit,
450 Host_get_ScriptName,
451 Host_get_ScriptFullName,
452 Host_get_Arguments,
453 Host_get_Version,
454 Host_get_BuildVersion,
455 Host_get_Timeout,
456 Host_put_Timeout,
457 Host_CreateObject,
458 Host_Echo,
459 Host_GetObject,
460 Host_DisconnectObject,
461 Host_Sleep,
462 Host_ConnectObject,
463 Host_get_StdIn,
464 Host_get_StdOut,
465 Host_get_StdErr
468 IHost host_obj = { &HostVtbl };