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>
36 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
38 static const WCHAR wscriptW
[] = {'W','S','c','r','i','p','t',0};
39 static const WCHAR wshW
[] = {'W','S','H',0};
40 WCHAR scriptFullName
[MAX_PATH
];
43 ITypeInfo
*arguments_ti
;
45 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
46 REFIID riid
, void **ppv
)
48 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
49 WINE_TRACE("(IID_IUnknown %p)\n", ppv
);
51 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
52 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
56 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
60 IUnknown_AddRef((IUnknown
*)*ppv
);
64 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
69 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
74 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
78 *plcid
= GetUserDefaultLCID();
82 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
,
83 LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
**ppunkItem
, ITypeInfo
**ppti
)
85 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName
), dwReturnMask
, ppunkItem
, ppti
);
87 if(strcmpW(pstrName
, wshW
) && strcmpW(pstrName
, wscriptW
))
90 if(dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
91 ITypeInfo_AddRef(host_ti
);
95 if(dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
96 IHost_AddRef(&host_obj
);
97 *ppunkItem
= (IUnknown
*)&host_obj
;
103 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
,
110 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
111 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
117 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
,
118 SCRIPTSTATE ssScriptState
)
120 WINE_TRACE("(%x)\n", ssScriptState
);
124 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
,
125 IActiveScriptError
*pscripterror
)
131 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
137 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
143 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
144 ActiveScriptSite_QueryInterface
,
145 ActiveScriptSite_AddRef
,
146 ActiveScriptSite_Release
,
147 ActiveScriptSite_GetLCID
,
148 ActiveScriptSite_GetItemInfo
,
149 ActiveScriptSite_GetDocVersionString
,
150 ActiveScriptSite_OnScriptTerminate
,
151 ActiveScriptSite_OnStateChange
,
152 ActiveScriptSite_OnScriptError
,
153 ActiveScriptSite_OnEnterScript
,
154 ActiveScriptSite_OnLeaveScript
157 IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
159 static BOOL
load_typelib(void)
164 static const WCHAR wscript_exeW
[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
166 hres
= LoadTypeLib(wscript_exeW
, &typelib
);
170 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IHost
, &host_ti
);
172 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IArguments2
, &arguments_ti
);
174 ITypeLib_Release(typelib
);
175 return SUCCEEDED(hres
);
178 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
180 WCHAR fileid
[64], progid
[64];
186 static const WCHAR script_engineW
[] =
187 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
189 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
190 if(res
!= ERROR_SUCCESS
)
193 size
= sizeof(fileid
)/sizeof(WCHAR
);
194 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
196 if(res
!= ERROR_SUCCESS
)
199 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
201 strcatW(fileid
, script_engineW
);
202 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
203 if(res
!= ERROR_SUCCESS
)
206 size
= sizeof(progid
)/sizeof(WCHAR
);
207 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
209 if(res
!= ERROR_SUCCESS
)
212 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
214 hres
= CLSIDFromProgID(progid
, clsid
);
215 return SUCCEEDED(hres
);
218 static HRESULT
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
219 IActiveScriptParse
**parser
)
221 IActiveScript
*script
;
225 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
226 &IID_IUnknown
, (void**)&unk
);
230 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
231 IUnknown_Release(unk
);
235 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
237 IActiveScript_Release(script
);
241 *script_ret
= script
;
245 static HRESULT
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
252 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
256 hres
= IActiveScriptParse64_InitNew(parser
);
260 hres
= IActiveScript_AddNamedItem(script
, wscriptW
, SCRIPTITEM_ISVISIBLE
);
264 hres
= IActiveScript_AddNamedItem(script
, wshW
, SCRIPTITEM_ISVISIBLE
);
268 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
269 return SUCCEEDED(hres
);
272 static BSTR
get_script_str(const WCHAR
*filename
)
274 const char *file_map
;
279 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
280 if(file
== INVALID_HANDLE_VALUE
)
283 size
= GetFileSize(file
, NULL
);
284 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
286 if(map
== INVALID_HANDLE_VALUE
)
289 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
294 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
295 ret
= SysAllocStringLen(NULL
, len
);
296 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
298 UnmapViewOfFile(file_map
);
302 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
307 text
= get_script_str(filename
);
309 WINE_FIXME("Could not get script text\n");
313 hres
= IActiveScriptParse64_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
314 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
317 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
321 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
323 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
326 static BOOL
set_host_properties(const WCHAR
*prop
)
328 static const WCHAR iactive
[] = {'i',0};
329 static const WCHAR batch
[] = {'b',0};
339 if(strcmpiW(prop
, iactive
) == 0)
340 wshInteractive
= VARIANT_TRUE
;
341 else if(strcmpiW(prop
, batch
) == 0)
342 wshInteractive
= VARIANT_FALSE
;
348 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
350 const WCHAR
*ext
, *filename
= NULL
;
351 IActiveScriptParse
*parser
;
352 IActiveScript
*script
;
358 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
360 argv
= CommandLineToArgvW(cmdline
, &argc
);
364 for(i
=0; i
<argc
; i
++) {
365 if(*argv
[i
] == '/' || *argv
[i
] == '-') {
366 if(!set_host_properties(argv
[i
]))
371 numOfArgs
= argc
-i
-1;
377 WINE_FIXME("No file name specified\n");
380 res
= GetFullPathNameW(filename
, sizeof(scriptFullName
)/sizeof(WCHAR
), scriptFullName
, NULL
);
381 if(!res
|| res
> sizeof(scriptFullName
)/sizeof(WCHAR
))
384 ext
= strchrW(filename
, '.');
387 if(!get_engine_clsid(ext
, &clsid
)) {
388 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext
));
394 if(!create_engine(&clsid
, &script
, &parser
)) {
395 WINE_FIXME("Could not create script engine\n");
400 if(init_engine(script
, parser
)) {
401 run_script(filename
, script
, parser
);
402 IActiveScript_Close(script
);
403 ITypeInfo_Release(host_ti
);
405 WINE_FIXME("Script initialization failed\n");
408 IActiveScript_Release(script
);
409 IUnknown_Release(parser
);