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
[] = {'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 static const WCHAR crnlW
[] = {'\r','\n'};
80 WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE
), crnlW
, ARRAY_SIZE(crnlW
), &count
, NULL
);
84 lena
= WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, NULL
, 0, NULL
, NULL
);
85 buf
= heap_alloc(len
);
89 WideCharToMultiByte(GetConsoleOutputCP(), 0, string
, len
, buf
, lena
, NULL
, NULL
);
90 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), buf
, lena
, &count
, FALSE
);
92 WriteFile(GetStdHandle(STD_OUTPUT_HANDLE
), "\r\n", 2, &count
, FALSE
);
95 static HRESULT WINAPI
Host_QueryInterface(IHost
*iface
, REFIID riid
, void **ppv
)
97 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
99 if(IsEqualGUID(&IID_IUnknown
, riid
)
100 || IsEqualGUID(&IID_IDispatch
, riid
)
101 || IsEqualGUID(&IID_IHost
, riid
)) {
107 return E_NOINTERFACE
;
110 static ULONG WINAPI
Host_AddRef(IHost
*iface
)
115 static ULONG WINAPI
Host_Release(IHost
*iface
)
120 static HRESULT WINAPI
Host_GetTypeInfoCount(IHost
*iface
, UINT
*pctinfo
)
122 WINE_TRACE("(%p)\n", pctinfo
);
127 static HRESULT WINAPI
Host_GetTypeInfo(IHost
*iface
, UINT iTInfo
, LCID lcid
,
130 WINE_TRACE("(%x %x %p\n", iTInfo
, lcid
, ppTInfo
);
132 ITypeInfo_AddRef(host_ti
);
137 static HRESULT WINAPI
Host_GetIDsOfNames(IHost
*iface
, REFIID riid
, LPOLESTR
*rgszNames
,
138 UINT cNames
, LCID lcid
, DISPID
*rgDispId
)
140 WINE_TRACE("(%s %p %d %x %p)\n", wine_dbgstr_guid(riid
), rgszNames
,
141 cNames
, lcid
, rgDispId
);
143 return ITypeInfo_GetIDsOfNames(host_ti
, rgszNames
, cNames
, rgDispId
);
146 static HRESULT WINAPI
Host_Invoke(IHost
*iface
, DISPID dispIdMember
, REFIID riid
,
147 LCID lcid
, WORD wFlags
, DISPPARAMS
*pDispParams
, VARIANT
*pVarResult
,
148 EXCEPINFO
*pExcepInfo
, UINT
*puArgErr
)
150 WINE_TRACE("(%d %p %p)\n", dispIdMember
, pDispParams
, pVarResult
);
152 return ITypeInfo_Invoke(host_ti
, iface
, dispIdMember
, wFlags
, pDispParams
,
153 pVarResult
, pExcepInfo
, puArgErr
);
156 static HRESULT WINAPI
Host_get_Name(IHost
*iface
, BSTR
*out_Name
)
158 WINE_TRACE("(%p)\n", out_Name
);
160 if(!(*out_Name
= SysAllocString(L
"Windows Script Host")))
161 return E_OUTOFMEMORY
;
165 static HRESULT WINAPI
Host_get_Application(IHost
*iface
, IDispatch
**out_Dispatch
)
167 WINE_TRACE("(%p)\n", out_Dispatch
);
169 *out_Dispatch
= (IDispatch
*)&host_obj
;
173 static HRESULT WINAPI
Host_get_FullName(IHost
*iface
, BSTR
*out_Path
)
175 WCHAR fullPath
[MAX_PATH
];
177 WINE_TRACE("(%p)\n", out_Path
);
179 if(GetModuleFileNameW(NULL
, fullPath
, ARRAY_SIZE(fullPath
)) == 0)
181 if(!(*out_Path
= SysAllocString(fullPath
)))
182 return E_OUTOFMEMORY
;
186 static HRESULT WINAPI
Host_get_Path(IHost
*iface
, BSTR
*out_Path
)
188 WCHAR path
[MAX_PATH
];
192 WINE_TRACE("(%p)\n", out_Path
);
194 if(GetModuleFileNameW(NULL
, path
, ARRAY_SIZE(path
)) == 0)
196 pos
= wcsrchr(path
, '\\');
197 howMany
= pos
- path
;
198 if(!(*out_Path
= SysAllocStringLen(path
, howMany
)))
199 return E_OUTOFMEMORY
;
203 static HRESULT WINAPI
Host_get_Interactive(IHost
*iface
, VARIANT_BOOL
*out_Interactive
)
205 WINE_TRACE("(%p)\n", out_Interactive
);
207 *out_Interactive
= wshInteractive
;
211 static HRESULT WINAPI
Host_put_Interactive(IHost
*iface
, VARIANT_BOOL v
)
213 WINE_TRACE("(%x)\n", v
);
219 static HRESULT WINAPI
Host_Quit(IHost
*iface
, int ExitCode
)
221 FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode
);
223 ExitProcess(ExitCode
);
227 static HRESULT WINAPI
Host_get_ScriptName(IHost
*iface
, BSTR
*out_ScriptName
)
231 WINE_TRACE("(%p)\n", out_ScriptName
);
233 scriptName
= wcsrchr(scriptFullName
, '\\');
235 if(!(*out_ScriptName
= SysAllocString(scriptName
)))
236 return E_OUTOFMEMORY
;
240 static HRESULT WINAPI
Host_get_ScriptFullName(IHost
*iface
, BSTR
*out_ScriptFullName
)
242 WINE_TRACE("(%p)\n", out_ScriptFullName
);
244 if(!(*out_ScriptFullName
= SysAllocString(scriptFullName
)))
245 return E_OUTOFMEMORY
;
249 static HRESULT WINAPI
Host_get_Arguments(IHost
*iface
, IArguments2
**out_Arguments
)
251 WINE_TRACE("(%p)\n", out_Arguments
);
253 *out_Arguments
= &arguments_obj
;
257 static HRESULT WINAPI
Host_get_Version(IHost
*iface
, BSTR
*out_Version
)
259 WINE_TRACE("(%p)\n", out_Version
);
261 if(!(*out_Version
= SysAllocString(wshVersionW
)))
262 return E_OUTOFMEMORY
;
266 static HRESULT WINAPI
Host_get_BuildVersion(IHost
*iface
, int *out_Build
)
268 WINE_TRACE("(%p)\n", out_Build
);
270 *out_Build
= BUILDVERSION
;
274 static HRESULT WINAPI
Host_get_Timeout(IHost
*iface
, LONG
*out_Timeout
)
276 WINE_FIXME("(%p)\n", out_Timeout
);
280 static HRESULT WINAPI
Host_put_Timeout(IHost
*iface
, LONG v
)
282 WINE_FIXME("(%d)\n", v
);
286 static HRESULT WINAPI
Host_CreateObject(IHost
*iface
, BSTR ProgID
, BSTR Prefix
,
287 IDispatch
**out_Dispatch
)
293 TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID
), wine_dbgstr_w(Prefix
), out_Dispatch
);
295 if(Prefix
&& *Prefix
) {
296 FIXME("Prefix %s not supported\n", debugstr_w(Prefix
));
300 hres
= CLSIDFromProgID(ProgID
, &guid
);
304 hres
= CoCreateInstance(&guid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_LOCAL_SERVER
|CLSCTX_REMOTE_SERVER
,
305 &IID_IUnknown
, (void**)&unk
);
309 hres
= IUnknown_QueryInterface(unk
, &IID_IDispatch
, (void**)out_Dispatch
);
310 IUnknown_Release(unk
);
314 static HRESULT WINAPI
Host_Echo(IHost
*iface
, SAFEARRAY
*args
)
316 WCHAR
*output
= NULL
, *ptr
;
317 unsigned argc
, i
, len
;
323 TRACE("(%p)\n", args
);
325 if(SafeArrayGetDim(args
) != 1) {
326 FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args
));
330 SafeArrayGetLBound(args
, 1, &lbound
);
331 SafeArrayGetUBound(args
, 1, &ubound
);
333 hres
= SafeArrayAccessData(args
, (void**)&argv
);
337 argc
= ubound
-lbound
+1;
338 strs
= heap_alloc_zero(argc
*sizeof(*strs
));
340 SafeArrayUnaccessData(args
);
341 return E_OUTOFMEMORY
;
344 /* Len of spaces between arguments. */
347 for(i
=0; i
< argc
; i
++) {
348 hres
= to_string(argv
+i
, strs
+i
);
352 len
+= SysStringLen(strs
[i
]);
355 SafeArrayUnaccessData(args
);
356 if(SUCCEEDED(hres
)) {
357 ptr
= output
= heap_alloc((len
+1)*sizeof(WCHAR
));
359 for(i
=0; i
< argc
; i
++) {
362 len
= SysStringLen(strs
[i
]);
363 memcpy(ptr
, strs
[i
], len
*sizeof(WCHAR
));
368 hres
= E_OUTOFMEMORY
;
372 for(i
=0; i
< argc
; i
++)
373 SysFreeString(strs
[i
]);
378 print_string(output
);
384 static HRESULT WINAPI
Host_GetObject(IHost
*iface
, BSTR Pathname
, BSTR ProgID
,
385 BSTR Prefix
, IDispatch
**out_Dispatch
)
387 WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname
), wine_dbgstr_w(ProgID
),
388 wine_dbgstr_w(Prefix
), out_Dispatch
);
392 static HRESULT WINAPI
Host_DisconnectObject(IHost
*iface
, IDispatch
*Object
)
394 WINE_FIXME("(%p)\n", Object
);
398 static HRESULT WINAPI
Host_Sleep(IHost
*iface
, LONG Time
)
400 WINE_FIXME("(%d)\n", Time
);
404 static HRESULT WINAPI
Host_ConnectObject(IHost
*iface
, IDispatch
*Object
, BSTR Prefix
)
406 WINE_FIXME("(%p %s)\n", Object
, wine_dbgstr_w(Prefix
));
410 static HRESULT WINAPI
Host_get_StdIn(IHost
*iface
, ITextStream
**ppts
)
412 WINE_FIXME("(%p)\n", ppts
);
416 static HRESULT WINAPI
Host_get_StdOut(IHost
*iface
, ITextStream
**ppts
)
418 WINE_FIXME("(%p)\n", ppts
);
422 static HRESULT WINAPI
Host_get_StdErr(IHost
*iface
, ITextStream
**ppts
)
424 WINE_FIXME("(%p)\n", ppts
);
428 static const IHostVtbl HostVtbl
= {
432 Host_GetTypeInfoCount
,
437 Host_get_Application
,
440 Host_get_Interactive
,
441 Host_put_Interactive
,
444 Host_get_ScriptFullName
,
447 Host_get_BuildVersion
,
453 Host_DisconnectObject
,
461 IHost host_obj
= { &HostVtbl
};