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};
42 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
43 REFIID riid
, void **ppv
)
45 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
46 WINE_TRACE("(IID_IUnknown %p)\n", ppv
);
48 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
49 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
53 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
57 IUnknown_AddRef((IUnknown
*)*ppv
);
61 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
66 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
71 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
75 *plcid
= GetUserDefaultLCID();
79 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
,
80 LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
**ppunkItem
, ITypeInfo
**ppti
)
82 WINE_TRACE("(%s %x %p %p)\n", wine_dbgstr_w(pstrName
), dwReturnMask
, ppunkItem
, ppti
);
84 if(strcmpW(pstrName
, wshW
) && strcmpW(pstrName
, wscriptW
))
87 if(dwReturnMask
& SCRIPTINFO_ITYPEINFO
) {
88 ITypeInfo_AddRef(host_ti
);
92 if(dwReturnMask
& SCRIPTINFO_IUNKNOWN
) {
93 IHost_AddRef(&host_obj
);
94 *ppunkItem
= (IUnknown
*)&host_obj
;
100 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
,
107 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
108 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
114 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
,
115 SCRIPTSTATE ssScriptState
)
117 WINE_TRACE("(%x)\n", ssScriptState
);
121 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
,
122 IActiveScriptError
*pscripterror
)
128 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
134 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
140 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
141 ActiveScriptSite_QueryInterface
,
142 ActiveScriptSite_AddRef
,
143 ActiveScriptSite_Release
,
144 ActiveScriptSite_GetLCID
,
145 ActiveScriptSite_GetItemInfo
,
146 ActiveScriptSite_GetDocVersionString
,
147 ActiveScriptSite_OnScriptTerminate
,
148 ActiveScriptSite_OnStateChange
,
149 ActiveScriptSite_OnScriptError
,
150 ActiveScriptSite_OnEnterScript
,
151 ActiveScriptSite_OnLeaveScript
154 IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
156 static BOOL
load_typelib(void)
161 static const WCHAR wscript_exeW
[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
163 hres
= LoadTypeLib(wscript_exeW
, &typelib
);
167 hres
= ITypeLib_GetTypeInfoOfGuid(typelib
, &IID_IHost
, &host_ti
);
169 ITypeLib_Release(typelib
);
170 return SUCCEEDED(hres
);
173 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
175 WCHAR fileid
[64], progid
[64];
181 static const WCHAR script_engineW
[] =
182 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
184 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
185 if(res
!= ERROR_SUCCESS
)
188 size
= sizeof(fileid
)/sizeof(WCHAR
);
189 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
191 if(res
!= ERROR_SUCCESS
)
194 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
196 strcatW(fileid
, script_engineW
);
197 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
198 if(res
!= ERROR_SUCCESS
)
201 size
= sizeof(progid
)/sizeof(WCHAR
);
202 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
204 if(res
!= ERROR_SUCCESS
)
207 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
209 hres
= CLSIDFromProgID(progid
, clsid
);
210 return SUCCEEDED(hres
);
213 static HRESULT
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
214 IActiveScriptParse
**parser
)
216 IActiveScript
*script
;
220 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
221 &IID_IUnknown
, (void**)&unk
);
225 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
226 IUnknown_Release(unk
);
230 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
232 IActiveScript_Release(script
);
236 *script_ret
= script
;
240 static HRESULT
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
247 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
251 hres
= IActiveScriptParse64_InitNew(parser
);
255 hres
= IActiveScript_AddNamedItem(script
, wscriptW
, SCRIPTITEM_ISVISIBLE
);
259 hres
= IActiveScript_AddNamedItem(script
, wshW
, SCRIPTITEM_ISVISIBLE
);
263 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
264 return SUCCEEDED(hres
);
267 static BSTR
get_script_str(const WCHAR
*filename
)
269 const char *file_map
;
274 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
275 if(file
== INVALID_HANDLE_VALUE
)
278 size
= GetFileSize(file
, NULL
);
279 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
281 if(map
== INVALID_HANDLE_VALUE
)
284 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
289 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
290 ret
= SysAllocStringLen(NULL
, len
);
291 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
293 UnmapViewOfFile(file_map
);
297 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
302 text
= get_script_str(filename
);
304 WINE_FIXME("Could not get script text\n");
308 hres
= IActiveScriptParse64_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
309 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
312 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
316 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
318 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
321 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
323 const WCHAR
*ext
, *filename
= NULL
;
324 IActiveScriptParse
*parser
;
325 IActiveScript
*script
;
330 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
332 argv
= CommandLineToArgvW(cmdline
, &argc
);
336 for(i
=0; i
<argc
; i
++) {
337 if(*argv
[i
] == '/' || *argv
[i
] == '-') {
338 WINE_FIXME("Unsupported argument %s\n", wine_dbgstr_w(argv
[i
]));
346 WINE_FIXME("No file name specified\n");
350 ext
= strchrW(filename
, '.');
353 if(!get_engine_clsid(ext
, &clsid
)) {
354 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext
));
360 if(!create_engine(&clsid
, &script
, &parser
)) {
361 WINE_FIXME("Could not create script engine\n");
366 if(init_engine(script
, parser
)) {
367 run_script(filename
, script
, parser
);
368 IActiveScript_Close(script
);
369 ITypeInfo_Release(host_ti
);
371 WINE_FIXME("Script initialization failed\n");
374 IActiveScript_Release(script
);
375 IUnknown_Release(parser
);