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 *****************************************************************************/
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
;
50 static char *FromWide (const wchar_t *wide
)
53 len
= WideCharToMultiByte (CP_UTF8
, 0, wide
, -1, NULL
, 0, NULL
, NULL
);
55 char *out
= (char *)malloc (len
);
57 WideCharToMultiByte (CP_UTF8
, 0, wide
, -1, out
, len
, NULL
, NULL
);
61 int WINAPI
WinMain( HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
67 /* VLC does not change the thread locale, so gettext/libintil will use the
68 * user default locale as reference. */
69 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
70 * if the GETTEXT_MUI environment variable is set. If not set or if running
71 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
72 * putenv() call may become redundant with later versions of gettext. */
73 putenv("GETTEXT_MUI=1");
75 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR
"/modules");
76 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR
"/share");
79 HeapSetInformation(NULL
, HeapEnableTerminationOnCorruption
, NULL
, 0);
81 /* SetProcessDEPPolicy */
82 HINSTANCE h_Kernel32
= LoadLibraryW(L
"kernel32.dll");
85 BOOL (WINAPI
* mySetProcessDEPPolicy
)( DWORD dwFlags
);
86 BOOL (WINAPI
* mySetDllDirectoryA
)(const char* lpPathName
);
87 # define PROCESS_DEP_ENABLE 1
89 mySetProcessDEPPolicy
= (BOOL
WINAPI (*)(DWORD
))
90 GetProcAddress(h_Kernel32
, "SetProcessDEPPolicy");
91 if(mySetProcessDEPPolicy
)
92 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE
);
94 /* Do NOT load any library from cwd. */
95 mySetDllDirectoryA
= (BOOL
WINAPI (*)(const char*))
96 GetProcAddress(h_Kernel32
, "SetDllDirectoryA");
97 if(mySetDllDirectoryA
)
98 mySetDllDirectoryA("");
100 FreeLibrary(h_Kernel32
);
104 wchar_t **wargv
= CommandLineToArgvW (GetCommandLine (), &argc
);
108 char *argv
[argc
+ 3];
109 BOOL crash_handling
= TRUE
;
112 argv
[j
++] = FromWide( L
"--media-library" );
113 argv
[j
++] = FromWide( L
"--no-ignore-config" );
114 for (int i
= 1; i
< argc
; i
++)
116 if(!wcscmp(wargv
[i
], L
"--no-crashdump"))
118 crash_handling
= FALSE
;
119 continue; /* don't give argument to libvlc */
122 argv
[j
++] = FromWide (wargv
[i
]);
131 static wchar_t path
[MAX_PATH
];
132 if( S_OK
!= SHGetFolderPathW( NULL
, CSIDL_APPDATA
| CSIDL_FLAG_CREATE
,
133 NULL
, SHGFP_TYPE_CURRENT
, path
) )
134 fprintf( stderr
, "Can't open the vlc conf PATH\n" );
135 _snwprintf( path
+wcslen( path
), MAX_PATH
, L
"%s", L
"\\vlc\\crashdump" );
136 crashdump_path
= &path
[0];
139 SetUnhandledExceptionFilter(vlc_exception_filter
);
142 _setmode( STDIN_FILENO
, _O_BINARY
); /* Needed for pipes */
144 /* Initialize libvlc */
145 libvlc_instance_t
*vlc
;
146 vlc
= libvlc_new (argc
, (const char **)argv
);
149 libvlc_set_user_agent (vlc
, "VLC media player", "VLC/"PACKAGE_VERSION
);
150 libvlc_add_intf (vlc
, "hotkeys,none");
151 libvlc_add_intf (vlc
, "globalhotkeys,none");
152 libvlc_add_intf (vlc
, NULL
);
153 libvlc_playlist_play (vlc
, -1, 0, NULL
);
155 libvlc_release (vlc
);
158 MessageBox (NULL
, TEXT("VLC media player could not start.\n"
159 "Either the command line options were invalid or no plugins were found.\n"),
160 TEXT("VLC media player"),
164 for (int i
= 0; i
< argc
; i
++)
167 (void)hInstance
; (void)hPrevInstance
; (void)lpCmdLine
; (void)nCmdShow
;
171 /* Crashdumps handling */
172 static void check_crashdump(void)
174 FILE * fd
= _wfopen ( crashdump_path
, L
"r, ccs=UTF-8" );
179 int answer
= MessageBox( NULL
, L
"Ooops: VLC media player just crashed.\n" \
180 "Would you like to send a bug report to the developers team?",
181 L
"VLC crash reporting", MB_YESNO
);
185 HINTERNET Hint
= InternetOpen(L
"VLC Crash Reporter",
186 INTERNET_OPEN_TYPE_PRECONFIG
, NULL
,NULL
,0);
189 HINTERNET ftp
= InternetConnect(Hint
, L
"crash.videolan.org",
190 INTERNET_DEFAULT_FTP_PORT
, NULL
, NULL
,
191 INTERNET_SERVICE_FTP
, INTERNET_FLAG_PASSIVE
, 0);
196 wchar_t remote_file
[MAX_PATH
];
197 _snwprintf(remote_file
, MAX_PATH
,
198 L
"/crashes-win32/%04d%02d%02d%02d%02d%02d",
199 now
.wYear
, now
.wMonth
, now
.wDay
, now
.wHour
,
200 now
.wMinute
, now
.wSecond
);
202 if( FtpPutFile( ftp
, crashdump_path
, remote_file
,
203 FTP_TRANSFER_TYPE_BINARY
, 0) )
204 MessageBox( NULL
, L
"Report sent correctly. Thanks a lot " \
205 "for the help.", L
"Report sent", MB_OK
);
207 MessageBox( NULL
, L
"There was an error while "\
208 "transferring the data to the FTP server.\n"\
209 "Thanks a lot for the help.",
210 L
"Report sending failed", MB_OK
);
211 InternetCloseHandle(ftp
);
215 MessageBox( NULL
, L
"There was an error while connecting to " \
217 "Thanks a lot for the help.",
218 L
"Report sending failed", MB_OK
);
219 fprintf(stderr
,"Can't connect to FTP server 0x%08lu\n",
220 (unsigned long)GetLastError());
222 InternetCloseHandle(Hint
);
226 MessageBox( NULL
, L
"There was an error while connecting to the Internet.\n"\
227 "Thanks a lot for the help anyway.",
228 L
"Report sending failed", MB_OK
);
232 _wremove(crashdump_path
);
235 /*****************************************************************************
236 * vlc_exception_filter: handles unhandled exceptions, like segfaults
237 *****************************************************************************/
238 LONG WINAPI
vlc_exception_filter(struct _EXCEPTION_POINTERS
*lpExceptionInfo
)
240 if(IsDebuggerPresent())
242 //If a debugger is present, pass the exception to the debugger
243 //with EXCEPTION_CONTINUE_SEARCH
244 return EXCEPTION_CONTINUE_SEARCH
;
248 fprintf( stderr
, "unhandled vlc exception\n" );
250 FILE * fd
= _wfopen ( crashdump_path
, L
"w, ccs=UTF-8" );
254 fprintf( stderr
, "\nerror while opening file" );
259 ZeroMemory( &osvi
, sizeof(OSVERSIONINFO
) );
260 osvi
.dwOSVersionInfoSize
= sizeof( OSVERSIONINFO
);
261 GetVersionEx( &osvi
);
263 fwprintf( fd
, L
"[version]\nOS=%d.%d.%d.%d.%s\nVLC=" VERSION_MESSAGE
,
264 osvi
.dwMajorVersion
, osvi
.dwMinorVersion
, osvi
.dwBuildNumber
,
265 osvi
.dwPlatformId
, osvi
.szCSDVersion
);
267 const CONTEXT
*const pContext
= (const CONTEXT
*)
268 lpExceptionInfo
->ContextRecord
;
269 const EXCEPTION_RECORD
*const pException
= (const EXCEPTION_RECORD
*)
270 lpExceptionInfo
->ExceptionRecord
;
271 /* No nested exceptions for now */
272 fwprintf( fd
, L
"\n\n[exceptions]\n%08x at %px",
273 pException
->ExceptionCode
, pException
->ExceptionAddress
);
275 for( unsigned int i
= 0; i
< pException
->NumberParameters
; i
++ )
276 fwprintf( fd
, L
" | %p", pException
->ExceptionInformation
[i
] );
279 fwprintf( fd
, L
"\n\n[context]\nRDI:%px\nRSI:%px\n" \
280 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
281 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
282 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
283 "R13:%px\nR14:%px\nR15:%px\n",
284 pContext
->Rdi
,pContext
->Rsi
,pContext
->Rbx
,
285 pContext
->Rdx
,pContext
->Rcx
,pContext
->Rax
,
286 pContext
->Rbp
,pContext
->Rip
,pContext
->Rsp
,
287 pContext
->R8
,pContext
->R9
,pContext
->R10
,
288 pContext
->R11
,pContext
->R12
,pContext
->R13
,
289 pContext
->R14
,pContext
->R15
);
291 fwprintf( fd
, L
"\n\n[context]\nEDI:%px\nESI:%px\n" \
292 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
293 "EBP:%px\nEIP:%px\nESP:%px\n",
294 pContext
->Edi
,pContext
->Esi
,pContext
->Ebx
,
295 pContext
->Edx
,pContext
->Ecx
,pContext
->Eax
,
296 pContext
->Ebp
,pContext
->Eip
,pContext
->Esp
);
299 fwprintf( fd
, L
"\n[stacktrace]\n#EIP|base|module\n" );
302 LPCVOID caller
= (LPCVOID
)pContext
->Rip
;
303 LPVOID
*pBase
= (LPVOID
*)pContext
->Rbp
;
305 LPVOID
*pBase
= (LPVOID
*)pContext
->Ebp
;
306 LPCVOID caller
= (LPCVOID
)pContext
->Eip
;
308 for( unsigned frame
= 0; frame
<= 100; frame
++ )
310 MEMORY_BASIC_INFORMATION mbi
;
311 wchar_t module
[ 256 ];
312 VirtualQuery( caller
, &mbi
, sizeof( mbi
) ) ;
313 GetModuleFileName( mbi
.AllocationBase
, module
, 256 );
314 fwprintf( fd
, L
"%p|%s\n", caller
, module
);
316 /*The last BP points to NULL!*/
317 caller
= *(pBase
+ 1);
323 HANDLE hpid
= OpenProcess(PROCESS_QUERY_INFORMATION
| PROCESS_VM_READ
,
324 FALSE
, GetCurrentProcessId());
328 if (EnumProcessModules(hpid
, mods
, sizeof(mods
), &size
)) {
329 fwprintf( fd
, L
"\n\n[modules]\n" );
330 for (unsigned int i
= 0; i
< size
/ sizeof(HMODULE
); i
++) {
331 wchar_t module
[ 256 ];
332 GetModuleFileName(mods
[i
], module
, 256);
333 fwprintf( fd
, L
"%p|%s\n", mods
[i
], module
);