inhibit/dbus: fix flawed logic
[vlc.git] / bin / winvlc.c
blobd586bff9349aad5d2e815879b1352cbc3026770c
1 /*****************************************************************************
2 * winvlc.c: the Windows VLC media player
3 *****************************************************************************
4 * Copyright (C) 1998-2011 the VideoLAN team
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Samuel Hocevar <sam@zoy.org>
8 * Gildas Bazin <gbazin@videolan.org>
9 * Derk-Jan Hartman <hartman at videolan dot org>
10 * Lots of other people, see the libvlc AUTHORS file
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #ifndef UNICODE
32 #define UNICODE
33 #endif
35 #include <vlc/vlc.h>
36 #include <windows.h>
37 #include <shellapi.h>
39 #ifndef _WIN32_IE
40 # define _WIN32_IE 0x501
41 #endif
42 #include <fcntl.h>
43 #include <io.h>
44 #include <shlobj.h>
45 #include <wininet.h>
46 #define PSAPI_VERSION 1
47 #include <psapi.h>
48 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
49 static void check_crashdump(void);
50 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
51 static const wchar_t *crashdump_path;
53 static char *FromWide (const wchar_t *wide)
55 size_t len;
56 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
58 char *out = (char *)malloc (len);
59 if (out)
60 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
61 return out;
64 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
65 static BOOL SetDefaultDllDirectories_(DWORD flags)
67 HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
68 if (h == NULL)
69 return FALSE;
71 BOOL (WINAPI * SetDefaultDllDirectoriesReal)(DWORD);
73 SetDefaultDllDirectoriesReal = GetProcAddress(h,
74 "SetDefaultDllDirectories");
75 if (SetDefaultDllDirectoriesReal == NULL)
76 return FALSE;
78 return SetDefaultDllDirectoriesReal(flags);
80 # define SetDefaultDllDirectories SetDefaultDllDirectories_
82 #endif
84 static void PrioritizeSystem32(void)
86 #ifndef HAVE_PROCESS_MITIGATION_IMAGE_LOAD_POLICY
87 typedef struct _PROCESS_MITIGATION_IMAGE_LOAD_POLICY {
88 union {
89 DWORD Flags;
90 struct {
91 DWORD NoRemoteImages :1;
92 DWORD NoLowMandatoryLabelImages :1;
93 DWORD PreferSystem32Images :1;
94 DWORD ReservedFlags :29;
97 } PROCESS_MITIGATION_IMAGE_LOAD_POLICY;
98 #endif
99 #if _WIN32_WINNT < _WIN32_WINNT_WIN8
100 BOOL WINAPI (*SetProcessMitigationPolicy)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T);
101 HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
102 if ( !h_Kernel32 )
103 return;
104 SetProcessMitigationPolicy = (BOOL (WINAPI *)(PROCESS_MITIGATION_POLICY, PVOID, SIZE_T))
105 GetProcAddress(h_Kernel32, "SetProcessMitigationPolicy");
106 if (SetProcessMitigationPolicy == NULL)
107 return;
108 #endif
109 PROCESS_MITIGATION_IMAGE_LOAD_POLICY m = { .Flags = 0 };
110 m.PreferSystem32Images = 1;
111 SetProcessMitigationPolicy( 10 /* ProcessImageLoadPolicy */, &m, sizeof( m ) );
114 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
115 LPSTR lpCmdLine,
116 int nCmdShow )
118 int argc;
120 /* VLC does not change the thread locale, so gettext/libintil will use the
121 * user default locale as reference. */
122 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
123 * if the GETTEXT_MUI environment variable is set. If not set or if running
124 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
125 * putenv() call may become redundant with later versions of gettext. */
126 putenv("GETTEXT_MUI=1");
127 #ifdef TOP_BUILDDIR
128 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
129 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
130 #endif
132 SetErrorMode(SEM_FAILCRITICALERRORS);
133 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
135 /* SetProcessDEPPolicy, SetDllDirectory, & Co. */
136 HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
137 if (h_Kernel32 != NULL)
139 /* Enable DEP */
140 # define PROCESS_DEP_ENABLE 1
141 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
142 mySetProcessDEPPolicy = (BOOL (WINAPI *)(DWORD))
143 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
144 if(mySetProcessDEPPolicy)
145 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
147 /* Do NOT load any library from cwd. */
148 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
149 mySetDllDirectoryA = (BOOL (WINAPI *)(const char*))
150 GetProcAddress(h_Kernel32, "SetDllDirectoryA");
151 if(mySetDllDirectoryA)
152 mySetDllDirectoryA("");
155 /***
156 * The LoadLibrary* calls from the modules and the 3rd party code
157 * will search in SYSTEM32 only
158 * */
159 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
160 /***
161 * Load DLLs from system32 before any other folder (when possible)
163 PrioritizeSystem32();
165 /* Args */
166 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
167 if (wargv == NULL)
168 return 1;
170 char *argv[argc + 3];
171 BOOL crash_handling = TRUE;
172 int j = 0;
173 char *lang = NULL;
175 argv[j++] = FromWide( L"--media-library" );
176 argv[j++] = FromWide( L"--no-ignore-config" );
177 for (int i = 1; i < argc; i++)
179 if(!wcscmp(wargv[i], L"--no-crashdump"))
181 crash_handling = FALSE;
182 continue; /* don't give argument to libvlc */
184 if (!wcsncmp(wargv[i], L"--language", 10) )
186 if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
187 lang = FromWide (wargv[++i]);
188 continue;
191 argv[j++] = FromWide (wargv[i]);
194 argc = j;
195 argv[argc] = NULL;
196 LocalFree (wargv);
198 if(crash_handling)
200 static wchar_t path[MAX_PATH];
201 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
202 NULL, SHGFP_TYPE_CURRENT, path ) )
203 fprintf( stderr, "Can't open the vlc conf PATH\n" );
204 _snwprintf( path+wcslen( path ), MAX_PATH, L"%s", L"\\vlc\\crashdump" );
205 crashdump_path = &path[0];
207 check_crashdump();
208 SetUnhandledExceptionFilter(vlc_exception_filter);
211 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
213 /* */
214 if (!lang)
216 HKEY h_key;
217 if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
218 == ERROR_SUCCESS )
220 TCHAR szData[256];
221 DWORD len = 256;
222 if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
223 lang = FromWide( szData );
227 if (lang && strncmp( lang, "auto", 4 ) )
229 char tmp[11];
230 snprintf(tmp, 11, "LANG=%s", lang);
231 putenv(tmp);
233 free(lang);
235 /* Initialize libvlc */
236 libvlc_instance_t *vlc;
237 vlc = libvlc_new (argc, (const char **)argv);
238 if (vlc != NULL)
240 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
241 PACKAGE_NAME);
242 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
243 libvlc_add_intf (vlc, "hotkeys,none");
244 libvlc_add_intf (vlc, "globalhotkeys,none");
245 libvlc_add_intf (vlc, NULL);
246 libvlc_playlist_play (vlc, -1, 0, NULL);
247 libvlc_wait (vlc);
248 libvlc_release (vlc);
250 else
251 MessageBox (NULL, TEXT("VLC media player could not start.\n"
252 "Either the command line options were invalid or no plugins were found.\n"),
253 TEXT("VLC media player"),
254 MB_OK|MB_ICONERROR);
257 for (int i = 0; i < argc; i++)
258 free (argv[i]);
260 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
261 return 0;
264 /* Crashdumps handling */
265 static void check_crashdump(void)
267 wchar_t mv_crashdump_path[MAX_PATH];
268 wcscpy (mv_crashdump_path, crashdump_path);
269 wcscat (mv_crashdump_path, L".mv");
271 if (_wrename (crashdump_path, mv_crashdump_path))
272 return;
274 FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
275 if( !fd )
276 return;
277 fclose( fd );
279 int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
280 "Would you like to send a bug report to the developers team?",
281 L"VLC crash reporting", MB_YESNO);
283 if(answer == IDYES)
285 HMODULE hWininet = LoadLibrary(TEXT("wininet.dll"));
286 if (hWininet == NULL)
288 fprintf(stderr, "There was an error loading the network"
289 " 0x%08lx\n", (unsigned long)GetLastError());
290 goto done;
293 HINTERNET (WINAPI *InternetOpenW_)(LPCWSTR ,DWORD dwAccessType,LPCWSTR lpszProxy,LPCWSTR lpszProxyBypass,DWORD dwFlags);
294 HINTERNET (WINAPI *InternetConnectW_)(HINTERNET hInternet,LPCWSTR lpszServerName,INTERNET_PORT nServerPort,LPCWSTR lpszUserName,LPCWSTR lpszPassword,DWORD dwService,DWORD dwFlags,DWORD_PTR dwContext);
295 BOOL (WINAPI *InternetCloseHandle_)(HINTERNET hInternet);
296 BOOL (WINAPI *FtpPutFileW_)(HINTERNET hConnect,LPCWSTR lpszLocalFile,LPCWSTR lpszNewRemoteFile,DWORD dwFlags,DWORD_PTR dwContext);
297 InternetOpenW_ = (void*)GetProcAddress(hWininet, "InternetOpenW");
298 InternetConnectW_ = (void*)GetProcAddress(hWininet, "InternetConnectW");
299 InternetCloseHandle_ = (void*)GetProcAddress(hWininet, "InternetCloseHandle");
300 FtpPutFileW_ = (void*)GetProcAddress(hWininet, "FtpPutFileW");
301 if (!InternetOpenW_ || !InternetConnectW_ || !InternetCloseHandle_ || !FtpPutFileW_)
303 fprintf(stderr, "There was an error loading the network API entries"
304 " 0x%08lx\n", (unsigned long)GetLastError());
305 goto done;
308 HINTERNET Hint = InternetOpenW_(L"VLC Crash Reporter",
309 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
310 if(Hint)
312 HINTERNET ftp = InternetConnectW_(Hint, L"crash.videolan.org",
313 INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
314 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
315 if(ftp)
317 SYSTEMTIME now;
318 GetSystemTime(&now);
319 wchar_t remote_file[MAX_PATH];
320 _snwprintf(remote_file, MAX_PATH,
321 L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
322 now.wYear, now.wMonth, now.wDay, now.wHour,
323 now.wMinute, now.wSecond );
325 if( FtpPutFileW_( ftp, mv_crashdump_path, remote_file,
326 FTP_TRANSFER_TYPE_BINARY, 0) )
327 fprintf(stderr, "Report sent correctly to FTP.\n");
328 else
329 fprintf(stderr,"Couldn't send report to FTP server\n");
331 InternetCloseHandle_(ftp);
333 else
335 fprintf(stderr, "Can't connect to FTP server 0x%08lx\n",
336 (unsigned long)GetLastError());
338 InternetCloseHandle_(Hint);
340 else
342 fprintf(stderr, "There was an error while connecting to the "
343 "Internet 0x%08lx\n", (unsigned long)GetLastError());
345 done:
346 if (hWininet != NULL)
347 FreeLibrary(hWininet);
348 MessageBox( NULL, L"Thanks a lot for helping improving VLC!",
349 L"VLC crash report" , MB_OK);
352 _wremove(mv_crashdump_path);
355 /*****************************************************************************
356 * vlc_exception_filter: handles unhandled exceptions, like segfaults
357 *****************************************************************************/
358 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
360 if(IsDebuggerPresent())
362 //If a debugger is present, pass the exception to the debugger
363 //with EXCEPTION_CONTINUE_SEARCH
364 return EXCEPTION_CONTINUE_SEARCH;
366 else
368 fprintf( stderr, "unhandled vlc exception\n" );
370 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
372 if( !fd )
374 fprintf( stderr, "\nerror while opening file" );
375 exit( 1 );
378 OSVERSIONINFO osvi;
379 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
380 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
381 GetVersionEx( &osvi );
383 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
384 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
385 osvi.dwPlatformId, osvi.szCSDVersion);
387 const CONTEXT *const pContext = (const CONTEXT *)
388 lpExceptionInfo->ContextRecord;
389 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
390 lpExceptionInfo->ExceptionRecord;
391 /* No nested exceptions for now */
392 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",
393 pException->ExceptionCode, pException->ExceptionAddress );
395 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
396 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
398 #ifdef _WIN64
399 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
400 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
401 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
402 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
403 "R13:%px\nR14:%px\nR15:%px\n",
404 pContext->Rdi,pContext->Rsi,pContext->Rbx,
405 pContext->Rdx,pContext->Rcx,pContext->Rax,
406 pContext->Rbp,pContext->Rip,pContext->Rsp,
407 pContext->R8,pContext->R9,pContext->R10,
408 pContext->R11,pContext->R12,pContext->R13,
409 pContext->R14,pContext->R15 );
410 #else
411 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
412 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
413 "EBP:%px\nEIP:%px\nESP:%px\n",
414 pContext->Edi,pContext->Esi,pContext->Ebx,
415 pContext->Edx,pContext->Ecx,pContext->Eax,
416 pContext->Ebp,pContext->Eip,pContext->Esp );
417 #endif
419 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
421 #ifdef _WIN64
422 LPCVOID caller = (LPCVOID)pContext->Rip;
423 LPVOID *pBase = (LPVOID*)pContext->Rbp;
424 #else
425 LPVOID *pBase = (LPVOID*)pContext->Ebp;
426 LPCVOID caller = (LPCVOID)pContext->Eip;
427 #endif
428 for( unsigned frame = 0; frame <= 100; frame++ )
430 MEMORY_BASIC_INFORMATION mbi;
431 wchar_t module[ 256 ];
432 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
433 GetModuleFileName( mbi.AllocationBase, module, 256 );
434 fwprintf( fd, L"%p|%ls\n", caller, module );
436 if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
437 break;
439 /*The last BP points to NULL!*/
440 caller = *(pBase + 1);
441 if( !caller )
442 break;
443 pBase = *pBase;
444 if( !pBase )
445 break;
448 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
449 FALSE, GetCurrentProcessId());
450 if (hpid) {
451 HMODULE mods[1024];
452 DWORD size;
453 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
454 fwprintf( fd, L"\n\n[modules]\n" );
455 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
456 wchar_t module[ 256 ];
457 GetModuleFileName(mods[i], module, 256);
458 fwprintf( fd, L"%p|%ls\n", mods[i], module);
461 CloseHandle(hpid);
464 fclose( fd );
465 fflush( stderr );
466 exit( 1 );