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 *****************************************************************************/
40 # define _WIN32_IE 0x501
46 #define PSAPI_VERSION 1
48 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
49 static void check_crashdump(void);
50 LONG WINAPI
vlc_exception_filter(struct _EXCEPTION_POINTERS
*lpExceptionInfo
);
51 static const wchar_t *crashdump_path
;
53 static char *FromWide (const wchar_t *wide
)
56 len
= WideCharToMultiByte (CP_UTF8
, 0, wide
, -1, NULL
, 0, NULL
, NULL
);
58 char *out
= (char *)malloc (len
);
60 WideCharToMultiByte (CP_UTF8
, 0, wide
, -1, out
, len
, NULL
, NULL
);
64 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
65 static BOOL
SetDefaultDllDirectories_(DWORD flags
)
67 HMODULE h
= GetModuleHandle(TEXT("kernel32.dll"));
71 BOOL (WINAPI
* SetDefaultDllDirectoriesReal
)(DWORD
);
73 SetDefaultDllDirectoriesReal
= GetProcAddress(h
,
74 "SetDefaultDllDirectories");
75 if (SetDefaultDllDirectoriesReal
== NULL
)
78 return SetDefaultDllDirectoriesReal(flags
);
80 # define SetDefaultDllDirectories SetDefaultDllDirectories_
84 static void PrioritizeSystem32(void)
86 #ifndef HAVE_PROCESS_MITIGATION_IMAGE_LOAD_POLICY
87 typedef struct _PROCESS_MITIGATION_IMAGE_LOAD_POLICY
{
91 DWORD NoRemoteImages
:1;
92 DWORD NoLowMandatoryLabelImages
:1;
93 DWORD PreferSystem32Images
:1;
94 DWORD ReservedFlags
:29;
97 } PROCESS_MITIGATION_IMAGE_LOAD_POLICY
;
99 #if _WIN32_WINNT < _WIN32_WINNT_WIN8
100 BOOL
WINAPI (*SetProcessMitigationPolicy
)(PROCESS_MITIGATION_POLICY
, PVOID
, SIZE_T
);
101 HINSTANCE h_Kernel32
= GetModuleHandle(TEXT("kernel32.dll"));
104 SetProcessMitigationPolicy
= (BOOL (WINAPI
*)(PROCESS_MITIGATION_POLICY
, PVOID
, SIZE_T
))
105 GetProcAddress(h_Kernel32
, "SetProcessMitigationPolicy");
106 if (SetProcessMitigationPolicy
== NULL
)
109 PROCESS_MITIGATION_IMAGE_LOAD_POLICY m
= { .Flags
= 0 };
110 m
.PreferSystem32Images
= 1;
111 SetProcessMitigationPolicy( 10 /* ProcessImageLoadPolicy */, &m
, sizeof( m
) );
114 int WINAPI
WinMain( HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
120 /* VLC does not change the thread locale, so gettext/libintil will use the
121 * user default locale as reference. */
122 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
123 * if the GETTEXT_MUI environment variable is set. If not set or if running
124 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
125 * putenv() call may become redundant with later versions of gettext. */
126 putenv("GETTEXT_MUI=1");
128 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR
"/modules");
129 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR
"/share");
133 /* Disable stderr buffering. Indeed, stderr can be buffered on Windows (if
134 * connected to a pipe). */
135 setvbuf (stderr
, NULL
, _IONBF
, BUFSIZ
);
138 HeapSetInformation(NULL
, HeapEnableTerminationOnCorruption
, NULL
, 0);
140 /* SetProcessDEPPolicy, SetDllDirectory, & Co. */
141 HINSTANCE h_Kernel32
= GetModuleHandle(TEXT("kernel32.dll"));
142 if (h_Kernel32
!= NULL
)
145 # define PROCESS_DEP_ENABLE 1
146 BOOL (WINAPI
* mySetProcessDEPPolicy
)( DWORD dwFlags
);
147 mySetProcessDEPPolicy
= (BOOL (WINAPI
*)(DWORD
))
148 GetProcAddress(h_Kernel32
, "SetProcessDEPPolicy");
149 if(mySetProcessDEPPolicy
)
150 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE
);
154 /* Do NOT load any library from cwd. */
155 SetDllDirectory(TEXT(""));
158 * The LoadLibrary* calls from the modules and the 3rd party code
159 * will search in SYSTEM32 only
161 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32
);
163 * Load DLLs from system32 before any other folder (when possible)
165 PrioritizeSystem32();
168 wchar_t **wargv
= CommandLineToArgvW (GetCommandLine (), &argc
);
172 char *argv
[argc
+ 3];
173 BOOL crash_handling
= TRUE
;
177 argv
[j
++] = FromWide( L
"--media-library" );
178 argv
[j
++] = FromWide( L
"--no-ignore-config" );
179 for (int i
= 1; i
< argc
; i
++)
181 if(!wcscmp(wargv
[i
], L
"--no-crashdump"))
183 crash_handling
= FALSE
;
184 continue; /* don't give argument to libvlc */
186 if (!wcsncmp(wargv
[i
], L
"--language", 10) )
188 if (i
< argc
- 1 && wcsncmp( wargv
[i
+ 1], L
"--", 2 ))
189 lang
= FromWide (wargv
[++i
]);
193 argv
[j
++] = FromWide (wargv
[i
]);
202 static wchar_t path
[MAX_PATH
];
203 if( S_OK
!= SHGetFolderPathW( NULL
, CSIDL_APPDATA
| CSIDL_FLAG_CREATE
,
204 NULL
, SHGFP_TYPE_CURRENT
, path
) )
205 fprintf( stderr
, "Can't open the vlc conf PATH\n" );
206 _snwprintf( path
+wcslen( path
), MAX_PATH
, L
"%s", L
"\\vlc\\crashdump" );
207 crashdump_path
= &path
[0];
210 SetUnhandledExceptionFilter(vlc_exception_filter
);
213 _setmode( _fileno( stdin
), _O_BINARY
); /* Needed for pipes */
219 if( RegOpenKeyEx( HKEY_CURRENT_USER
, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ
, &h_key
)
224 if( RegQueryValueEx( h_key
, TEXT("Lang"), NULL
, NULL
, (LPBYTE
) &szData
, &len
) == ERROR_SUCCESS
)
225 lang
= FromWide( szData
);
229 if (lang
&& strncmp( lang
, "auto", 4 ) )
232 snprintf(tmp
, 11, "LANG=%s", lang
);
237 /* Initialize libvlc */
238 libvlc_instance_t
*vlc
;
239 vlc
= libvlc_new (argc
, (const char **)argv
);
242 libvlc_set_app_id (vlc
, "org.VideoLAN.VLC", PACKAGE_VERSION
,
244 libvlc_set_user_agent (vlc
, "VLC media player", "VLC/"PACKAGE_VERSION
);
245 libvlc_add_intf (vlc
, "hotkeys,none");
246 libvlc_add_intf (vlc
, "globalhotkeys,none");
247 libvlc_add_intf (vlc
, NULL
);
248 libvlc_playlist_play (vlc
, -1, 0, NULL
);
250 libvlc_release (vlc
);
253 MessageBox (NULL
, TEXT("VLC media player could not start.\n"
254 "Either the command line options were invalid or no plugins were found.\n"),
255 TEXT("VLC media player"),
259 for (int i
= 0; i
< argc
; i
++)
262 (void)hInstance
; (void)hPrevInstance
; (void)lpCmdLine
; (void)nCmdShow
;
266 /* Crashdumps handling */
267 static void check_crashdump(void)
269 wchar_t mv_crashdump_path
[MAX_PATH
];
270 wcscpy (mv_crashdump_path
, crashdump_path
);
271 wcscat (mv_crashdump_path
, L
".mv");
273 if (_wrename (crashdump_path
, mv_crashdump_path
))
276 FILE * fd
= _wfopen ( mv_crashdump_path
, L
"r, ccs=UTF-8" );
281 int answer
= MessageBox( NULL
, L
"Ooops: VLC media player just crashed.\n" \
282 "Would you like to send a bug report to the developers team?",
283 L
"VLC crash reporting", MB_YESNO
);
287 HMODULE hWininet
= LoadLibrary(TEXT("wininet.dll"));
288 if (hWininet
== NULL
)
290 fprintf(stderr
, "There was an error loading the network"
291 " 0x%08lx\n", (unsigned long)GetLastError());
295 HINTERNET (WINAPI
*InternetOpenW_
)(LPCWSTR
,DWORD dwAccessType
,LPCWSTR lpszProxy
,LPCWSTR lpszProxyBypass
,DWORD dwFlags
);
296 HINTERNET (WINAPI
*InternetConnectW_
)(HINTERNET hInternet
,LPCWSTR lpszServerName
,INTERNET_PORT nServerPort
,LPCWSTR lpszUserName
,LPCWSTR lpszPassword
,DWORD dwService
,DWORD dwFlags
,DWORD_PTR dwContext
);
297 BOOL (WINAPI
*InternetCloseHandle_
)(HINTERNET hInternet
);
298 BOOL (WINAPI
*FtpPutFileW_
)(HINTERNET hConnect
,LPCWSTR lpszLocalFile
,LPCWSTR lpszNewRemoteFile
,DWORD dwFlags
,DWORD_PTR dwContext
);
299 InternetOpenW_
= (void*)GetProcAddress(hWininet
, "InternetOpenW");
300 InternetConnectW_
= (void*)GetProcAddress(hWininet
, "InternetConnectW");
301 InternetCloseHandle_
= (void*)GetProcAddress(hWininet
, "InternetCloseHandle");
302 FtpPutFileW_
= (void*)GetProcAddress(hWininet
, "FtpPutFileW");
303 if (!InternetOpenW_
|| !InternetConnectW_
|| !InternetCloseHandle_
|| !FtpPutFileW_
)
305 fprintf(stderr
, "There was an error loading the network API entries"
306 " 0x%08lx\n", (unsigned long)GetLastError());
310 HINTERNET Hint
= InternetOpenW_(L
"VLC Crash Reporter",
311 INTERNET_OPEN_TYPE_PRECONFIG
, NULL
,NULL
,0);
314 HINTERNET ftp
= InternetConnectW_(Hint
, L
"crash.videolan.org",
315 INTERNET_DEFAULT_FTP_PORT
, NULL
, NULL
,
316 INTERNET_SERVICE_FTP
, INTERNET_FLAG_PASSIVE
, 0);
321 wchar_t remote_file
[MAX_PATH
];
322 _snwprintf(remote_file
, MAX_PATH
,
323 L
"/crashes-win32/%04d%02d%02d%02d%02d%02d",
324 now
.wYear
, now
.wMonth
, now
.wDay
, now
.wHour
,
325 now
.wMinute
, now
.wSecond
);
327 if( FtpPutFileW_( ftp
, mv_crashdump_path
, remote_file
,
328 FTP_TRANSFER_TYPE_BINARY
, 0) )
329 fprintf(stderr
, "Report sent correctly to FTP.\n");
331 fprintf(stderr
,"Couldn't send report to FTP server\n");
333 InternetCloseHandle_(ftp
);
337 fprintf(stderr
, "Can't connect to FTP server 0x%08lx\n",
338 (unsigned long)GetLastError());
340 InternetCloseHandle_(Hint
);
344 fprintf(stderr
, "There was an error while connecting to the "
345 "Internet 0x%08lx\n", (unsigned long)GetLastError());
348 if (hWininet
!= NULL
)
349 FreeLibrary(hWininet
);
350 MessageBox( NULL
, L
"Thanks a lot for helping improving VLC!",
351 L
"VLC crash report" , MB_OK
);
354 _wremove(mv_crashdump_path
);
357 /*****************************************************************************
358 * vlc_exception_filter: handles unhandled exceptions, like segfaults
359 *****************************************************************************/
360 LONG WINAPI
vlc_exception_filter(struct _EXCEPTION_POINTERS
*lpExceptionInfo
)
362 if(IsDebuggerPresent())
364 //If a debugger is present, pass the exception to the debugger
365 //with EXCEPTION_CONTINUE_SEARCH
366 return EXCEPTION_CONTINUE_SEARCH
;
370 fprintf( stderr
, "unhandled vlc exception\n" );
372 FILE * fd
= _wfopen ( crashdump_path
, L
"w, ccs=UTF-8" );
376 fprintf( stderr
, "\nerror while opening file" );
381 ZeroMemory( &osvi
, sizeof(OSVERSIONINFO
) );
382 osvi
.dwOSVersionInfoSize
= sizeof( OSVERSIONINFO
);
383 GetVersionEx( &osvi
);
385 fwprintf( fd
, L
"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE
,
386 osvi
.dwMajorVersion
, osvi
.dwMinorVersion
, osvi
.dwBuildNumber
,
387 osvi
.dwPlatformId
, osvi
.szCSDVersion
);
389 const CONTEXT
*const pContext
= (const CONTEXT
*)
390 lpExceptionInfo
->ContextRecord
;
391 const EXCEPTION_RECORD
*const pException
= (const EXCEPTION_RECORD
*)
392 lpExceptionInfo
->ExceptionRecord
;
393 /* No nested exceptions for now */
394 fwprintf( fd
, L
"\n\n[exceptions]\n%08x at %px",
395 pException
->ExceptionCode
, pException
->ExceptionAddress
);
397 for( unsigned int i
= 0; i
< pException
->NumberParameters
; i
++ )
398 fwprintf( fd
, L
" | %p", pException
->ExceptionInformation
[i
] );
401 fwprintf( fd
, L
"\n\n[context]\nRDI:%px\nRSI:%px\n" \
402 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
403 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
404 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
405 "R13:%px\nR14:%px\nR15:%px\n",
406 pContext
->Rdi
,pContext
->Rsi
,pContext
->Rbx
,
407 pContext
->Rdx
,pContext
->Rcx
,pContext
->Rax
,
408 pContext
->Rbp
,pContext
->Rip
,pContext
->Rsp
,
409 pContext
->R8
,pContext
->R9
,pContext
->R10
,
410 pContext
->R11
,pContext
->R12
,pContext
->R13
,
411 pContext
->R14
,pContext
->R15
);
413 fwprintf( fd
, L
"\n\n[context]\nEDI:%px\nESI:%px\n" \
414 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
415 "EBP:%px\nEIP:%px\nESP:%px\n",
416 pContext
->Edi
,pContext
->Esi
,pContext
->Ebx
,
417 pContext
->Edx
,pContext
->Ecx
,pContext
->Eax
,
418 pContext
->Ebp
,pContext
->Eip
,pContext
->Esp
);
421 fwprintf( fd
, L
"\n[stacktrace]\n#EIP|base|module\n" );
424 LPCVOID caller
= (LPCVOID
)pContext
->Rip
;
425 LPVOID
*pBase
= (LPVOID
*)pContext
->Rbp
;
427 LPVOID
*pBase
= (LPVOID
*)pContext
->Ebp
;
428 LPCVOID caller
= (LPCVOID
)pContext
->Eip
;
430 for( unsigned frame
= 0; frame
<= 100; frame
++ )
432 MEMORY_BASIC_INFORMATION mbi
;
433 wchar_t module
[ 256 ];
434 VirtualQuery( caller
, &mbi
, sizeof( mbi
) ) ;
435 GetModuleFileName( mbi
.AllocationBase
, module
, 256 );
436 fwprintf( fd
, L
"%p|%ls\n", caller
, module
);
438 if( IsBadReadPtr( pBase
, 2 * sizeof( void* ) ) )
441 /*The last BP points to NULL!*/
442 caller
= *(pBase
+ 1);
450 HANDLE hpid
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
,
451 FALSE
, GetCurrentProcessId());
455 if (EnumProcessModules(hpid
, mods
, sizeof(mods
), &size
)) {
456 fwprintf( fd
, L
"\n\n[modules]\n" );
457 for (unsigned int i
= 0; i
< size
/ sizeof(HMODULE
); i
++) {
458 wchar_t module
[ 256 ];
459 GetModuleFileName(mods
[i
], module
, 256);
460 fwprintf( fd
, L
"%p|%ls\n", mods
[i
], module
);