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];
43 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
44 REFIID riid, void **ppv)
46 if(IsEqualGUID(riid, &IID_IUnknown)) {
47 WINE_TRACE("(IID_IUnknown %p)\n", ppv);
49 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
50 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv);
54 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
58 IUnknown_AddRef((IUnknown*)*ppv);
62 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
67 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
72 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
76 *plcid = GetUserDefaultLCID();
80 static HRESULT WINAPI ActiveScriptSite_GetItemInfo(IActiveScriptSite *iface,
81 LPCOLESTR pstrName, DWORD dwReturnMask, IUnknown **ppunkItem, ITypeInfo **ppti)
83 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName), dwReturnMask, ppunkItem, ppti);
85 if(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
88 if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
89 ITypeInfo_AddRef(host_ti);
93 if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
94 IHost_AddRef(&host_obj);
95 *ppunkItem = (IUnknown*)&host_obj;
101 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
108 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
109 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
115 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
116 SCRIPTSTATE ssScriptState)
118 WINE_TRACE("(%x)\n", ssScriptState);
122 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
123 IActiveScriptError *pscripterror)
129 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
135 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
141 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl = {
142 ActiveScriptSite_QueryInterface,
143 ActiveScriptSite_AddRef,
144 ActiveScriptSite_Release,
145 ActiveScriptSite_GetLCID,
146 ActiveScriptSite_GetItemInfo,
147 ActiveScriptSite_GetDocVersionString,
148 ActiveScriptSite_OnScriptTerminate,
149 ActiveScriptSite_OnStateChange,
150 ActiveScriptSite_OnScriptError,
151 ActiveScriptSite_OnEnterScript,
152 ActiveScriptSite_OnLeaveScript
155 IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
157 static BOOL load_typelib(void)
162 static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
164 hres = LoadTypeLib(wscript_exeW, &typelib);
168 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
170 ITypeLib_Release(typelib);
171 return SUCCEEDED(hres);
174 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
176 WCHAR fileid[64], progid[64];
182 static const WCHAR script_engineW[] =
183 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
185 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
186 if(res != ERROR_SUCCESS)
189 size = sizeof(fileid)/sizeof(WCHAR);
190 res = RegQueryValueW(hkey, NULL, fileid, &size);
192 if(res != ERROR_SUCCESS)
195 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
197 strcatW(fileid, script_engineW);
198 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
199 if(res != ERROR_SUCCESS)
202 size = sizeof(progid)/sizeof(WCHAR);
203 res = RegQueryValueW(hkey, NULL, progid, &size);
205 if(res != ERROR_SUCCESS)
208 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
210 hres = CLSIDFromProgID(progid, clsid);
211 return SUCCEEDED(hres);
214 static HRESULT create_engine(CLSID *clsid, IActiveScript **script_ret,
215 IActiveScriptParse **parser)
217 IActiveScript *script;
221 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
222 &IID_IUnknown, (void**)&unk);
226 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
227 IUnknown_Release(unk);
231 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
233 IActiveScript_Release(script);
237 *script_ret = script;
241 static HRESULT init_engine(IActiveScript *script, IActiveScriptParse *parser)
248 hres = IActiveScript_SetScriptSite(script, &script_site);
252 hres = IActiveScriptParse64_InitNew(parser);
256 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
260 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
264 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
265 return SUCCEEDED(hres);
268 static BSTR get_script_str(const WCHAR *filename)
270 const char *file_map;
275 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
276 if(file == INVALID_HANDLE_VALUE)
279 size = GetFileSize(file, NULL);
280 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
282 if(map == INVALID_HANDLE_VALUE)
285 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
290 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
291 ret = SysAllocStringLen(NULL, len);
292 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
294 UnmapViewOfFile(file_map);
298 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
303 text = get_script_str(filename);
305 WINE_FIXME("Could not get script text\n");
309 hres = IActiveScriptParse64_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
310 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
313 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
317 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
319 WINE_FIXME("SetScriptState failed: %08x\n", hres);
322 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
324 const WCHAR *ext, *filename = NULL;
325 IActiveScriptParse *parser;
326 IActiveScript *script;
332 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
334 argv = CommandLineToArgvW(cmdline, &argc);
338 for(i=0; i<argc; i++) {
339 if(*argv[i] == '/' || *argv[i] == '-') {
340 WINE_FIXME("Unsupported argument %s\n", wine_dbgstr_w(argv[i]));
348 WINE_FIXME("No file name specified\n");
351 res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, NULL);
352 if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
355 ext = strchrW(filename, '.');
358 if(!get_engine_clsid(ext, &clsid)) {
359 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext));
365 if(!create_engine(&clsid, &script, &parser)) {
366 WINE_FIXME("Could not create script engine\n");
371 if(init_engine(script, parser)) {
372 run_script(filename, script, parser);
373 IActiveScript_Close(script);
374 ITypeInfo_Release(host_ti);
376 WINE_FIXME("Script initialization failed\n");
379 IActiveScript_Release(script);
380 IUnknown_Release(parser);