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");
132 SetErrorMode(SEM_FAILCRITICALERRORS
);
133 HeapSetInformation(NULL
, HeapEnableTerminationOnCorruption
, NULL
, 0);
135 /* SetProcessDEPPolicy, SetDllDirectory, & Co. */
136 HINSTANCE h_Kernel32
= GetModuleHandle(TEXT("kernel32.dll"));
137 if (h_Kernel32
!= NULL
)
140 # define PROCESS_DEP_ENABLE 1
141 BOOL (WINAPI
* mySetProcessDEPPolicy
)( DWORD dwFlags
);
142 mySetProcessDEPPolicy
= (BOOL (WINAPI
*)(DWORD
))
143 GetProcAddress(h_Kernel32
, "SetProcessDEPPolicy");
144 if(mySetProcessDEPPolicy
)
145 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE
);
147 /* Do NOT load any library from cwd. */
148 BOOL (WINAPI
* mySetDllDirectoryA
)(const char* lpPathName
);
149 mySetDllDirectoryA
= (BOOL (WINAPI
*)(const char*))
150 GetProcAddress(h_Kernel32
, "SetDllDirectoryA");
151 if(mySetDllDirectoryA
)
152 mySetDllDirectoryA("");
156 * The LoadLibrary* calls from the modules and the 3rd party code
157 * will search in SYSTEM32 only
159 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32
);
161 * Load DLLs from system32 before any other folder (when possible)
163 PrioritizeSystem32();
166 wchar_t **wargv
= CommandLineToArgvW (GetCommandLine (), &argc
);
170 char *argv
[argc
+ 3];
171 BOOL crash_handling
= TRUE
;
175 argv
[j
++] = FromWide( L
"--media-library" );
176 argv
[j
++] = FromWide( L
"--no-ignore-config" );
177 for (int i
= 1; i
< argc
; i
++)
179 if(!wcscmp(wargv
[i
], L
"--no-crashdump"))
181 crash_handling
= FALSE
;
182 continue; /* don't give argument to libvlc */
184 if (!wcsncmp(wargv
[i
], L
"--language", 10) )
186 if (i
< argc
- 1 && wcsncmp( wargv
[i
+ 1], L
"--", 2 ))
187 lang
= FromWide (wargv
[++i
]);
191 argv
[j
++] = FromWide (wargv
[i
]);
200 static wchar_t path
[MAX_PATH
];
201 if( S_OK
!= SHGetFolderPathW( NULL
, CSIDL_APPDATA
| CSIDL_FLAG_CREATE
,
202 NULL
, SHGFP_TYPE_CURRENT
, path
) )
203 fprintf( stderr
, "Can't open the vlc conf PATH\n" );
204 _snwprintf( path
+wcslen( path
), MAX_PATH
, L
"%s", L
"\\vlc\\crashdump" );
205 crashdump_path
= &path
[0];
208 SetUnhandledExceptionFilter(vlc_exception_filter
);
211 _setmode( _fileno( stdin
), _O_BINARY
); /* Needed for pipes */
217 if( RegOpenKeyEx( HKEY_CURRENT_USER
, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ
, &h_key
)
222 if( RegQueryValueEx( h_key
, TEXT("Lang"), NULL
, NULL
, (LPBYTE
) &szData
, &len
) == ERROR_SUCCESS
)
223 lang
= FromWide( szData
);
227 if (lang
&& strncmp( lang
, "auto", 4 ) )
230 snprintf(tmp
, 11, "LANG=%s", lang
);
235 /* Initialize libvlc */
236 libvlc_instance_t
*vlc
;
237 vlc
= libvlc_new (argc
, (const char **)argv
);
240 libvlc_set_app_id (vlc
, "org.VideoLAN.VLC", PACKAGE_VERSION
,
242 libvlc_set_user_agent (vlc
, "VLC media player", "VLC/"PACKAGE_VERSION
);
243 libvlc_add_intf (vlc
, "hotkeys,none");
244 libvlc_add_intf (vlc
, "globalhotkeys,none");
245 libvlc_add_intf (vlc
, NULL
);
246 libvlc_playlist_play (vlc
, -1, 0, NULL
);
248 libvlc_release (vlc
);
251 MessageBox (NULL
, TEXT("VLC media player could not start.\n"
252 "Either the command line options were invalid or no plugins were found.\n"),
253 TEXT("VLC media player"),
257 for (int i
= 0; i
< argc
; i
++)
260 (void)hInstance
; (void)hPrevInstance
; (void)lpCmdLine
; (void)nCmdShow
;
264 /* Crashdumps handling */
265 static void check_crashdump(void)
267 wchar_t mv_crashdump_path
[MAX_PATH
];
268 wcscpy (mv_crashdump_path
, crashdump_path
);
269 wcscat (mv_crashdump_path
, L
".mv");
271 if (_wrename (crashdump_path
, mv_crashdump_path
))
274 FILE * fd
= _wfopen ( mv_crashdump_path
, L
"r, ccs=UTF-8" );
279 int answer
= MessageBox( NULL
, L
"Ooops: VLC media player just crashed.\n" \
280 "Would you like to send a bug report to the developers team?",
281 L
"VLC crash reporting", MB_YESNO
);
285 HMODULE hWininet
= LoadLibrary(TEXT("wininet.dll"));
286 if (hWininet
== NULL
)
288 fprintf(stderr
, "There was an error loading the network"
289 " 0x%08lx\n", (unsigned long)GetLastError());
293 HINTERNET (WINAPI
*InternetOpenW_
)(LPCWSTR
,DWORD dwAccessType
,LPCWSTR lpszProxy
,LPCWSTR lpszProxyBypass
,DWORD dwFlags
);
294 HINTERNET (WINAPI
*InternetConnectW_
)(HINTERNET hInternet
,LPCWSTR lpszServerName
,INTERNET_PORT nServerPort
,LPCWSTR lpszUserName
,LPCWSTR lpszPassword
,DWORD dwService
,DWORD dwFlags
,DWORD_PTR dwContext
);
295 BOOL (WINAPI
*InternetCloseHandle_
)(HINTERNET hInternet
);
296 BOOL (WINAPI
*FtpPutFileW_
)(HINTERNET hConnect
,LPCWSTR lpszLocalFile
,LPCWSTR lpszNewRemoteFile
,DWORD dwFlags
,DWORD_PTR dwContext
);
297 InternetOpenW_
= (void*)GetProcAddress(hWininet
, "InternetOpenW");
298 InternetConnectW_
= (void*)GetProcAddress(hWininet
, "InternetConnectW");
299 InternetCloseHandle_
= (void*)GetProcAddress(hWininet
, "InternetCloseHandle");
300 FtpPutFileW_
= (void*)GetProcAddress(hWininet
, "FtpPutFileW");
301 if (!InternetOpenW_
|| !InternetConnectW_
|| !InternetCloseHandle_
|| !FtpPutFileW_
)
303 fprintf(stderr
, "There was an error loading the network API entries"
304 " 0x%08lx\n", (unsigned long)GetLastError());
308 HINTERNET Hint
= InternetOpenW_(L
"VLC Crash Reporter",
309 INTERNET_OPEN_TYPE_PRECONFIG
, NULL
,NULL
,0);
312 HINTERNET ftp
= InternetConnectW_(Hint
, L
"crash.videolan.org",
313 INTERNET_DEFAULT_FTP_PORT
, NULL
, NULL
,
314 INTERNET_SERVICE_FTP
, INTERNET_FLAG_PASSIVE
, 0);
319 wchar_t remote_file
[MAX_PATH
];
320 _snwprintf(remote_file
, MAX_PATH
,
321 L
"/crashes-win32/%04d%02d%02d%02d%02d%02d",
322 now
.wYear
, now
.wMonth
, now
.wDay
, now
.wHour
,
323 now
.wMinute
, now
.wSecond
);
325 if( FtpPutFileW_( ftp
, mv_crashdump_path
, remote_file
,
326 FTP_TRANSFER_TYPE_BINARY
, 0) )
327 fprintf(stderr
, "Report sent correctly to FTP.\n");
329 fprintf(stderr
,"Couldn't send report to FTP server\n");
331 InternetCloseHandle_(ftp
);
335 fprintf(stderr
, "Can't connect to FTP server 0x%08lx\n",
336 (unsigned long)GetLastError());
338 InternetCloseHandle_(Hint
);
342 fprintf(stderr
, "There was an error while connecting to the "
343 "Internet 0x%08lx\n", (unsigned long)GetLastError());
346 if (hWininet
!= NULL
)
347 FreeLibrary(hWininet
);
348 MessageBox( NULL
, L
"Thanks a lot for helping improving VLC!",
349 L
"VLC crash report" , MB_OK
);
352 _wremove(mv_crashdump_path
);
355 /*****************************************************************************
356 * vlc_exception_filter: handles unhandled exceptions, like segfaults
357 *****************************************************************************/
358 LONG WINAPI
vlc_exception_filter(struct _EXCEPTION_POINTERS
*lpExceptionInfo
)
360 if(IsDebuggerPresent())
362 //If a debugger is present, pass the exception to the debugger
363 //with EXCEPTION_CONTINUE_SEARCH
364 return EXCEPTION_CONTINUE_SEARCH
;
368 fprintf( stderr
, "unhandled vlc exception\n" );
370 FILE * fd
= _wfopen ( crashdump_path
, L
"w, ccs=UTF-8" );
374 fprintf( stderr
, "\nerror while opening file" );
379 ZeroMemory( &osvi
, sizeof(OSVERSIONINFO
) );
380 osvi
.dwOSVersionInfoSize
= sizeof( OSVERSIONINFO
);
381 GetVersionEx( &osvi
);
383 fwprintf( fd
, L
"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE
,
384 osvi
.dwMajorVersion
, osvi
.dwMinorVersion
, osvi
.dwBuildNumber
,
385 osvi
.dwPlatformId
, osvi
.szCSDVersion
);
387 const CONTEXT
*const pContext
= (const CONTEXT
*)
388 lpExceptionInfo
->ContextRecord
;
389 const EXCEPTION_RECORD
*const pException
= (const EXCEPTION_RECORD
*)
390 lpExceptionInfo
->ExceptionRecord
;
391 /* No nested exceptions for now */
392 fwprintf( fd
, L
"\n\n[exceptions]\n%08x at %px",
393 pException
->ExceptionCode
, pException
->ExceptionAddress
);
395 for( unsigned int i
= 0; i
< pException
->NumberParameters
; i
++ )
396 fwprintf( fd
, L
" | %p", pException
->ExceptionInformation
[i
] );
399 fwprintf( fd
, L
"\n\n[context]\nRDI:%px\nRSI:%px\n" \
400 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
401 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
402 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
403 "R13:%px\nR14:%px\nR15:%px\n",
404 pContext
->Rdi
,pContext
->Rsi
,pContext
->Rbx
,
405 pContext
->Rdx
,pContext
->Rcx
,pContext
->Rax
,
406 pContext
->Rbp
,pContext
->Rip
,pContext
->Rsp
,
407 pContext
->R8
,pContext
->R9
,pContext
->R10
,
408 pContext
->R11
,pContext
->R12
,pContext
->R13
,
409 pContext
->R14
,pContext
->R15
);
411 fwprintf( fd
, L
"\n\n[context]\nEDI:%px\nESI:%px\n" \
412 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
413 "EBP:%px\nEIP:%px\nESP:%px\n",
414 pContext
->Edi
,pContext
->Esi
,pContext
->Ebx
,
415 pContext
->Edx
,pContext
->Ecx
,pContext
->Eax
,
416 pContext
->Ebp
,pContext
->Eip
,pContext
->Esp
);
419 fwprintf( fd
, L
"\n[stacktrace]\n#EIP|base|module\n" );
422 LPCVOID caller
= (LPCVOID
)pContext
->Rip
;
423 LPVOID
*pBase
= (LPVOID
*)pContext
->Rbp
;
425 LPVOID
*pBase
= (LPVOID
*)pContext
->Ebp
;
426 LPCVOID caller
= (LPCVOID
)pContext
->Eip
;
428 for( unsigned frame
= 0; frame
<= 100; frame
++ )
430 MEMORY_BASIC_INFORMATION mbi
;
431 wchar_t module
[ 256 ];
432 VirtualQuery( caller
, &mbi
, sizeof( mbi
) ) ;
433 GetModuleFileName( mbi
.AllocationBase
, module
, 256 );
434 fwprintf( fd
, L
"%p|%ls\n", caller
, module
);
436 if( IsBadReadPtr( pBase
, 2 * sizeof( void* ) ) )
439 /*The last BP points to NULL!*/
440 caller
= *(pBase
+ 1);
448 HANDLE hpid
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
,
449 FALSE
, GetCurrentProcessId());
453 if (EnumProcessModules(hpid
, mods
, sizeof(mods
), &size
)) {
454 fwprintf( fd
, L
"\n\n[modules]\n" );
455 for (unsigned int i
= 0; i
< size
/ sizeof(HMODULE
); i
++) {
456 wchar_t module
[ 256 ];
457 GetModuleFileName(mods
[i
], module
, 256);
458 fwprintf( fd
, L
"%p|%ls\n", mods
[i
], module
);