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 *****************************************************************************/
35 #if !defined(UNDER_CE)
37 # define _WIN32_IE 0x501
43 # define PSAPI_VERSION 1
45 # define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
46 static void check_crashdump(void);
47 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo);
48 static const wchar_t *crashdump_path;
52 static char *FromWide (const wchar_t *wide)
55 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
57 char *out = (char *)malloc (len);
59 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
63 static int parse_cmdline (char *line, char ***argvp)
65 char **argv = malloc (sizeof (char *));
72 /* Skips white spaces */
73 while (strchr ("\t ", *line))
78 /* Starts a new parameter */
79 argv = realloc (argv, (argc + 2) * sizeof (char *));
88 while (*line && !strchr ("\t ", *line))
91 if (line > argv[argc - 1] && line[-1] == quote)
92 /* End of quoted parameter */
97 /* Space within a quote */
102 /* End of unquoted parameter */
112 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
124 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
127 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
129 /* SetProcessDEPPolicy */
130 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
133 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
134 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
135 # define PROCESS_DEP_ENABLE 1
137 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
138 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
139 if(mySetProcessDEPPolicy)
140 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
142 /* Do NOT load any library from cwd. */
143 mySetDllDirectoryA = (BOOL WINAPI (*)(const char*)) GetProcAddress(h_Kernel32, "SetDllDirectoryA");
144 if(mySetDllDirectoryA)
145 mySetDllDirectoryA("");
147 FreeLibrary(h_Kernel32);
151 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
155 char *argv[argc + 3];
156 BOOL crash_handling = TRUE;
159 argv[j++] = FromWide( L"--media-library" );
160 argv[j++] = FromWide( L"--no-ignore-config" );
162 argv[j++] = FromWide (L"--data-path=Z:"TOP_SRCDIR"/share");
164 for (int i = 1; i < argc; i++)
166 if(!wcscmp(wargv[i], L"--no-crashdump"))
168 crash_handling = FALSE;
169 continue; /* don't give argument to libvlc */
172 argv[j++] = FromWide (wargv[i]);
181 static wchar_t path[MAX_PATH];
182 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
183 NULL, SHGFP_TYPE_CURRENT, path ) )
184 fprintf( stderr, "Can't open the vlc conf PATH\n" );
185 swprintf( path+wcslen( path ), L"%s", L"\\vlc\\crashdump" );
186 crashdump_path = &path[0];
189 SetUnhandledExceptionFilter(vlc_exception_filter);
192 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
195 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
197 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
198 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
200 argc = parse_cmdline (psz_cmdline, &argv);
203 /* Initialize libvlc */
204 libvlc_instance_t *vlc;
205 vlc = libvlc_new (argc, (const char **)argv);
208 libvlc_add_intf (vlc, "globalhotkeys,none");
209 libvlc_add_intf (vlc, NULL);
210 libvlc_playlist_play (vlc, -1, 0, NULL);
212 libvlc_release (vlc);
215 for (int i = 0; i < argc; i++)
218 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
222 #if !defined( UNDER_CE )
223 /* Crashdumps handling */
224 static void check_crashdump(void)
226 FILE * fd = _wfopen ( crashdump_path, L"r, ccs=UTF-8" );
231 int answer = MessageBox( NULL, L"VLC media player just crashed." \
232 " Do you want to send a bug report to the developers team?",
233 L"VLC crash reporting", MB_YESNO);
237 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
240 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
241 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
246 wchar_t remote_file[MAX_PATH];
247 swprintf( remote_file, L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
248 now.wYear, now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
250 if( FtpPutFile( ftp, crashdump_path, remote_file, FTP_TRANSFER_TYPE_BINARY, 0) )
251 MessageBox( NULL, L"Report sent correctly. Thanks a lot for the help.",
252 L"Report sent", MB_OK);
254 MessageBox( NULL, L"There was an error while transferring to the FTP server. "\
255 "Thanks a lot for the help anyway.",
256 L"Report sending failed", MB_OK);
257 InternetCloseHandle(ftp);
261 MessageBox( NULL, L"There was an error while connecting to the FTP server. "\
262 "Thanks a lot for the help anyway.",
263 L"Report sending failed", MB_OK);
264 fprintf(stderr,"Can't connect to FTP server 0x%08lu\n",
265 (unsigned long)GetLastError());
267 InternetCloseHandle(Hint);
271 MessageBox( NULL, L"There was an error while connecting to Internet. "\
272 "Thanks a lot for the help anyway.",
273 L"Report sending failed", MB_OK);
277 _wremove(crashdump_path);
280 /*****************************************************************************
281 * vlc_exception_filter: handles unhandled exceptions, like segfaults
282 *****************************************************************************/
283 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
285 if(IsDebuggerPresent())
287 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
288 return EXCEPTION_CONTINUE_SEARCH;
292 fprintf( stderr, "unhandled vlc exception\n" );
294 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
298 fprintf( stderr, "\nerror while opening file" );
303 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
304 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
305 GetVersionEx( &osvi );
307 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
313 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
314 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
315 /*No nested exceptions for now*/
316 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",pException->ExceptionCode,
317 pException->ExceptionAddress );
319 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
320 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
323 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
324 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
325 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
326 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
327 "R13:%px\nR14:%px\nR15:%px\n",
328 pContext->Rdi,pContext->Rsi,pContext->Rbx,
329 pContext->Rdx,pContext->Rcx,pContext->Rax,
330 pContext->Rbp,pContext->Rip,pContext->Rsp,
331 pContext->R8,pContext->R9,pContext->R10,
332 pContext->R11,pContext->R12,pContext->R13,
333 pContext->R14,pContext->R15 );
335 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
336 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
337 "EBP:%px\nEIP:%px\nESP:%px\n",
338 pContext->Edi,pContext->Esi,pContext->Ebx,
339 pContext->Edx,pContext->Ecx,pContext->Eax,
340 pContext->Ebp,pContext->Eip,pContext->Esp );
343 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
344 FALSE, GetCurrentProcessId());
348 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
349 fwprintf( fd, L"\n\n[modules]\n" );
350 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
351 wchar_t module[ 256 ];
352 GetModuleFileName(mods[i], module, 256);
353 fwprintf( fd, L"%p|%s\n", mods[i], module);
360 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
363 LPCVOID caller = (LPCVOID)pContext->Rip;
364 LPVOID *pBase = (LPVOID*)pContext->Rbp;
366 LPVOID *pBase = (LPVOID*)pContext->Ebp;
367 LPCVOID caller = (LPCVOID)pContext->Eip;
369 for( unsigned frame = 0; frame <= 100; frame++ )
371 MEMORY_BASIC_INFORMATION mbi;
372 wchar_t module[ 256 ];
373 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
374 GetModuleFileName( mbi.AllocationBase, module, 256 );
375 fwprintf( fd, L"%p|%s\n", caller, module );
377 /*The last BP points to NULL!*/
378 caller = *(pBase + 1);