comctl32: Store style bits provided with WM_CREATE.
[wine/multimedia.git] / programs / wscript / main.c
blob9d79415e49034105616ca652959bc3090b0425ad
1 /*
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
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include <windef.h>
24 #include <winbase.h>
25 #include <winreg.h>
26 #include <ole2.h>
27 #include <activscp.h>
28 #include <initguid.h>
30 #include "wscript.h"
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};
39 WCHAR scriptFullName[MAX_PATH];
41 ITypeInfo *host_ti;
42 ITypeInfo *arguments_ti;
44 static HRESULT WINAPI ActiveScriptSite_QueryInterface(IActiveScriptSite *iface,
45 REFIID riid, void **ppv)
47 if(IsEqualGUID(riid, &IID_IUnknown)) {
48 WINE_TRACE("(IID_IUnknown %p)\n", ppv);
49 *ppv = iface;
50 }else if(IsEqualGUID(riid, &IID_IActiveScriptSite)) {
51 WINE_TRACE("(IID_IActiveScriptSite %p)\n", ppv);
52 *ppv = iface;
53 }else {
54 *ppv = NULL;
55 WINE_TRACE("(%s %p)\n", wine_dbgstr_guid(riid), ppv);
56 return E_NOINTERFACE;
59 IUnknown_AddRef((IUnknown*)*ppv);
60 return S_OK;
63 static ULONG WINAPI ActiveScriptSite_AddRef(IActiveScriptSite *iface)
65 return 2;
68 static ULONG WINAPI ActiveScriptSite_Release(IActiveScriptSite *iface)
70 return 1;
73 static HRESULT WINAPI ActiveScriptSite_GetLCID(IActiveScriptSite *iface, LCID *plcid)
75 WINE_TRACE("()\n");
77 *plcid = GetUserDefaultLCID();
78 return S_OK;
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(strcmpW(pstrName, wshW) && strcmpW(pstrName, wscriptW))
87 return E_FAIL;
89 if(dwReturnMask & SCRIPTINFO_ITYPEINFO) {
90 ITypeInfo_AddRef(host_ti);
91 *ppti = host_ti;
94 if(dwReturnMask & SCRIPTINFO_IUNKNOWN) {
95 IHost_AddRef(&host_obj);
96 *ppunkItem = (IUnknown*)&host_obj;
99 return S_OK;
102 static HRESULT WINAPI ActiveScriptSite_GetDocVersionString(IActiveScriptSite *iface,
103 BSTR *pbstrVersion)
105 WINE_FIXME("()\n");
106 return E_NOTIMPL;
109 static HRESULT WINAPI ActiveScriptSite_OnScriptTerminate(IActiveScriptSite *iface,
110 const VARIANT *pvarResult, const EXCEPINFO *pexcepinfo)
112 WINE_FIXME("()\n");
113 return E_NOTIMPL;
116 static HRESULT WINAPI ActiveScriptSite_OnStateChange(IActiveScriptSite *iface,
117 SCRIPTSTATE ssScriptState)
119 WINE_TRACE("(%x)\n", ssScriptState);
120 return S_OK;
123 static HRESULT WINAPI ActiveScriptSite_OnScriptError(IActiveScriptSite *iface,
124 IActiveScriptError *pscripterror)
126 WINE_FIXME("()\n");
127 return E_NOTIMPL;
130 static HRESULT WINAPI ActiveScriptSite_OnEnterScript(IActiveScriptSite *iface)
132 WINE_TRACE("()\n");
133 return S_OK;
136 static HRESULT WINAPI ActiveScriptSite_OnLeaveScript(IActiveScriptSite *iface)
138 WINE_TRACE("()\n");
139 return S_OK;
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 IActiveScriptSite script_site = { &ActiveScriptSiteVtbl };
158 static BOOL load_typelib(void)
160 ITypeLib *typelib;
161 HRESULT hres;
163 static const WCHAR wscript_exeW[] = {'w','s','c','r','i','p','t','.','e','x','e',0};
165 hres = LoadTypeLib(wscript_exeW, &typelib);
166 if(FAILED(hres))
167 return FALSE;
169 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IHost, &host_ti);
170 if(SUCCEEDED(hres))
171 hres = ITypeLib_GetTypeInfoOfGuid(typelib, &IID_IArguments2, &arguments_ti);
173 ITypeLib_Release(typelib);
174 return SUCCEEDED(hres);
177 static BOOL get_engine_clsid(const WCHAR *ext, CLSID *clsid)
179 WCHAR fileid[64], progid[64];
180 DWORD res;
181 LONG size;
182 HKEY hkey;
183 HRESULT hres;
185 static const WCHAR script_engineW[] =
186 {'\\','S','c','r','i','p','t','E','n','g','i','n','e',0};
188 res = RegOpenKeyW(HKEY_CLASSES_ROOT, ext, &hkey);
189 if(res != ERROR_SUCCESS)
190 return FALSE;
192 size = sizeof(fileid)/sizeof(WCHAR);
193 res = RegQueryValueW(hkey, NULL, fileid, &size);
194 RegCloseKey(hkey);
195 if(res != ERROR_SUCCESS)
196 return FALSE;
198 WINE_TRACE("fileid is %s\n", wine_dbgstr_w(fileid));
200 strcatW(fileid, script_engineW);
201 res = RegOpenKeyW(HKEY_CLASSES_ROOT, fileid, &hkey);
202 if(res != ERROR_SUCCESS)
203 return FALSE;
205 size = sizeof(progid)/sizeof(WCHAR);
206 res = RegQueryValueW(hkey, NULL, progid, &size);
207 RegCloseKey(hkey);
208 if(res != ERROR_SUCCESS)
209 return FALSE;
211 WINE_TRACE("ProgID is %s\n", wine_dbgstr_w(progid));
213 hres = CLSIDFromProgID(progid, clsid);
214 return SUCCEEDED(hres);
217 static HRESULT create_engine(CLSID *clsid, IActiveScript **script_ret,
218 IActiveScriptParse **parser)
220 IActiveScript *script;
221 IUnknown *unk;
222 HRESULT hres;
224 hres = CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
225 &IID_IUnknown, (void**)&unk);
226 if(FAILED(hres))
227 return FALSE;
229 hres = IUnknown_QueryInterface(unk, &IID_IActiveScript, (void**)&script);
230 IUnknown_Release(unk);
231 if(FAILED(hres))
232 return FALSE;
234 hres = IActiveScript_QueryInterface(script, &IID_IActiveScriptParse, (void**)parser);
235 if(FAILED(hres)) {
236 IActiveScript_Release(script);
237 return FALSE;
240 *script_ret = script;
241 return TRUE;
244 static HRESULT init_engine(IActiveScript *script, IActiveScriptParse *parser)
246 HRESULT hres;
248 if(!load_typelib())
249 return FALSE;
251 hres = IActiveScript_SetScriptSite(script, &script_site);
252 if(FAILED(hres))
253 return FALSE;
255 hres = IActiveScriptParse64_InitNew(parser);
256 if(FAILED(hres))
257 return FALSE;
259 hres = IActiveScript_AddNamedItem(script, wscriptW, SCRIPTITEM_ISVISIBLE);
260 if(FAILED(hres))
261 return FALSE;
263 hres = IActiveScript_AddNamedItem(script, wshW, SCRIPTITEM_ISVISIBLE);
264 if(FAILED(hres))
265 return FALSE;
267 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_INITIALIZED);
268 return SUCCEEDED(hres);
271 static BSTR get_script_str(const WCHAR *filename)
273 const char *file_map;
274 HANDLE file, map;
275 DWORD size, len;
276 BSTR ret;
278 file = CreateFileW(filename, GENERIC_READ, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_READONLY, NULL);
279 if(file == INVALID_HANDLE_VALUE)
280 return NULL;
282 size = GetFileSize(file, NULL);
283 map = CreateFileMappingW(file, NULL, PAGE_READONLY, 0, 0, NULL);
284 CloseHandle(file);
285 if(map == INVALID_HANDLE_VALUE)
286 return NULL;
288 file_map = MapViewOfFile(map, FILE_MAP_READ, 0, 0, 0);
289 CloseHandle(map);
290 if(!file_map)
291 return NULL;
293 len = MultiByteToWideChar(CP_ACP, 0, file_map, size, NULL, 0);
294 ret = SysAllocStringLen(NULL, len);
295 MultiByteToWideChar(CP_ACP, 0, file_map, size, ret, len);
297 UnmapViewOfFile(file_map);
298 return ret;
301 static void run_script(const WCHAR *filename, IActiveScript *script, IActiveScriptParse *parser)
303 BSTR text;
304 HRESULT hres;
306 text = get_script_str(filename);
307 if(!text) {
308 WINE_FIXME("Could not get script text\n");
309 return;
312 hres = IActiveScriptParse64_ParseScriptText(parser, text, NULL, NULL, NULL, 1, 1,
313 SCRIPTTEXT_HOSTMANAGESSOURCE|SCRIPTITEM_ISVISIBLE, NULL, NULL);
314 SysFreeString(text);
315 if(FAILED(hres)) {
316 WINE_FIXME("ParseScriptText failed: %08x\n", hres);
317 return;
320 hres = IActiveScript_SetScriptState(script, SCRIPTSTATE_STARTED);
321 if(FAILED(hres))
322 WINE_FIXME("SetScriptState failed: %08x\n", hres);
325 static BOOL set_host_properties(const WCHAR *prop)
327 static const WCHAR iactive[] = {'i',0};
328 static const WCHAR batch[] = {'b',0};
330 if(*prop == '/') {
331 ++prop;
332 if(*prop == '/')
333 ++prop;
335 else
336 ++prop;
338 if(strcmpiW(prop, iactive) == 0)
339 wshInteractive = VARIANT_TRUE;
340 else if(strcmpiW(prop, batch) == 0)
341 wshInteractive = VARIANT_FALSE;
342 else
343 return FALSE;
344 return TRUE;
347 int WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR cmdline, int cmdshow)
349 const WCHAR *ext, *filename = NULL;
350 IActiveScriptParse *parser;
351 IActiveScript *script;
352 WCHAR **argv;
353 CLSID clsid;
354 int argc, i;
355 DWORD res;
357 WINE_TRACE("(%p %p %s %x)\n", hInst, hPrevInst, wine_dbgstr_w(cmdline), cmdshow);
359 argv = CommandLineToArgvW(cmdline, &argc);
360 if(!argv)
361 return 1;
363 for(i=0; i<argc; i++) {
364 if(*argv[i] == '/' || *argv[i] == '-') {
365 if(!set_host_properties(argv[i]))
366 return 1;
367 }else {
368 filename = argv[i];
369 argums = argv+i+1;
370 numOfArgs = argc-i-1;
371 break;
375 if(!filename) {
376 WINE_FIXME("No file name specified\n");
377 return 1;
379 res = GetFullPathNameW(filename, sizeof(scriptFullName)/sizeof(WCHAR), scriptFullName, NULL);
380 if(!res || res > sizeof(scriptFullName)/sizeof(WCHAR))
381 return 1;
383 ext = strchrW(filename, '.');
384 if(!ext)
385 ext = filename;
386 if(!get_engine_clsid(ext, &clsid)) {
387 WINE_FIXME("Could not find engine for %s\n", wine_dbgstr_w(ext));
388 return 1;
391 CoInitialize(NULL);
393 if(!create_engine(&clsid, &script, &parser)) {
394 WINE_FIXME("Could not create script engine\n");
395 CoUninitialize();
396 return 1;
399 if(init_engine(script, parser)) {
400 run_script(filename, script, parser);
401 IActiveScript_Close(script);
402 ITypeInfo_Release(host_ti);
403 }else {
404 WINE_FIXME("Script initialization failed\n");
407 IActiveScript_Release(script);
408 IUnknown_Release(parser);
410 CoUninitialize();
412 return 0;