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 WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
60 REFIID riid
, void **ppv
)
62 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
63 WINE_TRACE("(IID_IUnknown %p)\n", ppv
);
65 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
66 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
70 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
74 IUnknown_AddRef((IUnknown
*)*ppv
);
78 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
83 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
88 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
92 *plcid
= GetUserDefaultLCID();
96 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
,
97 LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
**ppunkItem
, ITypeInfo
**ppti
)
99 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName
), dwReturnMask
, ppunkItem
, ppti
);
101 if(strcmpW(pstrName
, wshW
) && strcmpW(pstrName
, wscriptW
))
104 if(dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
105 ITypeInfo_AddRef(host_ti
);
109 if(dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
110 IHost_AddRef(&host_obj
);
111 *ppunkItem
= (IUnknown
*)&host_obj
;
117 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
,
124 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
125 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
131 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
,
132 SCRIPTSTATE ssScriptState
)
134 WINE_TRACE("(%x)\n", ssScriptState
);
138 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
,
139 IActiveScriptError
*pscripterror
)
145 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
151 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
157 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
158 ActiveScriptSite_QueryInterface
,
159 ActiveScriptSite_AddRef
,
160 ActiveScriptSite_Release
,
161 ActiveScriptSite_GetLCID
,
162 ActiveScriptSite_GetItemInfo
,
163 ActiveScriptSite_GetDocVersionString
,
164 ActiveScriptSite_OnScriptTerminate
,
165 ActiveScriptSite_OnStateChange
,
166 ActiveScriptSite_OnScriptError
,
167 ActiveScriptSite_OnEnterScript
,
168 ActiveScriptSite_OnLeaveScript
171 IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
173 static BOOL
load_typelib(void)
178 static const WCHAR wscript_exeW
[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
180 hres
= LoadTypeLib(wscript_exeW
, &typelib
);
184 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IHost
, &host_ti
);
186 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IArguments2
, &arguments_ti
);
188 ITypeLib_Release(typelib
);
189 return SUCCEEDED(hres
);
192 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
194 WCHAR fileid
[64], progid
[64];
200 static const WCHAR script_engineW
[] =
201 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
203 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
204 if(res
!= ERROR_SUCCESS
)
207 size
= sizeof(fileid
)/sizeof(WCHAR
);
208 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
210 if(res
!= ERROR_SUCCESS
)
213 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
215 strcatW(fileid
, script_engineW
);
216 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
217 if(res
!= ERROR_SUCCESS
)
220 size
= sizeof(progid
)/sizeof(WCHAR
);
221 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
223 if(res
!= ERROR_SUCCESS
)
226 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
228 hres
= CLSIDFromProgID(progid
, clsid
);
229 return SUCCEEDED(hres
);
232 static HRESULT
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
233 IActiveScriptParse
**parser
)
235 IActiveScript
*script
;
239 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
240 &IID_IUnknown
, (void**)&unk
);
244 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
245 IUnknown_Release(unk
);
249 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
251 IActiveScript_Release(script
);
255 *script_ret
= script
;
259 static HRESULT
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
266 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
270 hres
= IActiveScriptParse_InitNew(parser
);
274 hres
= IActiveScript_AddNamedItem(script
, wscriptW
, SCRIPTITEM_ISVISIBLE
);
278 hres
= IActiveScript_AddNamedItem(script
, wshW
, SCRIPTITEM_ISVISIBLE
);
282 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
283 return SUCCEEDED(hres
);
286 static BSTR
get_script_str(const WCHAR
*filename
)
288 const char *file_map
;
293 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
294 if(file
== INVALID_HANDLE_VALUE
)
297 size
= GetFileSize(file
, NULL
);
298 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
300 if(map
== INVALID_HANDLE_VALUE
)
303 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
308 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
309 ret
= SysAllocStringLen(NULL
, len
);
310 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
312 UnmapViewOfFile(file_map
);
316 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
321 text
= get_script_str(filename
);
323 WINE_FIXME("Could not get script text\n");
327 hres
= IActiveScriptParse_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
328 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
331 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
335 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
337 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
340 static BOOL
set_host_properties(const WCHAR
*prop
)
342 static const WCHAR iactive
[] = {'i',0};
343 static const WCHAR batch
[] = {'b',0};
353 if(strcmpiW(prop
, iactive
) == 0)
354 wshInteractive
= VARIANT_TRUE
;
355 else if(strcmpiW(prop
, batch
) == 0)
356 wshInteractive
= VARIANT_FALSE
;
362 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
364 const WCHAR
*ext
, *filename
= NULL
;
365 IActiveScriptParse
*parser
;
366 IActiveScript
*script
;
372 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
374 argv
= CommandLineToArgvW(cmdline
, &argc
);
378 for(i
=0; i
<argc
; i
++) {
379 if(*argv
[i
] == '/' || *argv
[i
] == '-') {
380 if(!set_host_properties(argv
[i
]))
385 numOfArgs
= argc
-i
-1;
391 WINE_FIXME("No file name specified\n");
394 res
= GetFullPathNameW(filename
, sizeof(scriptFullName
)/sizeof(WCHAR
), scriptFullName
, NULL
);
395 if(!res
|| res
> sizeof(scriptFullName
)/sizeof(WCHAR
))
398 ext
= strchrW(filename
, '.');
401 if(!get_engine_clsid(ext
, &clsid
)) {
402 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext
));
408 if(!create_engine(&clsid
, &script
, &parser
)) {
409 WINE_FIXME("Could not create script engine\n");
414 if(init_engine(script
, parser
)) {
415 run_script(filename
, script
, parser
);
416 IActiveScript_Close(script
);
417 ITypeInfo_Release(host_ti
);
419 WINE_FIXME("Script initialization failed\n");
422 IActiveScript_Release(script
);
423 IActiveScriptParse_Release(parser
);