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>
42 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
46 static char *FromWide (const wchar_t *wide)
49 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
51 char *out = (char *)malloc (len);
53 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
57 static int parse_cmdline (char *line, char ***argvp)
59 char **argv = malloc (sizeof (char *));
66 /* Skips white spaces */
67 while (strchr ("\t ", *line))
72 /* Starts a new parameter */
73 argv = realloc (argv, (argc + 2) * sizeof (char *));
82 while (*line && !strchr ("\t ", *line))
85 if (line > argv[argc - 1] && line[-1] == quote)
86 /* End of quoted parameter */
91 /* Space within a quote */
96 /* End of unquoted parameter */
106 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
116 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
120 char *argv[argc + 1];
121 for (int i = 0; i < argc; i++)
122 argv[i] = FromWide (wargv[i]);
126 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
128 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
129 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
131 argc = parse_cmdline (psz_cmdline, &argv);
134 libvlc_exception_t ex, dummy;
135 libvlc_exception_init (&ex);
136 libvlc_exception_init (&dummy);
138 #if !defined( UNDER_CE ) && defined ( NDEBUG )
139 SetUnhandledExceptionFilter(vlc_exception_filter);
142 /* Initialize libvlc */
143 libvlc_instance_t *vlc;
144 vlc = libvlc_new (argc - 1, (const char **)argv + 1, &ex);
147 libvlc_add_intf (vlc, NULL, &ex);
148 libvlc_playlist_play (vlc, -1, 0, NULL, &dummy);
150 libvlc_release (vlc);
153 ret = libvlc_exception_raised (&ex);
154 libvlc_exception_clear (&ex);
155 libvlc_exception_clear (&dummy);
157 for (int i = 0; i < argc; i++)
160 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
164 #if !defined( UNDER_CE ) && defined ( NDEBUG )
165 /*****************************************************************************
166 * vlc_exception_filter: handles unhandled exceptions, like segfaults
167 *****************************************************************************/
168 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
170 wchar_t wdir[MAX_PATH];
172 fprintf( stderr, "unhandled vlc exception\n" );
174 if( S_OK != SHGetFolderPathW( NULL,
175 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
176 NULL, SHGFP_TYPE_CURRENT, wdir ) )
177 fprintf( stderr, "Can't open the vlc conf PATH\n" );
179 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
181 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
184 fprintf( stderr, "\nerror while opening file" );
187 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
188 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
189 GetVersionEx( &osvi );
191 fwprintf( fd, L"[Version]\n0S=%d.%d.%d.%d.%s\nVLC=%s", osvi.dwMajorVersion,
198 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
199 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
200 /*No nested exceptions for now*/
201 fwprintf( fd, L"\n\n[Exceptions]\n%08x at %08x",pException->ExceptionCode,
202 pException->ExceptionAddress );
203 if( pException->NumberParameters > 0 )
206 for( i = 0; i < pException->NumberParameters; i++ )
207 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
210 fwprintf( fd, L"\n\n[CONTEXT]\nEDI:%08x\nESI:%08x\n" \
211 "EBX:%08x\nEDX:%08xn\nECX:%08x\nEAX:%08x\n" \
212 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
213 pContext->Edi,pContext->Esi,pContext->Ebx,
214 pContext->Edx,pContext->Ecx,pContext->Eax,
215 pContext->Ebp,pContext->Eip,pContext->Esp );
217 fwprintf( fd, L"\n\n[STACKTRACE]\n#EIP|base|module\n" );
219 DWORD pEbp = pContext->Ebp;
220 DWORD caller = *((DWORD*)pEbp + 1) ;
222 wchar_t module[ 256 ];
226 MEMORY_BASIC_INFORMATION mbi ;
227 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
228 HINSTANCE hInstance = mbi.AllocationBase;
229 GetModuleFileName( hInstance, module, 256 ) ;
230 fwprintf( fd, L"%08x|%08x|%s\n", caller, hInstance, module );
231 pEbp = *(DWORD*)pEbp ;
232 caller = *((DWORD*)pEbp + 1) ;
233 /*The last EBP points to NULL!*/