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
30 #include <wine/debug.h>
31 #include <wine/unicode.h>
33 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
35 #define BUILDVERSION 16535
37 static const WCHAR wshNameW
[] = {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0};
38 static const WCHAR wshVersionW
[] = {'5','.','8'};
40 VARIANT_BOOL wshInteractive
=
47 static HRESULT
to_string(VARIANT
*src
, BSTR
*dst
)
52 static const WCHAR nullW
[] = {'n','u','l','l',0};
54 if(V_VT(src
) == VT_NULL
) {
55 *dst
= SysAllocString(nullW
);
56 return *dst
? S_OK
: E_OUTOFMEMORY
;
60 hres
= VariantChangeType(&v
, src
, 0, VT_BSTR
);
62 WARN("Could not convert argument %s to string\n", debugstr_variant(src
));
70 static void print_string(const WCHAR
*string
)
72 DWORD count
, ret
, len
, lena
;
76 static const WCHAR windows_script_hostW
[] =
77 {'W','i','n','d','o','w','s',' ','S','c','r','i','p','t',' ','H','o','s','t',0};
78 MessageBoxW(NULL
, string
, windows_script_hostW
, MB_OK
);
82 len
= strlenW(string
);
83 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), string
, len
, &count
, NULL
);
85 static const WCHAR crnlW
[] = {'\r','\n'};
86 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), crnlW
, sizeof(crnlW
)/sizeof(*crnlW
), &count
, NULL
);
90 lena
= WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, NULL
, 0, NULL
, NULL
);
91 buf
= heap_alloc(len
);
95 WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, buf
, lena
, NULL
, NULL
);
96 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, lena
, &count
, FALSE
);
98 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), "\r\n", 2, &count
, FALSE
);
101 static HRESULT WINAPI
Host_QueryInterface(IHost
*iface
, REFIID riid
, void **ppv
)
103 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
105 if(IsEqualGUID(&IID_IUnknown
, riid
)
106 || IsEqualGUID(&IID_IDispatch
, riid
)
107 || IsEqualGUID(&IID_IHost
, riid
)) {
113 return E_NOINTERFACE
;
116 static ULONG WINAPI
Host_AddRef(IHost
*iface
)
121 static ULONG WINAPI
Host_Release(IHost
*iface
)
126 static HRESULT WINAPI
Host_GetTypeInfoCount(IHost
*iface
, UINT
*pctinfo
)
128 WINE_TRACE("(%p)\n", pctinfo
);
133 static HRESULT WINAPI
Host_GetTypeInfo(IHost
*iface
, UINT iTInfo
, LCID lcid
,
136 WINE_TRACE("(%x %x %p\n", iTInfo
, lcid
, ppTInfo
);
138 ITypeInfo_AddRef(host_ti
);
143 static HRESULT WINAPI
Host_GetIDsOfNames(IHost
*iface
, REFIID riid
, LPOLESTR
*rgszNames
,
144 UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
146 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid
), rgszNames
,
147 cNames
, lcid
, rgDispId
);
149 return ITypeInfo_GetIDsOfNames(host_ti
, rgszNames
, cNames
, rgDispId
);
152 static HRESULT WINAPI
Host_Invoke(IHost
*iface
, DISPID dispIdMember
, REFIID riid
,
153 LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
,
154 EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
156 WINE_TRACE("(%d %p %p)\n", dispIdMember
, pDispParams
, pVarResult
);
158 return ITypeInfo_Invoke(host_ti
, iface
, dispIdMember
, wFlags
, pDispParams
,
159 pVarResult
, pExcepInfo
, puArgErr
);
162 static HRESULT WINAPI
Host_get_Name(IHost
*iface
, BSTR
*out_Name
)
164 WINE_TRACE("(%p)\n", out_Name
);
166 if(!(*out_Name
= SysAllocString(wshNameW
)))
167 return E_OUTOFMEMORY
;
171 static HRESULT WINAPI
Host_get_Application(IHost
*iface
, IDispatch
**out_Dispatch
)
173 WINE_TRACE("(%p)\n", out_Dispatch
);
175 *out_Dispatch
= (IDispatch
*)&host_obj
;
179 static HRESULT WINAPI
Host_get_FullName(IHost
*iface
, BSTR
*out_Path
)
181 WCHAR fullPath
[MAX_PATH
];
183 WINE_TRACE("(%p)\n", out_Path
);
185 if(GetModuleFileNameW(NULL
, fullPath
, sizeof(fullPath
)/sizeof(WCHAR
)) == 0)
187 if(!(*out_Path
= SysAllocString(fullPath
)))
188 return E_OUTOFMEMORY
;
192 static HRESULT WINAPI
Host_get_Path(IHost
*iface
, BSTR
*out_Path
)
194 WCHAR path
[MAX_PATH
];
198 WINE_TRACE("(%p)\n", out_Path
);
200 if(GetModuleFileNameW(NULL
, path
, sizeof(path
)/sizeof(WCHAR
)) == 0)
202 pos
= strrchrW(path
, '\\');
203 howMany
= pos
- path
;
204 if(!(*out_Path
= SysAllocStringLen(path
, howMany
)))
205 return E_OUTOFMEMORY
;
209 static HRESULT WINAPI
Host_get_Interactive(IHost
*iface
, VARIANT_BOOL
*out_Interactive
)
211 WINE_TRACE("(%p)\n", out_Interactive
);
213 *out_Interactive
= wshInteractive
;
217 static HRESULT WINAPI
Host_put_Interactive(IHost
*iface
, VARIANT_BOOL v
)
219 WINE_TRACE("(%x)\n", v
);
225 static HRESULT WINAPI
Host_Quit(IHost
*iface
, int ExitCode
)
227 WINE_FIXME("(%d)\n", ExitCode
);
231 static HRESULT WINAPI
Host_get_ScriptName(IHost
*iface
, BSTR
*out_ScriptName
)
235 WINE_TRACE("(%p)\n", out_ScriptName
);
237 scriptName
= strrchrW(scriptFullName
, '\\');
239 if(!(*out_ScriptName
= SysAllocString(scriptName
)))
240 return E_OUTOFMEMORY
;
244 static HRESULT WINAPI
Host_get_ScriptFullName(IHost
*iface
, BSTR
*out_ScriptFullName
)
246 WINE_TRACE("(%p)\n", out_ScriptFullName
);
248 if(!(*out_ScriptFullName
= SysAllocString(scriptFullName
)))
249 return E_OUTOFMEMORY
;
253 static HRESULT WINAPI
Host_get_Arguments(IHost
*iface
, IArguments2
**out_Arguments
)
255 WINE_TRACE("(%p)\n", out_Arguments
);
257 *out_Arguments
= &arguments_obj
;
261 static HRESULT WINAPI
Host_get_Version(IHost
*iface
, BSTR
*out_Version
)
263 WINE_TRACE("(%p)\n", out_Version
);
265 if(!(*out_Version
= SysAllocString(wshVersionW
)))
266 return E_OUTOFMEMORY
;
270 static HRESULT WINAPI
Host_get_BuildVersion(IHost
*iface
, int *out_Build
)
272 WINE_TRACE("(%p)\n", out_Build
);
274 *out_Build
= BUILDVERSION
;
278 static HRESULT WINAPI
Host_get_Timeout(IHost
*iface
, LONG
*out_Timeout
)
280 WINE_FIXME("(%p)\n", out_Timeout
);
284 static HRESULT WINAPI
Host_put_Timeout(IHost
*iface
, LONG v
)
286 WINE_FIXME("(%d)\n", v
);
290 static HRESULT WINAPI
Host_CreateObject(IHost
*iface
, BSTR ProgID
, BSTR Prefix
,
291 IDispatch
**out_Dispatch
)
297 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID
), wine_dbgstr_w(Prefix
), out_Dispatch
);
299 if(Prefix
&& *Prefix
) {
300 FIXME("Prefix %s not supported\n", debugstr_w(Prefix
));
304 hres
= CLSIDFromProgID(ProgID
, &guid
);
308 hres
= CoCreateInstance(&guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_LOCAL_SERVER
|CLSCTX_REMOTE_SERVER
,
309 &IID_IUnknown
, (void**)&unk
);
313 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)out_Dispatch
);
314 IUnknown_Release(unk
);
318 static HRESULT WINAPI
Host_Echo(IHost
*iface
, SAFEARRAY
*args
)
320 WCHAR
*output
= NULL
, *ptr
;
321 unsigned argc
, i
, len
;
327 TRACE("(%p)\n", args
);
329 if(SafeArrayGetDim(args
) != 1) {
330 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args
));
334 SafeArrayGetLBound(args
, 1, &lbound
);
335 SafeArrayGetUBound(args
, 1, &ubound
);
337 hres
= SafeArrayAccessData(args
, (void**)&argv
);
341 argc
= ubound
-lbound
+1;
342 strs
= heap_alloc_zero(argc
*sizeof(*strs
));
344 SafeArrayUnaccessData(args
);
345 return E_OUTOFMEMORY
;
348 /* Len of spaces between arguments. */
351 for(i
=0; i
< argc
; i
++) {
352 hres
= to_string(argv
+i
, strs
+i
);
356 len
+= SysStringLen(strs
[i
]);
359 SafeArrayUnaccessData(args
);
360 if(SUCCEEDED(hres
)) {
361 ptr
= output
= heap_alloc((len
+1)*sizeof(WCHAR
));
363 for(i
=0; i
< argc
; i
++) {
366 len
= SysStringLen(strs
[i
]);
367 memcpy(ptr
, strs
[i
], len
*sizeof(WCHAR
));
372 hres
= E_OUTOFMEMORY
;
376 for(i
=0; i
< argc
; i
++)
377 SysFreeString(strs
[i
]);
382 print_string(output
);
388 static HRESULT WINAPI
Host_GetObject(IHost
*iface
, BSTR Pathname
, BSTR ProgID
,
389 BSTR Prefix
, IDispatch
**out_Dispatch
)
391 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname
), wine_dbgstr_w(ProgID
),
392 wine_dbgstr_w(Prefix
), out_Dispatch
);
396 static HRESULT WINAPI
Host_DisconnectObject(IHost
*iface
, IDispatch
*Object
)
398 WINE_FIXME("(%p)\n", Object
);
402 static HRESULT WINAPI
Host_Sleep(IHost
*iface
, LONG Time
)
404 WINE_FIXME("(%d)\n", Time
);
408 static HRESULT WINAPI
Host_ConnectObject(IHost
*iface
, IDispatch
*Object
, BSTR Prefix
)
410 WINE_FIXME("(%p %s)\n", Object
, wine_dbgstr_w(Prefix
));
414 static HRESULT WINAPI
Host_get_StdIn(IHost
*iface
, ITextStream
**ppts
)
416 WINE_FIXME("(%p)\n", ppts
);
420 static HRESULT WINAPI
Host_get_StdOut(IHost
*iface
, ITextStream
**ppts
)
422 WINE_FIXME("(%p)\n", ppts
);
426 static HRESULT WINAPI
Host_get_StdErr(IHost
*iface
, ITextStream
**ppts
)
428 WINE_FIXME("(%p)\n", ppts
);
432 static const IHostVtbl HostVtbl
= {
436 Host_GetTypeInfoCount
,
441 Host_get_Application
,
444 Host_get_Interactive
,
445 Host_put_Interactive
,
448 Host_get_ScriptFullName
,
451 Host_get_BuildVersion
,
457 Host_DisconnectObject
,
465 IHost host_obj
= { &HostVtbl
};