access_New: handle errors
[vlc/asuraparaju-public.git] / bin / winvlc.c
blob90ed7ce4de1cbea559207622261811dcb451d16d
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 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #define UNICODE
32 #include <vlc/vlc.h>
33 #include <string.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <windows.h>
38 #if !defined(UNDER_CE)
39 # ifndef _WIN32_IE
40 # define _WIN32_IE 0x501
41 # endif
42 # include <shlobj.h>
43 # include <tlhelp32.h>
44 # include <wininet.h>
45 # ifndef _WIN64
46 static void check_crashdump(void);
47 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
48 # endif
49 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
50 #endif
52 #ifndef UNDER_CE
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;
63 #else
64 static int parse_cmdline (char *line, char ***argvp)
66 char **argv = malloc (sizeof (char *));
67 int argc = 0;
69 while (*line != '\0')
71 char quote = 0;
73 /* Skips white spaces */
74 while (strchr ("\t ", *line))
75 line++;
76 if (!*line)
77 break;
79 /* Starts a new parameter */
80 argv = realloc (argv, (argc + 2) * sizeof (char *));
81 if (*line == '"')
83 quote = '"';
84 line++;
86 argv[argc++] = line;
88 more:
89 while (*line && !strchr ("\t ", *line))
90 line++;
92 if (line > argv[argc - 1] && line[-1] == quote)
93 /* End of quoted parameter */
94 line[-1] = 0;
95 else
96 if (*line && quote)
98 /* Space within a quote */
99 line++;
100 goto more;
102 else
103 /* End of unquoted parameter */
104 if (*line)
105 *line++ = 0;
107 argv[argc] = NULL;
108 *argvp = argv;
109 return argc;
111 #endif
113 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
114 #ifndef UNDER_CE
115 LPSTR lpCmdLine,
116 #else
117 LPWSTR lpCmdLine,
118 #endif
119 int nCmdShow )
121 int argc;
122 #ifndef UNDER_CE
123 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
124 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
125 if (wargv == NULL)
126 return 1;
128 char *argv[argc + 1];
129 BOOL crash_handling = TRUE;
130 int j = 0;
132 argv[j++] = FromWide( L"--no-ignore-config" );
133 for (int i = 1; i < argc; i++)
135 if(!wcscmp(wargv[i], L"--no-crashdump"))
137 crash_handling = FALSE;
139 else
141 argv[j] = FromWide (wargv[i]);
142 j++;
146 argc = j;
147 argv[argc] = NULL;
148 LocalFree (wargv);
150 # ifndef _WIN64
151 if(crash_handling)
153 check_crashdump();
154 SetUnhandledExceptionFilter(vlc_exception_filter);
156 # endif /* WIN64 */
158 #else
159 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
161 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
162 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
164 argc = parse_cmdline (psz_cmdline, &argv);
165 #endif
167 /* Initialize libvlc */
168 libvlc_instance_t *vlc;
169 vlc = libvlc_new (argc, (const char **)argv);
170 if (vlc != NULL)
172 libvlc_add_intf (vlc, "globalhotkeys,none");
173 libvlc_add_intf (vlc, NULL);
174 libvlc_playlist_play (vlc, -1, 0, NULL);
175 libvlc_wait (vlc);
176 libvlc_release (vlc);
179 for (int i = 0; i < argc; i++)
180 free (argv[i]);
182 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
183 return 0;
186 #if !defined( UNDER_CE ) && !defined( _WIN64 )
188 static void get_crashdump_path(wchar_t * wdir)
190 if( S_OK != SHGetFolderPathW( NULL,
191 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
192 NULL, SHGFP_TYPE_CURRENT, wdir ) )
193 fprintf( stderr, "Can't open the vlc conf PATH\n" );
195 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
198 static void check_crashdump()
200 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
201 get_crashdump_path(wdir);
203 FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
204 if( fd )
206 fclose( fd );
207 int answer = MessageBox( NULL, L"VLC media player just crashed." \
208 " Do you want to send a bug report to the developers team?",
209 L"VLC crash reporting", MB_YESNO);
211 if(answer == IDYES)
213 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
214 if(Hint)
216 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
217 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
218 if(ftp)
220 SYSTEMTIME now;
221 GetSystemTime(&now);
222 wchar_t remote_file[MAX_PATH];
223 swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
224 now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
226 FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
227 InternetCloseHandle(ftp);
229 else
230 fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
231 InternetCloseHandle(Hint);
235 _wremove(wdir);
237 free((void *)wdir);
240 /*****************************************************************************
241 * vlc_exception_filter: handles unhandled exceptions, like segfaults
242 *****************************************************************************/
243 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
245 if(IsDebuggerPresent())
247 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
248 return EXCEPTION_CONTINUE_SEARCH;
250 else
252 fprintf( stderr, "unhandled vlc exception\n" );
254 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
255 get_crashdump_path(wdir);
256 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
257 free((void *)wdir);
259 if( !fd )
261 fprintf( stderr, "\nerror while opening file" );
262 exit( 1 );
265 OSVERSIONINFO osvi;
266 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
267 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
268 GetVersionEx( &osvi );
270 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
271 osvi.dwMinorVersion,
272 osvi.dwBuildNumber,
273 osvi.dwPlatformId,
274 osvi.szCSDVersion);
276 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
277 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
278 /*No nested exceptions for now*/
279 fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
280 pException->ExceptionAddress );
281 if( pException->NumberParameters > 0 )
283 unsigned int i;
284 for( i = 0; i < pException->NumberParameters; i++ )
285 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
288 fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
289 "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
290 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
291 pContext->Edi,pContext->Esi,pContext->Ebx,
292 pContext->Edx,pContext->Ecx,pContext->Eax,
293 pContext->Ebp,pContext->Eip,pContext->Esp );
295 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
297 wchar_t module[ 256 ];
298 MEMORY_BASIC_INFORMATION mbi ;
299 VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
300 HINSTANCE hInstance = mbi.AllocationBase;
301 GetModuleFileName( hInstance, module, 256 ) ;
302 fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
304 DWORD pEbp = pContext->Ebp;
305 DWORD caller = *((DWORD*)pEbp + 1);
309 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
310 HINSTANCE hInstance = mbi.AllocationBase;
311 GetModuleFileName( hInstance, module, 256 ) ;
312 fwprintf( fd, L"%08x|%s\n", caller, module );
313 pEbp = *(DWORD*)pEbp ;
314 caller = *((DWORD*)pEbp + 1) ;
315 /*The last EBP points to NULL!*/
316 }while(caller);
318 fclose( fd );
319 fflush( stderr );
320 exit( 1 );
323 #endif