Qt: remove a suspicious delete
[vlc.git] / bin / winvlc.c
blobd44773da2542dcf0a752eca00266b2c05c475873
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 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
65 LPSTR lpCmdLine,
66 int nCmdShow )
68 int argc;
70 /* VLC does not change the thread locale, so gettext/libintil will use the
71 * user default locale as reference. */
72 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
73 * if the GETTEXT_MUI environment variable is set. If not set or if running
74 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
75 * putenv() call may become redundant with later versions of gettext. */
76 putenv("GETTEXT_MUI=1");
77 #ifdef TOP_BUILDDIR
78 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
79 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
80 #endif
82 SetErrorMode(SEM_FAILCRITICALERRORS);
83 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
85 /* SetProcessDEPPolicy */
86 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
87 if(h_Kernel32)
89 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
90 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
91 # define PROCESS_DEP_ENABLE 1
93 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
94 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
95 if(mySetProcessDEPPolicy)
96 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
98 /* Do NOT load any library from cwd. */
99 mySetDllDirectoryA = (BOOL WINAPI (*)(const char*))
100 GetProcAddress(h_Kernel32, "SetDllDirectoryA");
101 if(mySetDllDirectoryA)
102 mySetDllDirectoryA("");
104 FreeLibrary(h_Kernel32);
107 /* Args */
108 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
109 if (wargv == NULL)
110 return 1;
112 char *argv[argc + 4];
113 BOOL crash_handling = TRUE;
114 int j = 0;
115 char *lang = NULL;
117 argv[j++] = FromWide( L"--media-library" );
118 argv[j++] = FromWide( L"--stats" );
119 argv[j++] = FromWide( L"--no-ignore-config" );
120 for (int i = 1; i < argc; i++)
122 if(!wcscmp(wargv[i], L"--no-crashdump"))
124 crash_handling = FALSE;
125 continue; /* don't give argument to libvlc */
127 if (!wcsncmp(wargv[i], L"--language", 10) )
129 if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
130 lang = FromWide (wargv[++i]);
131 continue;
134 argv[j++] = FromWide (wargv[i]);
137 argc = j;
138 argv[argc] = NULL;
139 LocalFree (wargv);
141 if(crash_handling)
143 static wchar_t path[MAX_PATH];
144 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
145 NULL, SHGFP_TYPE_CURRENT, path ) )
146 fprintf( stderr, "Can't open the vlc conf PATH\n" );
147 _snwprintf( path+wcslen( path ), MAX_PATH, L"%s", L"\\vlc\\crashdump" );
148 crashdump_path = &path[0];
150 check_crashdump();
151 SetUnhandledExceptionFilter(vlc_exception_filter);
154 _setmode( STDIN_FILENO, _O_BINARY ); /* Needed for pipes */
156 /* */
157 if (!lang)
159 HKEY h_key;
160 if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
161 == ERROR_SUCCESS )
163 TCHAR szData[256];
164 DWORD len = 256;
165 if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
166 lang = FromWide( szData );
170 if (lang && strncmp( lang, "auto", 4 ) )
172 char tmp[11];
173 snprintf(tmp, 11, "LANG=%s", lang);
174 putenv(tmp);
176 free(lang);
178 /* Initialize libvlc */
179 libvlc_instance_t *vlc;
180 vlc = libvlc_new (argc, (const char **)argv);
181 if (vlc != NULL)
183 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
184 PACKAGE_NAME);
185 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
186 libvlc_add_intf (vlc, "hotkeys,none");
187 libvlc_add_intf (vlc, "globalhotkeys,none");
188 libvlc_add_intf (vlc, NULL);
189 libvlc_playlist_play (vlc, -1, 0, NULL);
190 libvlc_wait (vlc);
191 libvlc_release (vlc);
193 else
194 MessageBox (NULL, TEXT("VLC media player could not start.\n"
195 "Either the command line options were invalid or no plugins were found.\n"),
196 TEXT("VLC media player"),
197 MB_OK|MB_ICONERROR);
200 for (int i = 0; i < argc; i++)
201 free (argv[i]);
203 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
204 return 0;
207 /* Crashdumps handling */
208 static void check_crashdump(void)
210 FILE * fd = _wfopen ( crashdump_path, L"r, ccs=UTF-8" );
211 if( !fd )
212 return;
213 fclose( fd );
215 int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
216 "Would you like to send a bug report to the developers team?",
217 L"VLC crash reporting", MB_YESNO);
219 if(answer == IDYES)
221 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
222 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
223 if(Hint)
225 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
226 INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
227 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
228 if(ftp)
230 SYSTEMTIME now;
231 GetSystemTime(&now);
232 wchar_t remote_file[MAX_PATH];
233 _snwprintf(remote_file, MAX_PATH,
234 L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
235 now.wYear, now.wMonth, now.wDay, now.wHour,
236 now.wMinute, now.wSecond );
238 if( FtpPutFile( ftp, crashdump_path, remote_file,
239 FTP_TRANSFER_TYPE_BINARY, 0) )
240 MessageBox( NULL, L"Report sent correctly. Thanks a lot " \
241 "for the help.", L"Report sent", MB_OK);
242 else
243 MessageBox( NULL, L"There was an error while "\
244 "transferring the data to the FTP server.\n"\
245 "Thanks a lot for the help.",
246 L"Report sending failed", MB_OK);
247 InternetCloseHandle(ftp);
249 else
251 MessageBox( NULL, L"There was an error while connecting to " \
252 "the FTP server. "\
253 "Thanks a lot for the help.",
254 L"Report sending failed", MB_OK);
255 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
256 (unsigned long)GetLastError());
258 InternetCloseHandle(Hint);
260 else
262 MessageBox( NULL, L"There was an error while connecting to the Internet.\n"\
263 "Thanks a lot for the help anyway.",
264 L"Report sending failed", MB_OK);
268 _wremove(crashdump_path);
271 /*****************************************************************************
272 * vlc_exception_filter: handles unhandled exceptions, like segfaults
273 *****************************************************************************/
274 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
276 if(IsDebuggerPresent())
278 //If a debugger is present, pass the exception to the debugger
279 //with EXCEPTION_CONTINUE_SEARCH
280 return EXCEPTION_CONTINUE_SEARCH;
282 else
284 fprintf( stderr, "unhandled vlc exception\n" );
286 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
288 if( !fd )
290 fprintf( stderr, "\nerror while opening file" );
291 exit( 1 );
294 OSVERSIONINFO osvi;
295 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
296 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
297 GetVersionEx( &osvi );
299 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE,
300 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
301 osvi.dwPlatformId, osvi.szCSDVersion);
303 const CONTEXT *const pContext = (const CONTEXT *)
304 lpExceptionInfo->ContextRecord;
305 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
306 lpExceptionInfo->ExceptionRecord;
307 /* No nested exceptions for now */
308 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",
309 pException->ExceptionCode, pException->ExceptionAddress );
311 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
312 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
314 #ifdef _WIN64
315 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
316 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
317 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
318 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
319 "R13:%px\nR14:%px\nR15:%px\n",
320 pContext->Rdi,pContext->Rsi,pContext->Rbx,
321 pContext->Rdx,pContext->Rcx,pContext->Rax,
322 pContext->Rbp,pContext->Rip,pContext->Rsp,
323 pContext->R8,pContext->R9,pContext->R10,
324 pContext->R11,pContext->R12,pContext->R13,
325 pContext->R14,pContext->R15 );
326 #else
327 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
328 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
329 "EBP:%px\nEIP:%px\nESP:%px\n",
330 pContext->Edi,pContext->Esi,pContext->Ebx,
331 pContext->Edx,pContext->Ecx,pContext->Eax,
332 pContext->Ebp,pContext->Eip,pContext->Esp );
333 #endif
335 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
337 #ifdef _WIN64
338 LPCVOID caller = (LPCVOID)pContext->Rip;
339 LPVOID *pBase = (LPVOID*)pContext->Rbp;
340 #else
341 LPVOID *pBase = (LPVOID*)pContext->Ebp;
342 LPCVOID caller = (LPCVOID)pContext->Eip;
343 #endif
344 for( unsigned frame = 0; frame <= 100; frame++ )
346 MEMORY_BASIC_INFORMATION mbi;
347 wchar_t module[ 256 ];
348 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
349 GetModuleFileName( mbi.AllocationBase, module, 256 );
350 fwprintf( fd, L"%p|%s\n", caller, module );
352 /*The last BP points to NULL!*/
353 caller = *(pBase + 1);
354 if( !caller )
355 break;
356 pBase = *pBase;
359 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
360 FALSE, GetCurrentProcessId());
361 if (hpid) {
362 HMODULE mods[1024];
363 DWORD size;
364 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
365 fwprintf( fd, L"\n\n[modules]\n" );
366 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
367 wchar_t module[ 256 ];
368 GetModuleFileName(mods[i], module, 256);
369 fwprintf( fd, L"%p|%s\n", mods[i], module);
372 CloseHandle(hpid);
375 fclose( fd );
376 fflush( stderr );
377 exit( 1 );