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/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
=
48 static HRESULT
to_string(VARIANT
*src
, BSTR
*dst
)
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
;
61 hres
= VariantChangeType(&v
, src
, 0, VT_BSTR
);
63 WARN("Could not convert argument %s to string\n", debugstr_variant(src
));
71 static void print_string(const WCHAR
*string
)
73 DWORD count
, ret
, len
, lena
;
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
);
83 len
= strlenW(string
);
84 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), string
, len
, &count
, NULL
);
86 static const WCHAR crnlW
[] = {'\r','\n'};
87 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), crnlW
, sizeof(crnlW
)/sizeof(*crnlW
), &count
, NULL
);
91 lena
= WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, NULL
, 0, NULL
, NULL
);
92 buf
= heap_alloc(len
);
96 WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, buf
, lena
, NULL
, NULL
);
97 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, lena
, &count
, FALSE
);
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
)) {
114 return E_NOINTERFACE
;
117 static ULONG WINAPI
Host_AddRef(IHost
*iface
)
122 static ULONG WINAPI
Host_Release(IHost
*iface
)
127 static HRESULT WINAPI
Host_GetTypeInfoCount(IHost
*iface
, UINT
*pctinfo
)
129 WINE_TRACE("(%p)\n", pctinfo
);
134 static HRESULT WINAPI
Host_GetTypeInfo(IHost
*iface
, UINT iTInfo
, LCID lcid
,
137 WINE_TRACE("(%x %x %p\n", iTInfo
, lcid
, ppTInfo
);
139 ITypeInfo_AddRef(host_ti
);
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
;
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
;
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
, sizeof(fullPath
)/sizeof(WCHAR
)) == 0)
188 if(!(*out_Path
= SysAllocString(fullPath
)))
189 return E_OUTOFMEMORY
;
193 static HRESULT WINAPI
Host_get_Path(IHost
*iface
, BSTR
*out_Path
)
195 WCHAR path
[MAX_PATH
];
199 WINE_TRACE("(%p)\n", out_Path
);
201 if(GetModuleFileNameW(NULL
, path
, sizeof(path
)/sizeof(WCHAR
)) == 0)
203 pos
= strrchrW(path
, '\\');
204 howMany
= pos
- path
;
205 if(!(*out_Path
= SysAllocStringLen(path
, howMany
)))
206 return E_OUTOFMEMORY
;
210 static HRESULT WINAPI
Host_get_Interactive(IHost
*iface
, VARIANT_BOOL
*out_Interactive
)
212 WINE_TRACE("(%p)\n", out_Interactive
);
214 *out_Interactive
= wshInteractive
;
218 static HRESULT WINAPI
Host_put_Interactive(IHost
*iface
, VARIANT_BOOL v
)
220 WINE_TRACE("(%x)\n", v
);
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
);
234 static HRESULT WINAPI
Host_get_ScriptName(IHost
*iface
, BSTR
*out_ScriptName
)
238 WINE_TRACE("(%p)\n", out_ScriptName
);
240 scriptName
= strrchrW(scriptFullName
, '\\');
242 if(!(*out_ScriptName
= SysAllocString(scriptName
)))
243 return E_OUTOFMEMORY
;
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
;
256 static HRESULT WINAPI
Host_get_Arguments(IHost
*iface
, IArguments2
**out_Arguments
)
258 WINE_TRACE("(%p)\n", out_Arguments
);
260 *out_Arguments
= &arguments_obj
;
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
;
273 static HRESULT WINAPI
Host_get_BuildVersion(IHost
*iface
, int *out_Build
)
275 WINE_TRACE("(%p)\n", out_Build
);
277 *out_Build
= BUILDVERSION
;
281 static HRESULT WINAPI
Host_get_Timeout(IHost
*iface
, LONG
*out_Timeout
)
283 WINE_FIXME("(%p)\n", out_Timeout
);
287 static HRESULT WINAPI
Host_put_Timeout(IHost
*iface
, LONG v
)
289 WINE_FIXME("(%d)\n", v
);
293 static HRESULT WINAPI
Host_CreateObject(IHost
*iface
, BSTR ProgID
, BSTR Prefix
,
294 IDispatch
**out_Dispatch
)
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
));
307 hres
= CLSIDFromProgID(ProgID
, &guid
);
311 hres
= CoCreateInstance(&guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_LOCAL_SERVER
|CLSCTX_REMOTE_SERVER
,
312 &IID_IUnknown
, (void**)&unk
);
316 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)out_Dispatch
);
317 IUnknown_Release(unk
);
321 static HRESULT WINAPI
Host_Echo(IHost
*iface
, SAFEARRAY
*args
)
323 WCHAR
*output
= NULL
, *ptr
;
324 unsigned argc
, i
, len
;
330 TRACE("(%p)\n", args
);
332 if(SafeArrayGetDim(args
) != 1) {
333 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args
));
337 SafeArrayGetLBound(args
, 1, &lbound
);
338 SafeArrayGetUBound(args
, 1, &ubound
);
340 hres
= SafeArrayAccessData(args
, (void**)&argv
);
344 argc
= ubound
-lbound
+1;
345 strs
= heap_alloc_zero(argc
*sizeof(*strs
));
347 SafeArrayUnaccessData(args
);
348 return E_OUTOFMEMORY
;
351 /* Len of spaces between arguments. */
354 for(i
=0; i
< argc
; i
++) {
355 hres
= to_string(argv
+i
, strs
+i
);
359 len
+= SysStringLen(strs
[i
]);
362 SafeArrayUnaccessData(args
);
363 if(SUCCEEDED(hres
)) {
364 ptr
= output
= heap_alloc((len
+1)*sizeof(WCHAR
));
366 for(i
=0; i
< argc
; i
++) {
369 len
= SysStringLen(strs
[i
]);
370 memcpy(ptr
, strs
[i
], len
*sizeof(WCHAR
));
375 hres
= E_OUTOFMEMORY
;
379 for(i
=0; i
< argc
; i
++)
380 SysFreeString(strs
[i
]);
385 print_string(output
);
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
);
399 static HRESULT WINAPI
Host_DisconnectObject(IHost
*iface
, IDispatch
*Object
)
401 WINE_FIXME("(%p)\n", Object
);
405 static HRESULT WINAPI
Host_Sleep(IHost
*iface
, LONG Time
)
407 WINE_FIXME("(%d)\n", Time
);
411 static HRESULT WINAPI
Host_ConnectObject(IHost
*iface
, IDispatch
*Object
, BSTR Prefix
)
413 WINE_FIXME("(%p %s)\n", Object
, wine_dbgstr_w(Prefix
));
417 static HRESULT WINAPI
Host_get_StdIn(IHost
*iface
, ITextStream
**ppts
)
419 WINE_FIXME("(%p)\n", ppts
);
423 static HRESULT WINAPI
Host_get_StdOut(IHost
*iface
, ITextStream
**ppts
)
425 WINE_FIXME("(%p)\n", ppts
);
429 static HRESULT WINAPI
Host_get_StdErr(IHost
*iface
, ITextStream
**ppts
)
431 WINE_FIXME("(%p)\n", ppts
);
435 static const IHostVtbl HostVtbl
= {
439 Host_GetTypeInfoCount
,
444 Host_get_Application
,
447 Host_get_Interactive
,
448 Host_put_Interactive
,
451 Host_get_ScriptFullName
,
454 Host_get_BuildVersion
,
460 Host_DisconnectObject
,
468 IHost host_obj
= { &HostVtbl
};