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>
37 #define IActiveScriptParse_Release IActiveScriptParse64_Release
38 #define IActiveScriptParse_InitNew IActiveScriptParse64_InitNew
39 #define IActiveScriptParse_ParseScriptText IActiveScriptParse64_ParseScriptText
43 #define IActiveScriptParse_Release IActiveScriptParse32_Release
44 #define IActiveScriptParse_InitNew IActiveScriptParse32_InitNew
45 #define IActiveScriptParse_ParseScriptText IActiveScriptParse32_ParseScriptText
49 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
50 WCHAR scriptFullName
[MAX_PATH
];
53 ITypeInfo
*arguments_ti
;
55 static HRESULT
query_interface(REFIID
,void**);
57 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
58 REFIID riid
, void **ppv
)
60 return query_interface(riid
, 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(lstrcmpW(pstrName
, L
"WSH") && lstrcmpW(pstrName
, L
"WScript"))
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 static IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
158 static HRESULT WINAPI
ActiveScriptSiteWindow_QueryInterface(IActiveScriptSiteWindow
*iface
, REFIID riid
, void **ppv
)
160 return query_interface(riid
, ppv
);
163 static ULONG WINAPI
ActiveScriptSiteWindow_AddRef(IActiveScriptSiteWindow
*iface
)
168 static ULONG WINAPI
ActiveScriptSiteWindow_Release(IActiveScriptSiteWindow
*iface
)
173 static HRESULT WINAPI
ActiveScriptSiteWindow_GetWindow(IActiveScriptSiteWindow
*iface
, HWND
*phwnd
)
175 TRACE("(%p)\n", phwnd
);
181 static HRESULT WINAPI
ActiveScriptSiteWindow_EnableModeless(IActiveScriptSiteWindow
*iface
, BOOL fEnable
)
183 TRACE("(%x)\n", fEnable
);
187 static const IActiveScriptSiteWindowVtbl ActiveScriptSiteWindowVtbl
= {
188 ActiveScriptSiteWindow_QueryInterface
,
189 ActiveScriptSiteWindow_AddRef
,
190 ActiveScriptSiteWindow_Release
,
191 ActiveScriptSiteWindow_GetWindow
,
192 ActiveScriptSiteWindow_EnableModeless
195 static IActiveScriptSiteWindow script_site_window
= { &ActiveScriptSiteWindowVtbl
};
197 static HRESULT
query_interface(REFIID riid
, void **ppv
)
199 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
200 TRACE("(IID_IUnknown %p)\n", ppv
);
202 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
203 TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
205 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSiteWindow
)) {
206 TRACE("(IID_IActiveScriptSiteWindow %p)\n", ppv
);
207 *ppv
= &script_site_window
;
210 TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
211 return E_NOINTERFACE
;
214 IUnknown_AddRef((IUnknown
*)*ppv
);
218 static BOOL
load_typelib(void)
223 hres
= LoadTypeLib(L
"wscript.exe", &typelib
);
227 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IHost
, &host_ti
);
229 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IArguments2
, &arguments_ti
);
231 ITypeLib_Release(typelib
);
232 return SUCCEEDED(hres
);
235 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
237 WCHAR fileid
[64], progid
[64];
243 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
244 if(res
!= ERROR_SUCCESS
)
247 size
= ARRAY_SIZE(fileid
);
248 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
250 if(res
!= ERROR_SUCCESS
)
253 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
255 lstrcatW(fileid
, L
"\\ScriptEngine");
256 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
257 if(res
!= ERROR_SUCCESS
)
260 size
= ARRAY_SIZE(progid
);
261 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
263 if(res
!= ERROR_SUCCESS
)
266 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
268 hres
= CLSIDFromProgID(progid
, clsid
);
269 return SUCCEEDED(hres
);
272 static BOOL
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
273 IActiveScriptParse
**parser
)
275 IActiveScript
*script
;
279 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
280 &IID_IUnknown
, (void**)&unk
);
284 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
285 IUnknown_Release(unk
);
289 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
291 IActiveScript_Release(script
);
295 *script_ret
= script
;
299 static BOOL
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
306 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
310 hres
= IActiveScriptParse_InitNew(parser
);
314 hres
= IActiveScript_AddNamedItem(script
, L
"WScript", SCRIPTITEM_ISVISIBLE
);
318 hres
= IActiveScript_AddNamedItem(script
, L
"WSH", SCRIPTITEM_ISVISIBLE
);
322 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
323 return SUCCEEDED(hres
);
326 static BSTR
get_script_str(const WCHAR
*filename
)
328 const char *file_map
;
333 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
334 if(file
== INVALID_HANDLE_VALUE
)
337 size
= GetFileSize(file
, NULL
);
338 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
340 if(map
== INVALID_HANDLE_VALUE
)
343 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
348 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
349 ret
= SysAllocStringLen(NULL
, len
);
350 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
352 UnmapViewOfFile(file_map
);
356 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
361 text
= get_script_str(filename
);
363 WINE_FIXME("Could not get script text\n");
367 hres
= IActiveScriptParse_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
368 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
371 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
375 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
377 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
380 static BOOL
set_host_properties(const WCHAR
*prop
)
390 if(wcsicmp(prop
, L
"i") == 0)
391 wshInteractive
= VARIANT_TRUE
;
392 else if(wcsicmp(prop
, L
"b") == 0)
393 wshInteractive
= VARIANT_FALSE
;
394 else if(wcsicmp(prop
, L
"nologo") == 0)
395 WINE_FIXME("ignored %s switch\n", debugstr_w(L
"nologo"));
398 WINE_FIXME("unsupported switch %s\n", debugstr_w(prop
));
404 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
406 WCHAR
*ext
, *filepart
, *filename
= NULL
;
407 IActiveScriptParse
*parser
;
408 IActiveScript
*script
;
414 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
416 argv
= CommandLineToArgvW(GetCommandLineW(), &argc
);
420 for(i
=1; i
<argc
; i
++) {
421 if(*argv
[i
] == '/' || *argv
[i
] == '-') {
422 if(!set_host_properties(argv
[i
]))
427 numOfArgs
= argc
-i
-1;
433 WINE_FIXME("No file name specified\n");
436 res
= GetFullPathNameW(filename
, ARRAY_SIZE(scriptFullName
), scriptFullName
, &filepart
);
437 if(!res
|| res
> ARRAY_SIZE(scriptFullName
))
440 ext
= wcsrchr(filepart
, '.');
441 if(!ext
|| !get_engine_clsid(ext
, &clsid
)) {
442 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext
));
448 if(!create_engine(&clsid
, &script
, &parser
)) {
449 WINE_FIXME("Could not create script engine\n");
454 if(init_engine(script
, parser
)) {
455 run_script(filename
, script
, parser
);
456 IActiveScript_Close(script
);
457 ITypeInfo_Release(host_ti
);
459 WINE_FIXME("Script initialization failed\n");
462 IActiveScript_Release(script
);
463 IActiveScriptParse_Release(parser
);