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 *****************************************************************************/
36 #if !defined(UNDER_CE)
38 # define _WIN32_IE 0x501
44 # define PSAPI_VERSION 1
46 # define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
47 static void check_crashdump(void);
48 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
49 static const wchar_t *crashdump_path;
53 static char *FromWide (const wchar_t *wide)
56 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
58 char *out = (char *)malloc (len);
60 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
64 static int parse_cmdline (char *line, char ***argvp)
66 char **argv = malloc (sizeof (char *));
73 /* Skips white spaces */
74 while (strchr ("\t ", *line))
79 /* Starts a new parameter */
80 argv = realloc (argv, (argc + 2) * sizeof (char *));
89 while (*line && !strchr ("\t ", *line))
92 if (line > argv[argc - 1] && line[-1] == quote)
93 /* End of quoted parameter */
98 /* Space within a quote */
103 /* End of unquoted parameter */
113 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
125 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
126 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
129 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
131 /* SetProcessDEPPolicy */
132 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
135 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
136 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
137 # define PROCESS_DEP_ENABLE 1
139 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
140 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
141 if(mySetProcessDEPPolicy)
142 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
144 /* Do NOT load any library from cwd. */
145 mySetDllDirectoryA = (BOOL WINAPI (*)(const char*)) GetProcAddress(h_Kernel32, "SetDllDirectoryA");
146 if(mySetDllDirectoryA)
147 mySetDllDirectoryA("");
149 FreeLibrary(h_Kernel32);
153 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
157 char *argv[argc + 3];
158 BOOL crash_handling = TRUE;
161 argv[j++] = FromWide( L"--media-library" );
162 argv[j++] = FromWide( L"--no-ignore-config" );
164 argv[j++] = FromWide (L"--data-path=Z:"TOP_SRCDIR"/share");
166 for (int i = 1; i < argc; i++)
168 if(!wcscmp(wargv[i], L"--no-crashdump"))
170 crash_handling = FALSE;
171 continue; /* don't give argument to libvlc */
174 argv[j++] = FromWide (wargv[i]);
183 static wchar_t path[MAX_PATH];
184 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
185 NULL, SHGFP_TYPE_CURRENT, path ) )
186 fprintf( stderr, "Can't open the vlc conf PATH\n" );
187 swprintf( path+wcslen( path ), L"%s", L"\\vlc\\crashdump" );
188 crashdump_path = &path[0];
191 SetUnhandledExceptionFilter(vlc_exception_filter);
194 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
197 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
199 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
200 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
202 argc = parse_cmdline (psz_cmdline, &argv);
205 /* Initialize libvlc */
206 libvlc_instance_t *vlc;
207 vlc = libvlc_new (argc, (const char **)argv);
210 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
211 libvlc_add_intf (vlc, "globalhotkeys,none");
212 libvlc_add_intf (vlc, NULL);
213 libvlc_playlist_play (vlc, -1, 0, NULL);
215 libvlc_release (vlc);
218 for (int i = 0; i < argc; i++)
221 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
225 #if !defined( UNDER_CE )
226 /* Crashdumps handling */
227 static void check_crashdump(void)
229 FILE * fd = _wfopen ( crashdump_path, L"r, ccs=UTF-8" );
234 int answer = MessageBox( NULL, L"VLC media player just crashed." \
235 " Do you want to send a bug report to the developers team?",
236 L"VLC crash reporting", MB_YESNO);
240 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
243 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
244 NULL, NULL, INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
249 wchar_t remote_file[MAX_PATH];
250 swprintf( remote_file, L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
251 now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
253 if( FtpPutFile( ftp, crashdump_path, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) )
254 MessageBox( NULL, L"Report sent correctly. Thanks a lot for the help.",
255 L"Report sent", MB_OK);
257 MessageBox( NULL, L"There was an error while transferring to the FTP server. "\
258 "Thanks a lot for the help anyway.",
259 L"Report sending failed", MB_OK);
260 InternetCloseHandle(ftp);
264 MessageBox( NULL, L"There was an error while connecting to the FTP server. "\
265 "Thanks a lot for the help anyway.",
266 L"Report sending failed", MB_OK);
267 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
268 (unsigned long)GetLastError());
270 InternetCloseHandle(Hint);
274 MessageBox( NULL, L"There was an error while connecting to Internet. "\
275 "Thanks a lot for the help anyway.",
276 L"Report sending failed", MB_OK);
280 _wremove(crashdump_path);
283 /*****************************************************************************
284 * vlc_exception_filter: handles unhandled exceptions, like segfaults
285 *****************************************************************************/
286 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
288 if(IsDebuggerPresent())
290 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
291 return EXCEPTION_CONTINUE_SEARCH;
295 fprintf( stderr, "unhandled vlc exception\n" );
297 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
301 fprintf( stderr, "\nerror while opening file" );
306 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
307 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
308 GetVersionEx( &osvi );
310 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
316 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
317 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
318 /*No nested exceptions for now*/
319 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",pException->ExceptionCode,
320 pException->ExceptionAddress );
322 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
323 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
326 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
327 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
328 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
329 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
330 "R13:%px\nR14:%px\nR15:%px\n",
331 pContext->Rdi,pContext->Rsi,pContext->Rbx,
332 pContext->Rdx,pContext->Rcx,pContext->Rax,
333 pContext->Rbp,pContext->Rip,pContext->Rsp,
334 pContext->R8,pContext->R9,pContext->R10,
335 pContext->R11,pContext->R12,pContext->R13,
336 pContext->R14,pContext->R15 );
338 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
339 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
340 "EBP:%px\nEIP:%px\nESP:%px\n",
341 pContext->Edi,pContext->Esi,pContext->Ebx,
342 pContext->Edx,pContext->Ecx,pContext->Eax,
343 pContext->Ebp,pContext->Eip,pContext->Esp );
346 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
347 FALSE, GetCurrentProcessId());
351 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
352 fwprintf( fd, L"\n\n[modules]\n" );
353 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
354 wchar_t module[ 256 ];
355 GetModuleFileName(mods[i], module, 256);
356 fwprintf( fd, L"%p|%s\n", mods[i], module);
363 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
366 LPCVOID caller = (LPCVOID)pContext->Rip;
367 LPVOID *pBase = (LPVOID*)pContext->Rbp;
369 LPVOID *pBase = (LPVOID*)pContext->Ebp;
370 LPCVOID caller = (LPCVOID)pContext->Eip;
372 for( unsigned frame = 0; frame <= 100; frame++ )
374 MEMORY_BASIC_INFORMATION mbi;
375 wchar_t module[ 256 ];
376 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
377 GetModuleFileName( mbi.AllocationBase, module, 256 );
378 fwprintf( fd, L"%p|%s\n", caller, module );
380 /*The last BP points to NULL!*/
381 caller = *(pBase + 1);