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
45 #define HeapEnableTerminationOnCorruption (HEAP_INFORMATION_CLASS)1
48 void CheckCrashDump( const wchar_t* crashdump_path
);
49 void* InstallCrashHandler( const wchar_t* crashdump_path
);
50 void ReleaseCrashHandler( void* handler
);
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
= (BOOL (WINAPI
*)(DWORD
))
74 GetProcAddress(h
, "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 static void vlc_kill(void *data
)
118 ReleaseSemaphore(*semp
, 1, NULL
);
121 int WINAPI
WinMain( HINSTANCE hInstance
, HINSTANCE hPrevInstance
,
127 /* VLC does not change the thread locale, so gettext/libintil will use the
128 * user default locale as reference. */
129 /* gettext versions 0.18-0.18.1 will use the Windows Vista locale name
130 * if the GETTEXT_MUI environment variable is set. If not set or if running
131 * on Windows 2000/XP/2003 an hard-coded language ID list is used. This
132 * putenv() call may become redundant with later versions of gettext. */
133 putenv("GETTEXT_MUI=1");
135 putenv("VLC_PLUGIN_PATH=Z:"TOP_BUILDDIR
"/modules");
136 putenv("VLC_DATA_PATH=Z:"TOP_SRCDIR
"/share");
140 /* Disable stderr buffering. Indeed, stderr can be buffered on Windows (if
141 * connected to a pipe). */
142 setvbuf (stderr
, NULL
, _IONBF
, BUFSIZ
);
145 HeapSetInformation(NULL
, HeapEnableTerminationOnCorruption
, NULL
, 0);
147 /* SetProcessDEPPolicy, SetDllDirectory, & Co. */
148 HINSTANCE h_Kernel32
= GetModuleHandle(TEXT("kernel32.dll"));
149 if (h_Kernel32
!= NULL
)
152 #ifndef PROCESS_DEP_ENABLE
153 # define PROCESS_DEP_ENABLE 1
154 #endif /* PROCESS_DEP_ENABLE */
155 BOOL (WINAPI
* mySetProcessDEPPolicy
)( DWORD dwFlags
);
156 mySetProcessDEPPolicy
= (BOOL (WINAPI
*)(DWORD
))
157 GetProcAddress(h_Kernel32
, "SetProcessDEPPolicy");
158 if(mySetProcessDEPPolicy
)
159 mySetProcessDEPPolicy(PROCESS_DEP_ENABLE
);
163 /* Do NOT load any library from cwd. */
164 SetDllDirectory(TEXT(""));
167 * The LoadLibrary* calls from the modules and the 3rd party code
168 * will search in SYSTEM32 only
170 SetDefaultDllDirectories(LOAD_LIBRARY_SEARCH_SYSTEM32
);
172 * Load DLLs from system32 before any other folder (when possible)
174 PrioritizeSystem32();
177 wchar_t **wargv
= CommandLineToArgvW (GetCommandLine (), &argc
);
181 char *argv
[argc
+ 3];
182 BOOL crash_handling
= TRUE
;
186 argv
[j
++] = FromWide( L
"--media-library" );
187 argv
[j
++] = FromWide( L
"--no-ignore-config" );
188 for (int i
= 1; i
< argc
; i
++)
190 if(!wcscmp(wargv
[i
], L
"--no-crashdump"))
192 crash_handling
= FALSE
;
193 continue; /* don't give argument to libvlc */
195 if (!wcsncmp(wargv
[i
], L
"--language", 10) )
197 if (i
< argc
- 1 && wcsncmp( wargv
[i
+ 1], L
"--", 2 ))
198 lang
= FromWide (wargv
[++i
]);
202 argv
[j
++] = FromWide (wargv
[i
]);
213 static wchar_t path
[MAX_PATH
];
214 if( S_OK
!= SHGetFolderPathW( NULL
, CSIDL_APPDATA
| CSIDL_FLAG_CREATE
,
215 NULL
, SHGFP_TYPE_CURRENT
, path
) )
216 fprintf( stderr
, "Can't open the vlc conf PATH\n" );
217 _snwprintf( path
+wcslen( path
), MAX_PATH
, L
"%s", L
"\\vlc\\crashdump" );
218 CheckCrashDump( &path
[0] );
219 eh
= InstallCrashHandler( &path
[0] );
223 _setmode( _fileno( stdin
), _O_BINARY
); /* Needed for pipes */
229 if( RegOpenKeyEx( HKEY_CURRENT_USER
, TEXT("Software\\VideoLAN\\VLC\\"), 0, KEY_READ
, &h_key
)
234 if( RegQueryValueEx( h_key
, TEXT("Lang"), NULL
, NULL
, (LPBYTE
) &szData
, &len
) == ERROR_SUCCESS
)
235 lang
= FromWide( szData
);
239 if (lang
&& strncmp( lang
, "auto", 4 ) )
242 snprintf(tmp
, 11, "LANG=%s", lang
);
247 /* Initialize libvlc */
248 libvlc_instance_t
*vlc
;
249 vlc
= libvlc_new (argc
, (const char **)argv
);
252 HANDLE sem
= CreateSemaphore(NULL
, 0, 1, NULL
);
254 libvlc_set_exit_handler(vlc
, vlc_kill
, &sem
);
255 libvlc_set_app_id (vlc
, "org.VideoLAN.VLC", PACKAGE_VERSION
,
257 libvlc_set_user_agent (vlc
, "VLC media player", "VLC/"PACKAGE_VERSION
);
258 libvlc_add_intf (vlc
, "hotkeys,none");
259 libvlc_add_intf (vlc
, "globalhotkeys,none");
260 libvlc_add_intf (vlc
, NULL
);
261 libvlc_playlist_play (vlc
);
263 WaitForSingleObject(sem
, INFINITE
);
266 libvlc_release (vlc
);
269 MessageBox (NULL
, TEXT("VLC media player could not start.\n"
270 "Either the command line options were invalid or no plugins were found.\n"),
271 TEXT("VLC media player"),
276 ReleaseCrashHandler( eh
);
278 for (int i
= 0; i
< argc
; i
++)
281 (void)hInstance
; (void)hPrevInstance
; (void)lpCmdLine
; (void)nCmdShow
;