1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/*
* Copyright 2010 Jacek Caban for CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include <stdarg.h>
#define COBJMACROS
#define CONST_VTABLE
#include <windef.h>
#include <winbase.h>
#include <ole2.h>
#include "wscript.h"
#include <wine/debug.h>
WINE_DEFAULT_DEBUG_CHANNEL(wscript);
#define BUILDVERSION 16535
static const WCHAR wshVersionW[] = L"5.8";
VARIANT_BOOL wshInteractive =
#ifndef CSCRIPT_BUILD
VARIANT_TRUE;
#else
VARIANT_FALSE;
#endif
static HRESULT to_string(VARIANT *src, BSTR *dst)
{
VARIANT v;
HRESULT hres;
if(V_VT(src) == VT_NULL) {
*dst = SysAllocString(L"null");
return *dst ? S_OK : E_OUTOFMEMORY;
}
V_VT(&v) = VT_EMPTY;
hres = VariantChangeType(&v, src, 0, VT_BSTR);
if(FAILED(hres)) {
WARN("Could not convert argument %s to string\n", debugstr_variant(src));
return hres;
}
*dst = V_BSTR(&v);
return S_OK;
}
static void print_string(const WCHAR *string)
{
DWORD count, ret, len, lena;
char *buf;
if(wshInteractive) {
MessageBoxW(NULL, string, L"Windows Script Host", MB_OK);
return;
}
len = lstrlenW(string);
ret = WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), string, len, &count, NULL);
if(ret) {
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"\r\n", lstrlenW(L"\r\n"), &count, NULL);
return;
}
lena = WideCharToMultiByte(GetOEMCP(), 0, string, len, NULL, 0, NULL, NULL);
buf = malloc(len);
if(!buf)
return;
WideCharToMultiByte(GetOEMCP(), 0, string, len, buf, lena, NULL, NULL);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), buf, lena, &count, FALSE);
free(buf);
WriteFile(GetStdHandle(STD_OUTPUT_HANDLE), "\r\n", 2, &count, FALSE);
}
static HRESULT WINAPI Host_QueryInterface(IHost *iface, REFIID riid, void **ppv)
{
WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
if(IsEqualGUID(&IID_IUnknown, riid)
|| IsEqualGUID(&IID_IDispatch, riid)
|| IsEqualGUID(&IID_IHost, riid)) {
*ppv = iface;
return S_OK;
}
*ppv = NULL;
return E_NOINTERFACE;
}
static ULONG WINAPI Host_AddRef(IHost *iface)
{
return 2;
}
static ULONG WINAPI Host_Release(IHost *iface)
{
return 1;
}
static HRESULT WINAPI Host_GetTypeInfoCount(IHost *iface, UINT *pctinfo)
{
WINE_TRACE("(%p)\n", pctinfo);
*pctinfo = 1;
return S_OK;
}
static HRESULT WINAPI Host_GetTypeInfo(IHost *iface, UINT iTInfo, LCID lcid,
ITypeInfo **ppTInfo)
{
WINE_TRACE("(%x %lx %p\n", iTInfo, lcid, ppTInfo);
ITypeInfo_AddRef(host_ti);
*ppTInfo = host_ti;
return S_OK;
}
static HRESULT WINAPI Host_GetIDsOfNames(IHost *iface, REFIID riid, LPOLESTR *rgszNames,
UINT cNames, LCID lcid, DISPID *rgDispId)
{
WINE_TRACE("(%s %p %d %lx %p)\n", wine_dbgstr_guid(riid), rgszNames,
cNames, lcid, rgDispId);
return ITypeInfo_GetIDsOfNames(host_ti, rgszNames, cNames, rgDispId);
}
static HRESULT WINAPI Host_Invoke(IHost *iface, DISPID dispIdMember, REFIID riid,
LCID lcid, WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult,
EXCEPINFO *pExcepInfo, UINT *puArgErr)
{
WINE_TRACE("(%ld %p %p)\n", dispIdMember, pDispParams, pVarResult);
return ITypeInfo_Invoke(host_ti, iface, dispIdMember, wFlags, pDispParams,
pVarResult, pExcepInfo, puArgErr);
}
static HRESULT WINAPI Host_get_Name(IHost *iface, BSTR *out_Name)
{
WINE_TRACE("(%p)\n", out_Name);
if(!(*out_Name = SysAllocString(L"Windows Script Host")))
return E_OUTOFMEMORY;
return S_OK;
}
static HRESULT WINAPI Host_get_Application(IHost *iface, IDispatch **out_Dispatch)
{
WINE_TRACE("(%p)\n", out_Dispatch);
*out_Dispatch = (IDispatch*)&host_obj;
return S_OK;
}
static HRESULT WINAPI Host_get_FullName(IHost *iface, BSTR *out_Path)
{
WCHAR fullPath[MAX_PATH];
WINE_TRACE("(%p)\n", out_Path);
if(GetModuleFileNameW(NULL, fullPath, ARRAY_SIZE(fullPath)) == 0)
return E_FAIL;
if(!(*out_Path = SysAllocString(fullPath)))
return E_OUTOFMEMORY;
return S_OK;
}
static HRESULT WINAPI Host_get_Path(IHost *iface, BSTR *out_Path)
{
WCHAR path[MAX_PATH];
int howMany;
WCHAR *pos;
WINE_TRACE("(%p)\n", out_Path);
if(GetModuleFileNameW(NULL, path, ARRAY_SIZE(path)) == 0)
return E_FAIL;
pos = wcsrchr(path, '\\');
howMany = pos - path;
if(!(*out_Path = SysAllocStringLen(path, howMany)))
return E_OUTOFMEMORY;
return S_OK;
}
static HRESULT WINAPI Host_get_Interactive(IHost *iface, VARIANT_BOOL *out_Interactive)
{
WINE_TRACE("(%p)\n", out_Interactive);
*out_Interactive = wshInteractive;
return S_OK;
}
static HRESULT WINAPI Host_put_Interactive(IHost *iface, VARIANT_BOOL v)
{
WINE_TRACE("(%x)\n", v);
wshInteractive = v;
return S_OK;
}
static HRESULT WINAPI Host_Quit(IHost *iface, int ExitCode)
{
FIXME("(%d) semi-stub: no script engine clean up\n", ExitCode);
ExitProcess(ExitCode);
return S_OK;
}
static HRESULT WINAPI Host_get_ScriptName(IHost *iface, BSTR *out_ScriptName)
{
WCHAR *scriptName;
WINE_TRACE("(%p)\n", out_ScriptName);
scriptName = wcsrchr(scriptFullName, '\\');
++scriptName;
if(!(*out_ScriptName = SysAllocString(scriptName)))
return E_OUTOFMEMORY;
return S_OK;
}
static HRESULT WINAPI Host_get_ScriptFullName(IHost *iface, BSTR *out_ScriptFullName)
{
WINE_TRACE("(%p)\n", out_ScriptFullName);
if(!(*out_ScriptFullName = SysAllocString(scriptFullName)))
return E_OUTOFMEMORY;
return S_OK;
}
static HRESULT WINAPI Host_get_Arguments(IHost *iface, IArguments2 **out_Arguments)
{
WINE_TRACE("(%p)\n", out_Arguments);
*out_Arguments = &arguments_obj;
return S_OK;
}
static HRESULT WINAPI Host_get_Version(IHost *iface, BSTR *out_Version)
{
WINE_TRACE("(%p)\n", out_Version);
if(!(*out_Version = SysAllocString(wshVersionW)))
return E_OUTOFMEMORY;
return S_OK;
}
static HRESULT WINAPI Host_get_BuildVersion(IHost *iface, int *out_Build)
{
WINE_TRACE("(%p)\n", out_Build);
*out_Build = BUILDVERSION;
return S_OK;
}
static HRESULT WINAPI Host_get_Timeout(IHost *iface, LONG *out_Timeout)
{
WINE_FIXME("(%p)\n", out_Timeout);
return E_NOTIMPL;
}
static HRESULT WINAPI Host_put_Timeout(IHost *iface, LONG v)
{
WINE_FIXME("(%ld)\n", v);
return E_NOTIMPL;
}
static HRESULT WINAPI Host_CreateObject(IHost *iface, BSTR ProgID, BSTR Prefix,
IDispatch **out_Dispatch)
{
IUnknown *unk;
GUID guid;
HRESULT hres;
TRACE("(%s %s %p)\n", wine_dbgstr_w(ProgID), wine_dbgstr_w(Prefix), out_Dispatch);
if(Prefix && *Prefix) {
FIXME("Prefix %s not supported\n", debugstr_w(Prefix));
return E_NOTIMPL;
}
hres = CLSIDFromProgID(ProgID, &guid);
if(FAILED(hres))
return hres;
hres = CoCreateInstance(&guid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_LOCAL_SERVER|CLSCTX_REMOTE_SERVER,
&IID_IUnknown, (void**)&unk);
if(FAILED(hres))
return hres;
hres = IUnknown_QueryInterface(unk, &IID_IDispatch, (void**)out_Dispatch);
IUnknown_Release(unk);
return hres;
}
static HRESULT WINAPI Host_Echo(IHost *iface, SAFEARRAY *args)
{
WCHAR *output = NULL, *ptr;
unsigned argc, i, len;
LONG ubound, lbound;
VARIANT *argv;
BSTR *strs;
HRESULT hres;
TRACE("(%p)\n", args);
if(SafeArrayGetDim(args) != 1) {
FIXME("Unsupported args dim %d\n", SafeArrayGetDim(args));
return E_NOTIMPL;
}
SafeArrayGetLBound(args, 1, &lbound);
SafeArrayGetUBound(args, 1, &ubound);
hres = SafeArrayAccessData(args, (void**)&argv);
if(FAILED(hres))
return hres;
argc = ubound-lbound+1;
if (!(strs = calloc(argc, sizeof(*strs))))
{
SafeArrayUnaccessData(args);
return E_OUTOFMEMORY;
}
/* Len of spaces between arguments. */
len = argc-1;
for(i=0; i < argc; i++) {
hres = to_string(argv+i, strs+i);
if(FAILED(hres))
break;
len += SysStringLen(strs[i]);
}
SafeArrayUnaccessData(args);
if(SUCCEEDED(hres)) {
ptr = output = malloc((len+1)*sizeof(WCHAR));
if(output) {
for(i=0; i < argc; i++) {
if(i)
*ptr++ = ' ';
len = SysStringLen(strs[i]);
memcpy(ptr, strs[i], len*sizeof(WCHAR));
ptr += len;
}
*ptr = 0;
}else {
hres = E_OUTOFMEMORY;
}
}
for(i=0; i < argc; i++)
SysFreeString(strs[i]);
free(strs);
if(FAILED(hres))
return hres;
print_string(output);
free(output);
return S_OK;
}
static HRESULT WINAPI Host_GetObject(IHost *iface, BSTR Pathname, BSTR ProgID,
BSTR Prefix, IDispatch **out_Dispatch)
{
WINE_FIXME("(%s %s %s %p)\n", wine_dbgstr_w(Pathname), wine_dbgstr_w(ProgID),
wine_dbgstr_w(Prefix), out_Dispatch);
return E_NOTIMPL;
}
static HRESULT WINAPI Host_DisconnectObject(IHost *iface, IDispatch *Object)
{
WINE_FIXME("(%p)\n", Object);
return E_NOTIMPL;
}
static HRESULT WINAPI Host_Sleep(IHost *iface, LONG Time)
{
TRACE("(%ld)\n", Time);
if (Time < 0)
return E_INVALIDARG;
Sleep(Time);
return S_OK;
}
static HRESULT WINAPI Host_ConnectObject(IHost *iface, IDispatch *Object, BSTR Prefix)
{
WINE_FIXME("(%p %s)\n", Object, wine_dbgstr_w(Prefix));
return E_NOTIMPL;
}
static HRESULT WINAPI Host_get_StdIn(IHost *iface, ITextStream **ppts)
{
WINE_FIXME("(%p)\n", ppts);
return E_NOTIMPL;
}
static HRESULT WINAPI Host_get_StdOut(IHost *iface, ITextStream **ppts)
{
WINE_FIXME("(%p)\n", ppts);
return E_NOTIMPL;
}
static HRESULT WINAPI Host_get_StdErr(IHost *iface, ITextStream **ppts)
{
WINE_FIXME("(%p)\n", ppts);
return E_NOTIMPL;
}
static const IHostVtbl HostVtbl = {
Host_QueryInterface,
Host_AddRef,
Host_Release,
Host_GetTypeInfoCount,
Host_GetTypeInfo,
Host_GetIDsOfNames,
Host_Invoke,
Host_get_Name,
Host_get_Application,
Host_get_FullName,
Host_get_Path,
Host_get_Interactive,
Host_put_Interactive,
Host_Quit,
Host_get_ScriptName,
Host_get_ScriptFullName,
Host_get_Arguments,
Host_get_Version,
Host_get_BuildVersion,
Host_get_Timeout,
Host_put_Timeout,
Host_CreateObject,
Host_Echo,
Host_GetObject,
Host_DisconnectObject,
Host_Sleep,
Host_ConnectObject,
Host_get_StdIn,
Host_get_StdOut,
Host_get_StdErr
};
IHost host_obj = { &HostVtbl };
|