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>
33 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
35 #define BUILDVERSION 16535
36 static const WCHAR wshVersionW
[] = L
"5.8";
38 VARIANT_BOOL wshInteractive
=
45 static HRESULT
to_string(VARIANT
*src
, BSTR
*dst
)
50 if(V_VT(src
) == VT_NULL
) {
51 *dst
= SysAllocString(L
"null");
52 return *dst
? S_OK
: E_OUTOFMEMORY
;
56 hres
= VariantChangeType(&v
, src
, 0, VT_BSTR
);
58 WARN("Could not convert argument %s to string\n", debugstr_variant(src
));
66 static void print_string(const WCHAR
*string
)
68 DWORD count
, ret
, len
, lena
;
72 MessageBoxW(NULL
, string
, L
"Windows Script Host", MB_OK
);
76 len
= lstrlenW(string
);
77 ret
= WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), string
, len
, &count
, NULL
);
79 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), L
"\r\n", lstrlenW(L
"\r\n"), &count
, NULL
);
83 lena
= WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, NULL
, 0, NULL
, NULL
);
84 buf
= heap_alloc(len
);
88 WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, buf
, lena
, NULL
, NULL
);
89 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, lena
, &count
, FALSE
);
91 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), "\r\n", 2, &count
, FALSE
);
94 static HRESULT WINAPI
Host_QueryInterface(IHost
*iface
, REFIID riid
, void **ppv
)
96 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
98 if(IsEqualGUID(&IID_IUnknown
, riid
)
99 || IsEqualGUID(&IID_IDispatch
, riid
)
100 || IsEqualGUID(&IID_IHost
, riid
)) {
106 return E_NOINTERFACE
;
109 static ULONG WINAPI
Host_AddRef(IHost
*iface
)
114 static ULONG WINAPI
Host_Release(IHost
*iface
)
119 static HRESULT WINAPI
Host_GetTypeInfoCount(IHost
*iface
, UINT
*pctinfo
)
121 WINE_TRACE("(%p)\n", pctinfo
);
126 static HRESULT WINAPI
Host_GetTypeInfo(IHost
*iface
, UINT iTInfo
, LCID lcid
,
129 WINE_TRACE("(%x %x %p\n", iTInfo
, lcid
, ppTInfo
);
131 ITypeInfo_AddRef(host_ti
);
136 static HRESULT WINAPI
Host_GetIDsOfNames(IHost
*iface
, REFIID riid
, LPOLESTR
*rgszNames
,
137 UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
139 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid
), rgszNames
,
140 cNames
, lcid
, rgDispId
);
142 return ITypeInfo_GetIDsOfNames(host_ti
, rgszNames
, cNames
, rgDispId
);
145 static HRESULT WINAPI
Host_Invoke(IHost
*iface
, DISPID dispIdMember
, REFIID riid
,
146 LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
,
147 EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
149 WINE_TRACE("(%d %p %p)\n", dispIdMember
, pDispParams
, pVarResult
);
151 return ITypeInfo_Invoke(host_ti
, iface
, dispIdMember
, wFlags
, pDispParams
,
152 pVarResult
, pExcepInfo
, puArgErr
);
155 static HRESULT WINAPI
Host_get_Name(IHost
*iface
, BSTR
*out_Name
)
157 WINE_TRACE("(%p)\n", out_Name
);
159 if(!(*out_Name
= SysAllocString(L
"Windows Script Host")))
160 return E_OUTOFMEMORY
;
164 static HRESULT WINAPI
Host_get_Application(IHost
*iface
, IDispatch
**out_Dispatch
)
166 WINE_TRACE("(%p)\n", out_Dispatch
);
168 *out_Dispatch
= (IDispatch
*)&host_obj
;
172 static HRESULT WINAPI
Host_get_FullName(IHost
*iface
, BSTR
*out_Path
)
174 WCHAR fullPath
[MAX_PATH
];
176 WINE_TRACE("(%p)\n", out_Path
);
178 if(GetModuleFileNameW(NULL
, fullPath
, ARRAY_SIZE(fullPath
)) == 0)
180 if(!(*out_Path
= SysAllocString(fullPath
)))
181 return E_OUTOFMEMORY
;
185 static HRESULT WINAPI
Host_get_Path(IHost
*iface
, BSTR
*out_Path
)
187 WCHAR path
[MAX_PATH
];
191 WINE_TRACE("(%p)\n", out_Path
);
193 if(GetModuleFileNameW(NULL
, path
, ARRAY_SIZE(path
)) == 0)
195 pos
= wcsrchr(path
, '\\');
196 howMany
= pos
- path
;
197 if(!(*out_Path
= SysAllocStringLen(path
, howMany
)))
198 return E_OUTOFMEMORY
;
202 static HRESULT WINAPI
Host_get_Interactive(IHost
*iface
, VARIANT_BOOL
*out_Interactive
)
204 WINE_TRACE("(%p)\n", out_Interactive
);
206 *out_Interactive
= wshInteractive
;
210 static HRESULT WINAPI
Host_put_Interactive(IHost
*iface
, VARIANT_BOOL v
)
212 WINE_TRACE("(%x)\n", v
);
218 static HRESULT WINAPI
Host_Quit(IHost
*iface
, int ExitCode
)
220 FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode
);
222 ExitProcess(ExitCode
);
226 static HRESULT WINAPI
Host_get_ScriptName(IHost
*iface
, BSTR
*out_ScriptName
)
230 WINE_TRACE("(%p)\n", out_ScriptName
);
232 scriptName
= wcsrchr(scriptFullName
, '\\');
234 if(!(*out_ScriptName
= SysAllocString(scriptName
)))
235 return E_OUTOFMEMORY
;
239 static HRESULT WINAPI
Host_get_ScriptFullName(IHost
*iface
, BSTR
*out_ScriptFullName
)
241 WINE_TRACE("(%p)\n", out_ScriptFullName
);
243 if(!(*out_ScriptFullName
= SysAllocString(scriptFullName
)))
244 return E_OUTOFMEMORY
;
248 static HRESULT WINAPI
Host_get_Arguments(IHost
*iface
, IArguments2
**out_Arguments
)
250 WINE_TRACE("(%p)\n", out_Arguments
);
252 *out_Arguments
= &arguments_obj
;
256 static HRESULT WINAPI
Host_get_Version(IHost
*iface
, BSTR
*out_Version
)
258 WINE_TRACE("(%p)\n", out_Version
);
260 if(!(*out_Version
= SysAllocString(wshVersionW
)))
261 return E_OUTOFMEMORY
;
265 static HRESULT WINAPI
Host_get_BuildVersion(IHost
*iface
, int *out_Build
)
267 WINE_TRACE("(%p)\n", out_Build
);
269 *out_Build
= BUILDVERSION
;
273 static HRESULT WINAPI
Host_get_Timeout(IHost
*iface
, LONG
*out_Timeout
)
275 WINE_FIXME("(%p)\n", out_Timeout
);
279 static HRESULT WINAPI
Host_put_Timeout(IHost
*iface
, LONG v
)
281 WINE_FIXME("(%d)\n", v
);
285 static HRESULT WINAPI
Host_CreateObject(IHost
*iface
, BSTR ProgID
, BSTR Prefix
,
286 IDispatch
**out_Dispatch
)
292 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID
), wine_dbgstr_w(Prefix
), out_Dispatch
);
294 if(Prefix
&& *Prefix
) {
295 FIXME("Prefix %s not supported\n", debugstr_w(Prefix
));
299 hres
= CLSIDFromProgID(ProgID
, &guid
);
303 hres
= CoCreateInstance(&guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_LOCAL_SERVER
|CLSCTX_REMOTE_SERVER
,
304 &IID_IUnknown
, (void**)&unk
);
308 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)out_Dispatch
);
309 IUnknown_Release(unk
);
313 static HRESULT WINAPI
Host_Echo(IHost
*iface
, SAFEARRAY
*args
)
315 WCHAR
*output
= NULL
, *ptr
;
316 unsigned argc
, i
, len
;
322 TRACE("(%p)\n", args
);
324 if(SafeArrayGetDim(args
) != 1) {
325 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args
));
329 SafeArrayGetLBound(args
, 1, &lbound
);
330 SafeArrayGetUBound(args
, 1, &ubound
);
332 hres
= SafeArrayAccessData(args
, (void**)&argv
);
336 argc
= ubound
-lbound
+1;
337 strs
= heap_alloc_zero(argc
*sizeof(*strs
));
339 SafeArrayUnaccessData(args
);
340 return E_OUTOFMEMORY
;
343 /* Len of spaces between arguments. */
346 for(i
=0; i
< argc
; i
++) {
347 hres
= to_string(argv
+i
, strs
+i
);
351 len
+= SysStringLen(strs
[i
]);
354 SafeArrayUnaccessData(args
);
355 if(SUCCEEDED(hres
)) {
356 ptr
= output
= heap_alloc((len
+1)*sizeof(WCHAR
));
358 for(i
=0; i
< argc
; i
++) {
361 len
= SysStringLen(strs
[i
]);
362 memcpy(ptr
, strs
[i
], len
*sizeof(WCHAR
));
367 hres
= E_OUTOFMEMORY
;
371 for(i
=0; i
< argc
; i
++)
372 SysFreeString(strs
[i
]);
377 print_string(output
);
383 static HRESULT WINAPI
Host_GetObject(IHost
*iface
, BSTR Pathname
, BSTR ProgID
,
384 BSTR Prefix
, IDispatch
**out_Dispatch
)
386 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname
), wine_dbgstr_w(ProgID
),
387 wine_dbgstr_w(Prefix
), out_Dispatch
);
391 static HRESULT WINAPI
Host_DisconnectObject(IHost
*iface
, IDispatch
*Object
)
393 WINE_FIXME("(%p)\n", Object
);
397 static HRESULT WINAPI
Host_Sleep(IHost
*iface
, LONG Time
)
399 WINE_FIXME("(%d)\n", Time
);
403 static HRESULT WINAPI
Host_ConnectObject(IHost
*iface
, IDispatch
*Object
, BSTR Prefix
)
405 WINE_FIXME("(%p %s)\n", Object
, wine_dbgstr_w(Prefix
));
409 static HRESULT WINAPI
Host_get_StdIn(IHost
*iface
, ITextStream
**ppts
)
411 WINE_FIXME("(%p)\n", ppts
);
415 static HRESULT WINAPI
Host_get_StdOut(IHost
*iface
, ITextStream
**ppts
)
417 WINE_FIXME("(%p)\n", ppts
);
421 static HRESULT WINAPI
Host_get_StdErr(IHost
*iface
, ITextStream
**ppts
)
423 WINE_FIXME("(%p)\n", ppts
);
427 static const IHostVtbl HostVtbl
= {
431 Host_GetTypeInfoCount
,
436 Host_get_Application
,
439 Host_get_Interactive
,
440 Host_put_Interactive
,
443 Host_get_ScriptFullName
,
446 Host_get_BuildVersion
,
452 Host_DisconnectObject
,
460 IHost host_obj
= { &HostVtbl
};