Win32: Activate DEP on XP SP3. DEP is useless without ASLR, but people using an 10...
[vlc/solaris.git] / bin / winvlc.c
blob2d09cba320e3b0def7069ce1ebab25d1340161c5
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 typedef enum _HEAP_INFORMATION_CLASS {
50 HeapCompatibilityInformation,
51 HeapEnableTerminationOnCorruption
52 } HEAP_INFORMATION_CLASS;
53 WINBASEAPI BOOL WINAPI HeapSetInformation(HANDLE,HEAP_INFORMATION_CLASS,PVOID,SIZE_T);
54 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
55 #endif
57 #ifndef UNDER_CE
58 static char *FromWide (const wchar_t *wide)
60 size_t len;
61 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
63 char *out = (char *)malloc (len);
64 if (out)
65 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
66 return out;
68 #else
69 static int parse_cmdline (char *line, char ***argvp)
71 char **argv = malloc (sizeof (char *));
72 int argc = 0;
74 while (*line != '\0')
76 char quote = 0;
78 /* Skips white spaces */
79 while (strchr ("\t ", *line))
80 line++;
81 if (!*line)
82 break;
84 /* Starts a new parameter */
85 argv = realloc (argv, (argc + 2) * sizeof (char *));
86 if (*line == '"')
88 quote = '"';
89 line++;
91 argv[argc++] = line;
93 more:
94 while (*line && !strchr ("\t ", *line))
95 line++;
97 if (line > argv[argc - 1] && line[-1] == quote)
98 /* End of quoted parameter */
99 line[-1] = 0;
100 else
101 if (*line && quote)
103 /* Space within a quote */
104 line++;
105 goto more;
107 else
108 /* End of unquoted parameter */
109 if (*line)
110 *line++ = 0;
112 argv[argc] = NULL;
113 *argvp = argv;
114 return argc;
116 #endif
118 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
119 #ifndef UNDER_CE
120 LPSTR lpCmdLine,
121 #else
122 LPWSTR lpCmdLine,
123 #endif
124 int nCmdShow )
126 int argc;
127 #ifndef UNDER_CE
128 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
130 HINSTANCE h_Kernel32 = LoadLibraryW(L"kernel32.dll");
131 if(h_Kernel32)
133 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
134 # define PROCESS_DEP_ENABLE 1
136 mySetProcessDEPPolicy = (BOOL WINAPI (*)(DWORD))
137 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
138 if(mySetProcessDEPPolicy)
139 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
140 FreeLibrary(h_Kernel32);
143 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
144 if (wargv == NULL)
145 return 1;
147 char *argv[argc + 1];
148 BOOL crash_handling = TRUE;
149 int j = 0;
151 argv[j++] = FromWide( L"--no-ignore-config" );
152 for (int i = 1; i < argc; i++)
154 if(!wcscmp(wargv[i], L"--no-crashdump"))
156 crash_handling = FALSE;
158 else
160 argv[j] = FromWide (wargv[i]);
161 j++;
165 argc = j;
166 argv[argc] = NULL;
167 LocalFree (wargv);
169 # ifndef _WIN64
170 if(crash_handling)
172 check_crashdump();
173 SetUnhandledExceptionFilter(vlc_exception_filter);
175 # endif /* WIN64 */
177 #else
178 char **argv, psz_cmdline[wcslen(lpCmdLine) * 4];
180 WideCharToMultiByte( CP_UTF8, 0, lpCmdLine, -1,
181 psz_cmdline, sizeof (psz_cmdline), NULL, NULL );
183 argc = parse_cmdline (psz_cmdline, &argv);
184 #endif
186 /* Initialize libvlc */
187 libvlc_instance_t *vlc;
188 vlc = libvlc_new (argc, (const char **)argv);
189 if (vlc != NULL)
191 libvlc_add_intf (vlc, "globalhotkeys,none");
192 libvlc_add_intf (vlc, NULL);
193 libvlc_playlist_play (vlc, -1, 0, NULL);
194 libvlc_wait (vlc);
195 libvlc_release (vlc);
198 for (int i = 0; i < argc; i++)
199 free (argv[i]);
201 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
202 return 0;
205 #if !defined( UNDER_CE ) && !defined( _WIN64 )
207 static void get_crashdump_path(wchar_t * wdir)
209 if( S_OK != SHGetFolderPathW( NULL,
210 CSIDL_APPDATA | CSIDL_FLAG_CREATE,
211 NULL, SHGFP_TYPE_CURRENT, wdir ) )
212 fprintf( stderr, "Can't open the vlc conf PATH\n" );
214 swprintf( wdir+wcslen( wdir ), L"%s", L"\\vlc\\crashdump" );
217 static void check_crashdump()
219 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
220 get_crashdump_path(wdir);
222 FILE * fd = _wfopen ( wdir, L"r, ccs=UTF-8" );
223 if( fd )
225 fclose( fd );
226 int answer = MessageBox( NULL, L"VLC media player just crashed." \
227 " Do you want to send a bug report to the developers team?",
228 L"VLC crash reporting", MB_YESNO);
230 if(answer == IDYES)
232 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter", INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
233 if(Hint)
235 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org", INTERNET_DEFAULT_FTP_PORT,
236 NULL, NULL, INTERNET_SERVICE_FTP, 0, 0);
237 if(ftp)
239 SYSTEMTIME now;
240 GetSystemTime(&now);
241 wchar_t remote_file[MAX_PATH];
242 swprintf( remote_file, L"/crashs/%04d%02d%02d%02d%02d%02d",now.wYear,
243 now.wMonth, now.wDay, now.wHour, now.wMinute, now.wSecond );
245 FtpPutFile( ftp, wdir, remote_file, FTP_TRANSFER_TYPE_BINARY, 0);
246 InternetCloseHandle(ftp);
248 else
249 fprintf(stderr,"Can't connect to FTP server%d\n",GetLastError());
250 InternetCloseHandle(Hint);
254 _wremove(wdir);
256 free((void *)wdir);
259 /*****************************************************************************
260 * vlc_exception_filter: handles unhandled exceptions, like segfaults
261 *****************************************************************************/
262 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
264 if(IsDebuggerPresent())
266 //If a debugger is present, pass the exception to the debugger with EXCEPTION_CONTINUE_SEARCH
267 return EXCEPTION_CONTINUE_SEARCH;
269 else
271 fprintf( stderr, "unhandled vlc exception\n" );
273 wchar_t * wdir = (wchar_t *)malloc(sizeof(wchar_t)*MAX_PATH);
274 get_crashdump_path(wdir);
275 FILE * fd = _wfopen ( wdir, L"w, ccs=UTF-8" );
276 free((void *)wdir);
278 if( !fd )
280 fprintf( stderr, "\nerror while opening file" );
281 exit( 1 );
284 OSVERSIONINFO osvi;
285 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
286 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
287 GetVersionEx( &osvi );
289 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE, osvi.dwMajorVersion,
290 osvi.dwMinorVersion,
291 osvi.dwBuildNumber,
292 osvi.dwPlatformId,
293 osvi.szCSDVersion);
295 const CONTEXT *const pContext = (const CONTEXT *)lpExceptionInfo->ContextRecord;
296 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)lpExceptionInfo->ExceptionRecord;
297 /*No nested exceptions for now*/
298 fwprintf( fd, L"\n\n[exceptions]\n%08x at %08x",pException->ExceptionCode,
299 pException->ExceptionAddress );
300 if( pException->NumberParameters > 0 )
302 unsigned int i;
303 for( i = 0; i < pException->NumberParameters; i++ )
304 fprintf( fd, " | %08x", pException->ExceptionInformation[i] );
307 fwprintf( fd, L"\n\n[context]\nEDI:%08x\nESI:%08x\n" \
308 "EBX:%08x\nEDX:%08x\nECX:%08x\nEAX:%08x\n" \
309 "EBP:%08x\nEIP:%08x\nESP:%08x\n",
310 pContext->Edi,pContext->Esi,pContext->Ebx,
311 pContext->Edx,pContext->Ecx,pContext->Eax,
312 pContext->Ebp,pContext->Eip,pContext->Esp );
314 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
316 wchar_t module[ 256 ];
317 MEMORY_BASIC_INFORMATION mbi ;
318 VirtualQuery( (DWORD *)pContext->Eip, &mbi, sizeof( mbi ) ) ;
319 HINSTANCE hInstance = mbi.AllocationBase;
320 GetModuleFileName( hInstance, module, 256 ) ;
321 fwprintf( fd, L"%08x|%s\n", pContext->Eip, module );
323 DWORD pEbp = pContext->Ebp;
324 DWORD caller = *((DWORD*)pEbp + 1);
328 VirtualQuery( (DWORD *)caller, &mbi, sizeof( mbi ) ) ;
329 HINSTANCE hInstance = mbi.AllocationBase;
330 GetModuleFileName( hInstance, module, 256 ) ;
331 fwprintf( fd, L"%08x|%s\n", caller, module );
332 pEbp = *(DWORD*)pEbp ;
333 caller = *((DWORD*)pEbp + 1) ;
334 /*The last EBP points to NULL!*/
335 }while(caller);
337 fclose( fd );
338 fflush( stderr );
339 exit( 1 );
342 #endif