1 /*****************************************************************************
2 * winvlc.c: the Windows VLC player
3 *****************************************************************************
4 * Copyright (C) 1998-2008 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 *****************************************************************************/
38 #if !defined(UNDER_CE) && defined ( NDEBUG )
39 # define _WIN32_IE 0x500
41 # include <tlhelp32.h>
43 static void check_crashdump();
44 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
48 static char *FromWide (const wchar_t *wide)
51 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
53 char *out = (char *)malloc (len);
55 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
59 static int parse_cmdline (char *line, char ***argvp)
61 char **argv = malloc (sizeof (char *));
68 /* Skips white spaces */
69 while (strchr ("\t ", *line))
74 /* Starts a new parameter */
75 argv = realloc (argv, (argc + 2) * sizeof (char *));
84 while (*line && !strchr ("\t ", *line))
87 if (line > argv[argc - 1] && line[-1] == quote)
88 /* End of quoted parameter */
93 /* Space within a quote */
98 /* End of unquoted parameter */
108 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
118 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
122 char *argv[argc + 1];
123 for (int i = 0; i < argc; i++)
124 argv[i] = FromWide (wargv[i]);
128 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
130 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
131 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
133 argc = parse_cmdline (psz_cmdline, &argv);
136 libvlc_exception_t ex, dummy;
137 libvlc_exception_init (&ex);
138 libvlc_exception_init (&dummy);
140 #if !defined( UNDER_CE ) && defined ( NDEBUG )
142 SetUnhandledExceptionFilter(vlc_exception_filter);
145 /* Initialize libvlc */
146 libvlc_instance_t *vlc;
147 vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
150 libvlc_add_intf (vlc, NULL, &ex);
151 libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
153 libvlc_release (vlc);
156 ret = libvlc_exception_raised (&ex);
157 libvlc_exception_clear (&ex);
158 libvlc_exception_clear (&dummy);
160 for (int i = 0; i < argc; i++)
163 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
167 #if !defined( UNDER_CE ) && defined ( NDEBUG )
169 static void get_crashdump_path(wchar_t * wdir)
171 if( S_OK != SHGetFolderPathW( NULL,
172 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
173 NULL, SHGFP_TYPE_CURRENT, wdir ) )
174 fprintf( stderr, "Can't open the vlc conf PATH\n" );
176 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
179 static void check_crashdump()
181 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
182 get_crashdump_path(wdir);
184 FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
188 int answer = MessageBox( NULL, L"VLC media player just crashed." \
189 " Do you want to send a bug report to the developers team?",
190 L"VLC crash reporting", MB_YESNO);
194 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
197 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
198 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
203 wchar_t remote_file[MAX_PATH];
204 swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
205 now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
207 FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
208 InternetCloseHandle(ftp);
211 fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
212 InternetCloseHandle(Hint);
221 /*****************************************************************************
222 * vlc_exception_filter: handles unhandled exceptions, like segfaults
223 *****************************************************************************/
224 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
226 fprintf( stderr, "unhandled vlc exception\n" );
228 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
229 get_crashdump_path(wdir);
230 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
235 fprintf( stderr, "\nerror while opening file" );
240 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
241 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
242 GetVersionEx( &osvi );
244 fwprintf( fd, L"[Version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
250 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
251 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
252 /*No nested exceptions for now*/
253 fwprintf( fd, L"\n\n[Exceptions]\n%08x at %08x",pException->ExceptionCode,
254 pException->ExceptionAddress );
255 if( pException->NumberParameters > 0 )
258 for( i = 0; i < pException->NumberParameters; i++ )
259 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
262 fwprintf( fd, L"\n\n[CONTEXT]\nEDI:%08x\nESI:%08x\n" \
263 "EBX:%08x\nEDX:%08xn\nECX:%08x\nEAX:%08x\n" \
264 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
265 pContext->Edi,pContext->Esi,pContext->Ebx,
266 pContext->Edx,pContext->Ecx,pContext->Eax,
267 pContext->Ebp,pContext->Eip,pContext->Esp );
269 fwprintf( fd, L"\n\n[STACKTRACE]\n#EIP|base|module\n" );
271 DWORD pEbp = pContext->Ebp;
272 DWORD caller = *((DWORD*)pEbp + 1) ;
274 wchar_t module[ 256 ];
278 MEMORY_BASIC_INFORMATION mbi ;
279 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
280 HINSTANCE hInstance = mbi.AllocationBase;
281 GetModuleFileName( hInstance, module, 256 ) ;
282 fwprintf( fd, L"%08x|%08x|%s\n", caller, hInstance, module );
283 pEbp = *(DWORD*)pEbp ;
284 caller = *((DWORD*)pEbp + 1) ;
285 /*The last EBP points to NULL!*/