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
29 #include <wine/debug.h>
30 #include <wine/unicode.h>
32 WINE_DEFAULT_DEBUG_CHANNEL(wscript
);
34 static HRESULT WINAPI
ActiveScriptSite_QueryInterface(IActiveScriptSite
*iface
,
35 REFIID riid
, void **ppv
)
37 if(IsEqualGUID(riid
, &IID_IUnknown
)) {
38 WINE_TRACE("(IID_IUnknown %p)\n", ppv
);
40 }else if(IsEqualGUID(riid
, &IID_IActiveScriptSite
)) {
41 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv
);
45 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid
), ppv
);
49 IUnknown_AddRef((IUnknown
*)*ppv
);
53 static ULONG WINAPI
ActiveScriptSite_AddRef(IActiveScriptSite
*iface
)
58 static ULONG WINAPI
ActiveScriptSite_Release(IActiveScriptSite
*iface
)
63 static HRESULT WINAPI
ActiveScriptSite_GetLCID(IActiveScriptSite
*iface
, LCID
*plcid
)
67 *plcid
= GetUserDefaultLCID();
71 static HRESULT WINAPI
ActiveScriptSite_GetItemInfo(IActiveScriptSite
*iface
,
72 LPCOLESTR pstrName
, DWORD dwReturnMask
, IUnknown
**ppunkItem
, ITypeInfo
**ppti
)
74 WINE_FIXME("(%s %x %p %p)\n", wine_dbgstr_w(pstrName
), dwReturnMask
, ppunkItem
, ppti
);
78 static HRESULT WINAPI
ActiveScriptSite_GetDocVersionString(IActiveScriptSite
*iface
,
85 static HRESULT WINAPI
ActiveScriptSite_OnScriptTerminate(IActiveScriptSite
*iface
,
86 const VARIANT
*pvarResult
, const EXCEPINFO
*pexcepinfo
)
92 static HRESULT WINAPI
ActiveScriptSite_OnStateChange(IActiveScriptSite
*iface
,
93 SCRIPTSTATE ssScriptState
)
95 WINE_TRACE("(%x)\n", ssScriptState
);
99 static HRESULT WINAPI
ActiveScriptSite_OnScriptError(IActiveScriptSite
*iface
,
100 IActiveScriptError
*pscripterror
)
106 static HRESULT WINAPI
ActiveScriptSite_OnEnterScript(IActiveScriptSite
*iface
)
112 static HRESULT WINAPI
ActiveScriptSite_OnLeaveScript(IActiveScriptSite
*iface
)
118 static IActiveScriptSiteVtbl ActiveScriptSiteVtbl
= {
119 ActiveScriptSite_QueryInterface
,
120 ActiveScriptSite_AddRef
,
121 ActiveScriptSite_Release
,
122 ActiveScriptSite_GetLCID
,
123 ActiveScriptSite_GetItemInfo
,
124 ActiveScriptSite_GetDocVersionString
,
125 ActiveScriptSite_OnScriptTerminate
,
126 ActiveScriptSite_OnStateChange
,
127 ActiveScriptSite_OnScriptError
,
128 ActiveScriptSite_OnEnterScript
,
129 ActiveScriptSite_OnLeaveScript
132 IActiveScriptSite script_site
= { &ActiveScriptSiteVtbl
};
134 static BOOL
get_engine_clsid(const WCHAR
*ext
, CLSID
*clsid
)
136 WCHAR fileid
[64], progid
[64];
142 static const WCHAR script_engineW
[] =
143 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
145 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, ext
, &hkey
);
146 if(res
!= ERROR_SUCCESS
)
149 size
= sizeof(fileid
)/sizeof(WCHAR
);
150 res
= RegQueryValueW(hkey
, NULL
, fileid
, &size
);
152 if(res
!= ERROR_SUCCESS
)
155 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid
));
157 strcatW(fileid
, script_engineW
);
158 res
= RegOpenKeyW(HKEY_CLASSES_ROOT
, fileid
, &hkey
);
159 if(res
!= ERROR_SUCCESS
)
162 size
= sizeof(progid
)/sizeof(WCHAR
);
163 res
= RegQueryValueW(hkey
, NULL
, progid
, &size
);
165 if(res
!= ERROR_SUCCESS
)
168 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid
));
170 hres
= CLSIDFromProgID(progid
, clsid
);
171 return SUCCEEDED(hres
);
174 static HRESULT
create_engine(CLSID
*clsid
, IActiveScript
**script_ret
,
175 IActiveScriptParse
**parser
)
177 IActiveScript
*script
;
181 hres
= CoCreateInstance(clsid
, NULL
, CLSCTX_INPROC_SERVER
|CLSCTX_INPROC_HANDLER
,
182 &IID_IUnknown
, (void**)&unk
);
186 hres
= IUnknown_QueryInterface(unk
, &IID_IActiveScript
, (void**)&script
);
187 IUnknown_Release(unk
);
191 hres
= IActiveScript_QueryInterface(script
, &IID_IActiveScriptParse
, (void**)parser
);
193 IActiveScript_Release(script
);
197 *script_ret
= script
;
201 static HRESULT
init_engine(IActiveScript
*script
, IActiveScriptParse
*parser
)
205 static const WCHAR wscriptW
[] = {'W','S','c','r','i','p','t',0};
206 static const WCHAR wshW
[] = {'W','S','H',0};
208 hres
= IActiveScript_SetScriptSite(script
, &script_site
);
212 hres
= IActiveScriptParse64_InitNew(parser
);
216 hres
= IActiveScript_AddNamedItem(script
, wscriptW
, SCRIPTITEM_ISVISIBLE
);
220 hres
= IActiveScript_AddNamedItem(script
, wshW
, SCRIPTITEM_ISVISIBLE
);
224 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_INITIALIZED
);
225 return SUCCEEDED(hres
);
228 static BSTR
get_script_str(const WCHAR
*filename
)
230 const char *file_map
;
235 file
= CreateFileW(filename
, GENERIC_READ
, 0, NULL
, OPEN_EXISTING
, FILE_ATTRIBUTE_READONLY
, NULL
);
236 if(file
== INVALID_HANDLE_VALUE
)
239 size
= GetFileSize(file
, NULL
);
240 map
= CreateFileMappingW(file
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
242 if(map
== INVALID_HANDLE_VALUE
)
245 file_map
= MapViewOfFile(map
, FILE_MAP_READ
, 0, 0, 0);
250 len
= MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, NULL
, 0);
251 ret
= SysAllocStringLen(NULL
, len
);
252 MultiByteToWideChar(CP_ACP
, 0, file_map
, size
, ret
, len
);
254 UnmapViewOfFile(file_map
);
258 static void run_script(const WCHAR
*filename
, IActiveScript
*script
, IActiveScriptParse
*parser
)
263 text
= get_script_str(filename
);
265 WINE_FIXME("Could not get script text\n");
269 hres
= IActiveScriptParse64_ParseScriptText(parser
, text
, NULL
, NULL
, NULL
, 1, 1,
270 SCRIPTTEXT_HOSTMANAGESSOURCE
|SCRIPTITEM_ISVISIBLE
, NULL
, NULL
);
273 WINE_FIXME("ParseScriptText failed: %08x\n", hres
);
277 hres
= IActiveScript_SetScriptState(script
, SCRIPTSTATE_STARTED
);
279 WINE_FIXME("SetScriptState failed: %08x\n", hres
);
282 int WINAPI
wWinMain(HINSTANCE hInst
, HINSTANCE hPrevInst
, LPWSTR cmdline
, int cmdshow
)
284 IActiveScriptParse
*parser
;
285 IActiveScript
*script
;
289 WINE_TRACE("(%p %p %s %x)\n", hInst
, hPrevInst
, wine_dbgstr_w(cmdline
), cmdshow
);
294 ext
= strchrW(cmdline
, '.');
297 if(!get_engine_clsid(ext
, &clsid
)) {
298 WINE_FIXME("Could not fine engine for %s\n", wine_dbgstr_w(ext
));
304 if(!create_engine(&clsid
, &script
, &parser
)) {
305 WINE_FIXME("Could not create script engine\n");
310 if(init_engine(script
, parser
)) {
311 run_script(cmdline
, script
, parser
);
312 IActiveScript_Close(script
);
314 WINE_FIXME("Script initialization failed\n");
317 IActiveScript_Release(script
);
318 IUnknown_Release(parser
);