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
33 #include <wine/debug.h>
34 #include <wine/unicode.h>
38 #define IActiveScriptParse_Release IActiveScriptParse64_Release
39 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
40 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
44 #define IActiveScriptParse_Release IActiveScriptParse32_Release
45 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
46 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
50 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
52 static const WCHAR wscriptW
[] = {'W','S','c','r','i','p','t',0};
53 static const WCHAR wshW
[] = {'W','S','H',0};
54 WCHAR scriptFullName
[MAX_PATH
];
57 ITypeInfo
*arguments_ti
;
59 static HRESULT
query_interface(REFIID
,void**);
61 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
62 REFIID riid
, void **ppv
)
64 return query_interface(riid
, ppv
);
67 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
72 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
77 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
81 *plcid
= GetUserDefaultLCID();
85 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
,
86 LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
**ppunkItem
, ITypeInfo
**ppti
)
88 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName
), dwReturnMask
, ppunkItem
, ppti
);
90 if(strcmpW(pstrName
, wshW
) && strcmpW(pstrName
, wscriptW
))
93 if(dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
94 ITypeInfo_AddRef(host_ti
);
98 if(dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
99 IHost_AddRef(&host_obj
);
100 *ppunkItem
= (IUnknown
*)&host_obj
;
106 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
,
113 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
114 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
120 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
,
121 SCRIPTSTATE ssScriptState
)
123 WINE_TRACE("(%x)\n", ssScriptState
);
127 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
,
128 IActiveScriptError
*pscripterror
)
134 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
140 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
146 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
147 ActiveScriptSite_QueryInterface
,
148 ActiveScriptSite_AddRef
,
149 ActiveScriptSite_Release
,
150 ActiveScriptSite_GetLCID
,
151 ActiveScriptSite_GetItemInfo
,
152 ActiveScriptSite_GetDocVersionString
,
153 ActiveScriptSite_OnScriptTerminate
,
154 ActiveScriptSite_OnStateChange
,
155 ActiveScriptSite_OnScriptError
,
156 ActiveScriptSite_OnEnterScript
,
157 ActiveScriptSite_OnLeaveScript
160 static IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
162 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
, REFIID riid
, void **ppv
)
164 return query_interface(riid
, ppv
);
167 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
172 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
177 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
179 TRACE("(%p)\n", phwnd
);
185 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
187 TRACE("(%x)\n", fEnable
);
191 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
192 ActiveScriptSiteWindow_QueryInterface
,
193 ActiveScriptSiteWindow_AddRef
,
194 ActiveScriptSiteWindow_Release
,
195 ActiveScriptSiteWindow_GetWindow
,
196 ActiveScriptSiteWindow_EnableModeless
199 static IActiveScriptSiteWindow script_site_window
= { &ActiveScriptSiteWindowVtbl
};
201 static HRESULT
query_interface(REFIID riid
, void **ppv
)
203 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
204 TRACE("(IID_IUnknown %p)\n", ppv
);
206 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
207 TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
209 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSiteWindow
)) {
210 TRACE("(IID_IActiveScriptSiteWindow %p)\n", ppv
);
211 *ppv
= &script_site_window
;
214 TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
215 return E_NOINTERFACE
;
218 IUnknown_AddRef((IUnknown
*)*ppv
);
222 static BOOL
load_typelib(void)
227 static const WCHAR wscript_exeW
[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
229 hres
= LoadTypeLib(wscript_exeW
, &typelib
);
233 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IHost
, &host_ti
);
235 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IArguments2
, &arguments_ti
);
237 ITypeLib_Release(typelib
);
238 return SUCCEEDED(hres
);
241 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
243 WCHAR fileid
[64], progid
[64];
249 static const WCHAR script_engineW
[] =
250 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
252 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
253 if(res
!= ERROR_SUCCESS
)
256 size
= sizeof(fileid
)/sizeof(WCHAR
);
257 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
259 if(res
!= ERROR_SUCCESS
)
262 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
264 strcatW(fileid
, script_engineW
);
265 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
266 if(res
!= ERROR_SUCCESS
)
269 size
= sizeof(progid
)/sizeof(WCHAR
);
270 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
272 if(res
!= ERROR_SUCCESS
)
275 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
277 hres
= CLSIDFromProgID(progid
, clsid
);
278 return SUCCEEDED(hres
);
281 static BOOL
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
282 IActiveScriptParse
**parser
)
284 IActiveScript
*script
;
288 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
289 &IID_IUnknown
, (void**)&unk
);
293 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
294 IUnknown_Release(unk
);
298 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
300 IActiveScript_Release(script
);
304 *script_ret
= script
;
308 static BOOL
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
315 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
319 hres
= IActiveScriptParse_InitNew(parser
);
323 hres
= IActiveScript_AddNamedItem(script
, wscriptW
, SCRIPTITEM_ISVISIBLE
);
327 hres
= IActiveScript_AddNamedItem(script
, wshW
, SCRIPTITEM_ISVISIBLE
);
331 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
332 return SUCCEEDED(hres
);
335 static BSTR
get_script_str(const WCHAR
*filename
)
337 const char *file_map
;
342 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
343 if(file
== INVALID_HANDLE_VALUE
)
346 size
= GetFileSize(file
, NULL
);
347 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
349 if(map
== INVALID_HANDLE_VALUE
)
352 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
357 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
358 ret
= SysAllocStringLen(NULL
, len
);
359 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
361 UnmapViewOfFile(file_map
);
365 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
370 text
= get_script_str(filename
);
372 WINE_FIXME("Could not get script text\n");
376 hres
= IActiveScriptParse_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
377 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
380 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
384 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
386 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
389 static BOOL
set_host_properties(const WCHAR
*prop
)
391 static const WCHAR nologoW
[] = {'n','o','l','o','g','o',0};
392 static const WCHAR iactive
[] = {'i',0};
393 static const WCHAR batch
[] = {'b',0};
403 if(strcmpiW(prop
, iactive
) == 0)
404 wshInteractive
= VARIANT_TRUE
;
405 else if(strcmpiW(prop
, batch
) == 0)
406 wshInteractive
= VARIANT_FALSE
;
407 else if(strcmpiW(prop
, nologoW
) == 0)
408 WINE_FIXME("ignored %s switch\n", debugstr_w(nologoW
));
411 WINE_FIXME("unsupported switch %s\n", debugstr_w(prop
));
417 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
419 WCHAR
*ext
, *filepart
, *filename
= NULL
;
420 IActiveScriptParse
*parser
;
421 IActiveScript
*script
;
427 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
429 argv
= CommandLineToArgvW(cmdline
, &argc
);
433 for(i
=0; i
<argc
; i
++) {
434 if(*argv
[i
] == '/' || *argv
[i
] == '-') {
435 if(!set_host_properties(argv
[i
]))
440 numOfArgs
= argc
-i
-1;
446 WINE_FIXME("No file name specified\n");
449 res
= GetFullPathNameW(filename
, sizeof(scriptFullName
)/sizeof(WCHAR
), scriptFullName
, &filepart
);
450 if(!res
|| res
> sizeof(scriptFullName
)/sizeof(WCHAR
))
453 ext
= strrchrW(filepart
, '.');
454 if(!ext
|| !get_engine_clsid(ext
, &clsid
)) {
455 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext
));
461 if(!create_engine(&clsid
, &script
, &parser
)) {
462 WINE_FIXME("Could not create script engine\n");
467 if(init_engine(script
, parser
)) {
468 run_script(filename
, script
, parser
);
469 IActiveScript_Close(script
);
470 ITypeInfo_Release(host_ti
);
472 WINE_FIXME("Script initialization failed\n");
475 IActiveScript_Release(script
);
476 IActiveScriptParse_Release(parser
);