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
32 #include <wine/debug.h>
33 #include <wine/unicode.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
37 static const WCHAR wscriptW
[] = {'W','S','c','r','i','p','t',0};
38 static const WCHAR wshW
[] = {'W','S','H',0};
39 WCHAR scriptFullName
[MAX_PATH
];
42 ITypeInfo
*arguments_ti
;
44 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
45 REFIID riid
, void **ppv
)
47 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
48 WINE_TRACE("(IID_IUnknown %p)\n", ppv
);
50 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
51 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
55 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
59 IUnknown_AddRef((IUnknown
*)*ppv
);
63 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
68 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
73 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
77 *plcid
= GetUserDefaultLCID();
81 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
,
82 LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
**ppunkItem
, ITypeInfo
**ppti
)
84 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName
), dwReturnMask
, ppunkItem
, ppti
);
86 if(strcmpW(pstrName
, wshW
) && strcmpW(pstrName
, wscriptW
))
89 if(dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
90 ITypeInfo_AddRef(host_ti
);
94 if(dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
95 IHost_AddRef(&host_obj
);
96 *ppunkItem
= (IUnknown
*)&host_obj
;
102 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
,
109 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
110 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
116 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
,
117 SCRIPTSTATE ssScriptState
)
119 WINE_TRACE("(%x)\n", ssScriptState
);
123 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
,
124 IActiveScriptError
*pscripterror
)
130 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
136 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
142 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
143 ActiveScriptSite_QueryInterface
,
144 ActiveScriptSite_AddRef
,
145 ActiveScriptSite_Release
,
146 ActiveScriptSite_GetLCID
,
147 ActiveScriptSite_GetItemInfo
,
148 ActiveScriptSite_GetDocVersionString
,
149 ActiveScriptSite_OnScriptTerminate
,
150 ActiveScriptSite_OnStateChange
,
151 ActiveScriptSite_OnScriptError
,
152 ActiveScriptSite_OnEnterScript
,
153 ActiveScriptSite_OnLeaveScript
156 IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
158 static BOOL
load_typelib(void)
163 static const WCHAR wscript_exeW
[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
165 hres
= LoadTypeLib(wscript_exeW
, &typelib
);
169 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IHost
, &host_ti
);
171 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IArguments2
, &arguments_ti
);
173 ITypeLib_Release(typelib
);
174 return SUCCEEDED(hres
);
177 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
179 WCHAR fileid
[64], progid
[64];
185 static const WCHAR script_engineW
[] =
186 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
188 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
189 if(res
!= ERROR_SUCCESS
)
192 size
= sizeof(fileid
)/sizeof(WCHAR
);
193 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
195 if(res
!= ERROR_SUCCESS
)
198 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
200 strcatW(fileid
, script_engineW
);
201 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
202 if(res
!= ERROR_SUCCESS
)
205 size
= sizeof(progid
)/sizeof(WCHAR
);
206 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
208 if(res
!= ERROR_SUCCESS
)
211 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
213 hres
= CLSIDFromProgID(progid
, clsid
);
214 return SUCCEEDED(hres
);
217 static HRESULT
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
218 IActiveScriptParse
**parser
)
220 IActiveScript
*script
;
224 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
225 &IID_IUnknown
, (void**)&unk
);
229 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
230 IUnknown_Release(unk
);
234 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
236 IActiveScript_Release(script
);
240 *script_ret
= script
;
244 static HRESULT
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
251 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
255 hres
= IActiveScriptParse64_InitNew(parser
);
259 hres
= IActiveScript_AddNamedItem(script
, wscriptW
, SCRIPTITEM_ISVISIBLE
);
263 hres
= IActiveScript_AddNamedItem(script
, wshW
, SCRIPTITEM_ISVISIBLE
);
267 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
268 return SUCCEEDED(hres
);
271 static BSTR
get_script_str(const WCHAR
*filename
)
273 const char *file_map
;
278 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
279 if(file
== INVALID_HANDLE_VALUE
)
282 size
= GetFileSize(file
, NULL
);
283 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
285 if(map
== INVALID_HANDLE_VALUE
)
288 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
293 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
294 ret
= SysAllocStringLen(NULL
, len
);
295 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
297 UnmapViewOfFile(file_map
);
301 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
306 text
= get_script_str(filename
);
308 WINE_FIXME("Could not get script text\n");
312 hres
= IActiveScriptParse64_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
313 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
316 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
320 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
322 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
325 static BOOL
set_host_properties(const WCHAR
*prop
)
327 static const WCHAR iactive
[] = {'i',0};
328 static const WCHAR batch
[] = {'b',0};
338 if(strcmpiW(prop
, iactive
) == 0)
339 wshInteractive
= VARIANT_TRUE
;
340 else if(strcmpiW(prop
, batch
) == 0)
341 wshInteractive
= VARIANT_FALSE
;
347 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
349 const WCHAR
*ext
, *filename
= NULL
;
350 IActiveScriptParse
*parser
;
351 IActiveScript
*script
;
357 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
359 argv
= CommandLineToArgvW(cmdline
, &argc
);
363 for(i
=0; i
<argc
; i
++) {
364 if(*argv
[i
] == '/' || *argv
[i
] == '-') {
365 if(!set_host_properties(argv
[i
]))
370 numOfArgs
= argc
-i
-1;
376 WINE_FIXME("No file name specified\n");
379 res
= GetFullPathNameW(filename
, sizeof(scriptFullName
)/sizeof(WCHAR
), scriptFullName
, NULL
);
380 if(!res
|| res
> sizeof(scriptFullName
)/sizeof(WCHAR
))
383 ext
= strchrW(filename
, '.');
386 if(!get_engine_clsid(ext
, &clsid
)) {
387 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext
));
393 if(!create_engine(&clsid
, &script
, &parser
)) {
394 WINE_FIXME("Could not create script engine\n");
399 if(init_engine(script
, parser
)) {
400 run_script(filename
, script
, parser
);
401 IActiveScript_Close(script
);
402 ITypeInfo_Release(host_ti
);
404 WINE_FIXME("Script initialization failed\n");
407 IActiveScript_Release(script
);
408 IUnknown_Release(parser
);