core: WINAPI should be set on the pointer
[vlc.git] / bin / winvlc.c
blob05cbe750aefa83855711cb8cca0bb096d9d6fa4f
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 *****************************************************************************/
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
31 #ifndef UNICODE
32 #define UNICODE
33 #endif
35 #include <vlc/vlc.h>
36 #include <windows.h>
37 #include <shellapi.h>
39 #ifndef _WIN32_IE
40 # define _WIN32_IE 0x501
41 #endif
42 #include <fcntl.h>
43 #include <io.h>
44 #include <shlobj.h>
45 #include <wininet.h>
46 #define PSAPI_VERSION 1
47 #include <psapi.h>
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)
55 size_t len;
56 len = WideCharToMultiByte (CP_UTF8, 0, wide, -1, NULL, 0, NULL, NULL);
58 char *out = (char *)malloc (len);
59 if (out)
60 WideCharToMultiByte (CP_UTF8, 0, wide, -1, out, len, NULL, NULL);
61 return out;
64 #if (_WIN32_WINNT < _WIN32_WINNT_WIN8)
65 static BOOL SetDefaultDllDirectories_(DWORD flags)
67 HMODULE h = GetModuleHandle(TEXT("kernel32.dll"));
68 if (h == NULL)
69 return FALSE;
71 BOOL (WINAPI * SetDefaultDllDirectoriesReal)(DWORD);
73 SetDefaultDllDirectoriesReal = GetProcAddress(h,
74 "SetDefaultDllDirectories");
75 if (SetDefaultDllDirectoriesReal == NULL)
76 return FALSE;
78 return SetDefaultDllDirectoriesReal(flags);
80 # define SetDefaultDllDirectories SetDefaultDllDirectories_
81 #endif
83 int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
84 LPSTR lpCmdLine,
85 int nCmdShow )
87 int argc;
89 /* VLC does not change the thread locale, so gettext/libintil will use the
90 * user default locale as reference. */
91 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
92 * if the GETTEXT_MUI environment variable is set. If not set or if running
93 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
94 * putenv() call may become redundant with later versions of gettext. */
95 putenv("GETTEXT_MUI=1");
96 #ifdef TOP_BUILDDIR
97 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR"/modules");
98 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR"/share");
99 #endif
101 SetErrorMode(SEM_FAILCRITICALERRORS);
102 HeapSetInformation(NULL, HeapEnableTerminationOnCorruption, NULL, 0);
104 /* SetProcessDEPPolicy */
105 HINSTANCE h_Kernel32 = GetModuleHandle(TEXT("kernel32.dll"));
106 if (h_Kernel32 != NULL)
108 BOOL (WINAPI * mySetProcessDEPPolicy)( DWORD dwFlags);
109 BOOL (WINAPI * mySetDllDirectoryA)(const char* lpPathName);
110 # define PROCESS_DEP_ENABLE 1
112 mySetProcessDEPPolicy = (BOOL (WINAPI *)(DWORD))
113 GetProcAddress(h_Kernel32, "SetProcessDEPPolicy");
114 if(mySetProcessDEPPolicy)
115 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE);
117 /* Do NOT load any library from cwd. */
118 mySetDllDirectoryA = (BOOL (WINAPI *)(const char*))
119 GetProcAddress(h_Kernel32, "SetDllDirectoryA");
120 if(mySetDllDirectoryA)
121 mySetDllDirectoryA("");
124 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32);
126 /* Args */
127 wchar_t **wargv = CommandLineToArgvW (GetCommandLine (), &argc);
128 if (wargv == NULL)
129 return 1;
131 char *argv[argc + 3];
132 BOOL crash_handling = TRUE;
133 int j = 0;
134 char *lang = NULL;
136 argv[j++] = FromWide( L"--media-library" );
137 argv[j++] = FromWide( L"--no-ignore-config" );
138 for (int i = 1; i < argc; i++)
140 if(!wcscmp(wargv[i], L"--no-crashdump"))
142 crash_handling = FALSE;
143 continue; /* don't give argument to libvlc */
145 if (!wcsncmp(wargv[i], L"--language", 10) )
147 if (i < argc - 1 && wcsncmp( wargv[i + 1], L"--", 2 ))
148 lang = FromWide (wargv[++i]);
149 continue;
152 argv[j++] = FromWide (wargv[i]);
155 argc = j;
156 argv[argc] = NULL;
157 LocalFree (wargv);
159 if(crash_handling)
161 static wchar_t path[MAX_PATH];
162 if( S_OK != SHGetFolderPathW( NULL, CSIDL_APPDATA | CSIDL_FLAG_CREATE,
163 NULL, SHGFP_TYPE_CURRENT, path ) )
164 fprintf( stderr, "Can't open the vlc conf PATH\n" );
165 _snwprintf( path+wcslen( path ), MAX_PATH, L"%s", L"\\vlc\\crashdump" );
166 crashdump_path = &path[0];
168 check_crashdump();
169 SetUnhandledExceptionFilter(vlc_exception_filter);
172 _setmode( _fileno( stdin ), _O_BINARY ); /* Needed for pipes */
174 /* */
175 if (!lang)
177 HKEY h_key;
178 if( RegOpenKeyEx( HKEY_CURRENT_USER, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ, &h_key )
179 == ERROR_SUCCESS )
181 TCHAR szData[256];
182 DWORD len = 256;
183 if( RegQueryValueEx( h_key, TEXT("Lang"), NULL, NULL, (LPBYTE) &szData, &len ) == ERROR_SUCCESS )
184 lang = FromWide( szData );
188 if (lang && strncmp( lang, "auto", 4 ) )
190 char tmp[11];
191 snprintf(tmp, 11, "LANG=%s", lang);
192 putenv(tmp);
194 free(lang);
196 /* Initialize libvlc */
197 libvlc_instance_t *vlc;
198 vlc = libvlc_new (argc, (const char **)argv);
199 if (vlc != NULL)
201 libvlc_set_app_id (vlc, "org.VideoLAN.VLC", PACKAGE_VERSION,
202 PACKAGE_NAME);
203 libvlc_set_user_agent (vlc, "VLC media player", "VLC/"PACKAGE_VERSION);
204 libvlc_add_intf (vlc, "hotkeys,none");
205 libvlc_add_intf (vlc, "globalhotkeys,none");
206 libvlc_add_intf (vlc, NULL);
207 libvlc_playlist_play (vlc, -1, 0, NULL);
208 libvlc_wait (vlc);
209 libvlc_release (vlc);
211 else
212 MessageBox (NULL, TEXT("VLC media player could not start.\n"
213 "Either the command line options were invalid or no plugins were found.\n"),
214 TEXT("VLC media player"),
215 MB_OK|MB_ICONERROR);
218 for (int i = 0; i < argc; i++)
219 free (argv[i]);
221 (void)hInstance; (void)hPrevInstance; (void)lpCmdLine; (void)nCmdShow;
222 return 0;
225 /* Crashdumps handling */
226 static void check_crashdump(void)
228 wchar_t mv_crashdump_path[MAX_PATH];
229 wcscpy (mv_crashdump_path, crashdump_path);
230 wcscat (mv_crashdump_path, L".mv");
232 if (_wrename (crashdump_path, mv_crashdump_path))
233 return;
235 FILE * fd = _wfopen ( mv_crashdump_path, L"r, ccs=UTF-8" );
236 if( !fd )
237 return;
238 fclose( fd );
240 int answer = MessageBox( NULL, L"Ooops: VLC media player just crashed.\n" \
241 "Would you like to send a bug report to the developers team?",
242 L"VLC crash reporting", MB_YESNO);
244 if(answer == IDYES)
246 HINTERNET Hint = InternetOpen(L"VLC Crash Reporter",
247 INTERNET_OPEN_TYPE_PRECONFIG, NULL,NULL,0);
248 if(Hint)
250 HINTERNET ftp = InternetConnect(Hint, L"crash.videolan.org",
251 INTERNET_DEFAULT_FTP_PORT, NULL, NULL,
252 INTERNET_SERVICE_FTP, INTERNET_FLAG_PASSIVE, 0);
253 if(ftp)
255 SYSTEMTIME now;
256 GetSystemTime(&now);
257 wchar_t remote_file[MAX_PATH];
258 _snwprintf(remote_file, MAX_PATH,
259 L"/crashes-win32/%04d%02d%02d%02d%02d%02d",
260 now.wYear, now.wMonth, now.wDay, now.wHour,
261 now.wMinute, now.wSecond );
263 if( FtpPutFile( ftp, mv_crashdump_path, remote_file,
264 FTP_TRANSFER_TYPE_BINARY, 0) )
265 fprintf(stderr, "Report sent correctly to FTP.\n");
266 else
267 fprintf(stderr,"Couldn't send report to FTP server\n");
269 InternetCloseHandle(ftp);
271 else
273 fprintf(stderr, "Can't connect to FTP server 0x%08lx\n",
274 (unsigned long)GetLastError());
276 InternetCloseHandle(Hint);
278 else
280 fprintf(stderr, "There was an error while connecting to the "
281 "Internet 0x%08lx\n", (unsigned long)GetLastError());
283 MessageBox( NULL, L"Thanks a lot for helping improving VLC!",
284 L"VLC crash report" , MB_OK);
287 _wremove(mv_crashdump_path);
290 /*****************************************************************************
291 * vlc_exception_filter: handles unhandled exceptions, like segfaults
292 *****************************************************************************/
293 LONG WINAPI vlc_exception_filter(struct _EXCEPTION_POINTERS *lpExceptionInfo)
295 if(IsDebuggerPresent())
297 //If a debugger is present, pass the exception to the debugger
298 //with EXCEPTION_CONTINUE_SEARCH
299 return EXCEPTION_CONTINUE_SEARCH;
301 else
303 fprintf( stderr, "unhandled vlc exception\n" );
305 FILE * fd = _wfopen ( crashdump_path, L"w, ccs=UTF-8" );
307 if( !fd )
309 fprintf( stderr, "\nerror while opening file" );
310 exit( 1 );
313 OSVERSIONINFO osvi;
314 ZeroMemory( &osvi, sizeof(OSVERSIONINFO) );
315 osvi.dwOSVersionInfoSize = sizeof( OSVERSIONINFO );
316 GetVersionEx( &osvi );
318 fwprintf( fd, L"[version]\nOS=%d.%d.%d.%d.%ls\nVLC=" VERSION_MESSAGE,
319 osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber,
320 osvi.dwPlatformId, osvi.szCSDVersion);
322 const CONTEXT *const pContext = (const CONTEXT *)
323 lpExceptionInfo->ContextRecord;
324 const EXCEPTION_RECORD *const pException = (const EXCEPTION_RECORD *)
325 lpExceptionInfo->ExceptionRecord;
326 /* No nested exceptions for now */
327 fwprintf( fd, L"\n\n[exceptions]\n%08x at %px",
328 pException->ExceptionCode, pException->ExceptionAddress );
330 for( unsigned int i = 0; i < pException->NumberParameters; i++ )
331 fwprintf( fd, L" | %p", pException->ExceptionInformation[i] );
333 #ifdef _WIN64
334 fwprintf( fd, L"\n\n[context]\nRDI:%px\nRSI:%px\n" \
335 "RBX:%px\nRDX:%px\nRCX:%px\nRAX:%px\n" \
336 "RBP:%px\nRIP:%px\nRSP:%px\nR8:%px\n" \
337 "R9:%px\nR10:%px\nR11:%px\nR12:%px\n" \
338 "R13:%px\nR14:%px\nR15:%px\n",
339 pContext->Rdi,pContext->Rsi,pContext->Rbx,
340 pContext->Rdx,pContext->Rcx,pContext->Rax,
341 pContext->Rbp,pContext->Rip,pContext->Rsp,
342 pContext->R8,pContext->R9,pContext->R10,
343 pContext->R11,pContext->R12,pContext->R13,
344 pContext->R14,pContext->R15 );
345 #else
346 fwprintf( fd, L"\n\n[context]\nEDI:%px\nESI:%px\n" \
347 "EBX:%px\nEDX:%px\nECX:%px\nEAX:%px\n" \
348 "EBP:%px\nEIP:%px\nESP:%px\n",
349 pContext->Edi,pContext->Esi,pContext->Ebx,
350 pContext->Edx,pContext->Ecx,pContext->Eax,
351 pContext->Ebp,pContext->Eip,pContext->Esp );
352 #endif
354 fwprintf( fd, L"\n[stacktrace]\n#EIP|base|module\n" );
356 #ifdef _WIN64
357 LPCVOID caller = (LPCVOID)pContext->Rip;
358 LPVOID *pBase = (LPVOID*)pContext->Rbp;
359 #else
360 LPVOID *pBase = (LPVOID*)pContext->Ebp;
361 LPCVOID caller = (LPCVOID)pContext->Eip;
362 #endif
363 for( unsigned frame = 0; frame <= 100; frame++ )
365 MEMORY_BASIC_INFORMATION mbi;
366 wchar_t module[ 256 ];
367 VirtualQuery( caller, &mbi, sizeof( mbi ) ) ;
368 GetModuleFileName( mbi.AllocationBase, module, 256 );
369 fwprintf( fd, L"%p|%ls\n", caller, module );
371 if( IsBadReadPtr( pBase, 2 * sizeof( void* ) ) )
372 break;
374 /*The last BP points to NULL!*/
375 caller = *(pBase + 1);
376 if( !caller )
377 break;
378 pBase = *pBase;
379 if( !pBase )
380 break;
383 HANDLE hpid = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ,
384 FALSE, GetCurrentProcessId());
385 if (hpid) {
386 HMODULE mods[1024];
387 DWORD size;
388 if (EnumProcessModules(hpid, mods, sizeof(mods), &size)) {
389 fwprintf( fd, L"\n\n[modules]\n" );
390 for (unsigned int i = 0; i < size / sizeof(HMODULE); i++) {
391 wchar_t module[ 256 ];
392 GetModuleFileName(mods[i], module, 256);
393 fwprintf( fd, L"%p|%ls\n", mods[i], module);
396 CloseHandle(hpid);
399 fclose( fd );
400 fflush( stderr );
401 exit( 1 );