1 /*****************************************************************************
\r
2 * main.cpp: ActiveX control for VLC
\r
3 *****************************************************************************
\r
4 * Copyright (C) 2005 VideoLAN
\r
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
\r
8 * This program is free software; you can redistribute it and/or modify
\r
9 * it under the terms of the GNU General Public License as published by
\r
10 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * This program is distributed in the hope that it will be useful,
\r
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
16 * GNU General Public License for more details.
\r
18 * You should have received a copy of the GNU General Public License
\r
19 * along with this program; if not, write to the Free Software
\r
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
\r
21 *****************************************************************************/
\r
26 #include <windows.h>
\r
27 #include <shlwapi.h>
\r
29 using namespace std;
\r
31 #define THREADING_MODEL "Both"
\r
32 #define COMPANY_STR "VideoLAN"
\r
33 #define PROGRAM_STR "VLCPlugin"
\r
34 #define VERSION_MAJOR_STR "1"
\r
35 #define VERSION_MINOR_STR "0"
\r
36 #define DESCRIPTION "VideoLAN VLC ActiveX Plugin"
\r
38 #define PROGID_STR COMPANY_STR"."PROGRAM_STR
\r
39 #define VERS_PROGID_STR COMPANY_STR"."PROGRAM_STR"."VERSION_MAJOR_STR
\r
40 #define VERSION_STR VERSION_MAJOR_STR"."VERSION_MINOR_STR
\r
42 #define GUID_STRLEN 39
\r
45 ** MingW headers do not declare those
\r
47 extern const CATID CATID_SafeForInitializing;
\r
48 extern const CATID CATID_SafeForScripting;
\r
50 static LONG i_class_ref= 0;
\r
51 static HINSTANCE h_instance= 0;
\r
53 STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
\r
55 HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
\r
59 if( CLSID_VLCPlugin == rclsid )
\r
61 VLCPluginClass *plugin = new VLCPluginClass(&i_class_ref, h_instance);
\r
62 hr = plugin->QueryInterface(riid, ppv);
\r
68 STDAPI DllCanUnloadNow(VOID)
\r
70 return (0 == i_class_ref) ? S_OK: S_FALSE;
\r
73 static LPCTSTR TStrFromGUID(REFGUID clsid) {
\r
76 if( FAILED(StringFromIID(clsid, &oleStr)) )
\r
79 //check whether TCHAR and OLECHAR are both either ANSI or UNICODE
\r
80 if( sizeof(TCHAR) == sizeof(OLECHAR) )
\r
81 return (LPCTSTR)oleStr;
\r
83 LPTSTR pct_CLSID = NULL;
\r
85 size_t len = WideCharToMultiByte(CP_ACP, 0, oleStr, -1, NULL, 0, NULL, NULL);
\r
88 pct_CLSID = (char *)CoTaskMemAlloc(len);
\r
89 WideCharToMultiByte(CP_ACP, 0, oleStr, -1, pct_CLSID, len, NULL, NULL);
\r
92 size_t len = MutiByteToWideChar(CP_ACP, 0, oleStr, -1, NULL, 0);
\r
95 clsidStr = (wchar_t *)CoTaskMemAlloc(len*sizeof(wchar_t));
\r
96 WideCharToMultiByte(CP_ACP, 0, oleStr, -1, pct_CLSID, len);
\r
99 CoTaskMemFree(oleStr);
\r
103 static HKEY keyCreate(HKEY parentKey, LPCTSTR keyName)
\r
106 if( ERROR_SUCCESS == RegCreateKeyEx(parentKey, keyName, 0, NULL,
\r
107 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &childKey, NULL) )
\r
114 STDAPI DllUnregisterServer(VOID)
\r
116 // unregister type lib from the registry
\r
117 UnRegisterTypeLib(LIBID_AXVLC, 1, 0, LOCALE_NEUTRAL, SYS_WIN32);
\r
119 // remove component categories we supports
\r
121 if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
\r
122 NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
\r
123 CATID implCategories[] = {
\r
125 CATID_PersistsToPropertyBag,
\r
126 CATID_SafeForInitializing,
\r
127 CATID_SafeForScripting,
\r
130 pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin,
\r
131 sizeof(implCategories)/sizeof(CATID), implCategories);
\r
135 SHDeleteKey(HKEY_CLASSES_ROOT, TEXT(VERS_PROGID_STR));
\r
136 SHDeleteKey(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
\r
138 LPCTSTR psz_CLSID = TStrFromGUID(CLSID_VLCPlugin);
\r
140 if( NULL == psz_CLSID )
\r
141 return E_OUTOFMEMORY;
\r
144 if( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"), 0, KEY_WRITE, &hClsIDKey) )
\r
146 SHDeleteKey(hClsIDKey, psz_CLSID);
\r
147 RegCloseKey(hClsIDKey);
\r
149 CoTaskMemFree((void *)psz_CLSID);
\r
154 STDAPI DllRegisterServer(VOID)
\r
156 DllUnregisterServer();
\r
158 char DllPath[MAX_PATH];
\r
159 DWORD DllPathLen= GetModuleFileName(h_instance, DllPath, sizeof(DllPath)) ;
\r
160 if( 0 == DllPathLen )
\r
163 LPCTSTR psz_CLSID = TStrFromGUID(CLSID_VLCPlugin);
\r
165 if( NULL == psz_CLSID )
\r
166 return E_OUTOFMEMORY;
\r
170 if( ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"), 0, KEY_CREATE_SUB_KEY, &hBaseKey) )
\r
173 HKEY hClassKey = keyCreate(hBaseKey, psz_CLSID);
\r
174 if( NULL != hClassKey )
\r
178 // default key value
\r
179 RegSetValueEx(hClassKey, NULL, 0, REG_SZ,
\r
180 (const BYTE*)DESCRIPTION, sizeof(DESCRIPTION));
\r
182 // Control key value
\r
183 hSubKey = keyCreate(hClassKey, TEXT("Control"));
\r
184 RegCloseKey(hSubKey);
\r
186 // InprocServer32 key value
\r
187 hSubKey = keyCreate(hClassKey, TEXT("InprocServer32"));
\r
188 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
189 (const BYTE*)DllPath, DllPathLen);
\r
190 RegSetValueEx(hSubKey, TEXT("ThreadingModel"), 0, REG_SZ,
\r
191 (const BYTE*)THREADING_MODEL, sizeof(THREADING_MODEL));
\r
192 RegCloseKey(hSubKey);
\r
194 // MiscStatus key value
\r
195 hSubKey = keyCreate(hClassKey, TEXT("MiscStatus\\1"));
\r
196 RegSetValueEx(hSubKey, NULL, 0, REG_SZ, (const BYTE*)"131473", sizeof("131473"));
\r
197 RegCloseKey(hSubKey);
\r
199 // Programmable key value
\r
200 hSubKey = keyCreate(hClassKey, TEXT("Programmable"));
\r
201 RegCloseKey(hSubKey);
\r
203 // ProgID key value
\r
204 hSubKey = keyCreate(hClassKey, TEXT("ProgID"));
\r
205 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
206 (const BYTE*)VERS_PROGID_STR, sizeof(VERS_PROGID_STR));
\r
207 RegCloseKey(hSubKey);
\r
209 // VersionIndependentProgID key value
\r
210 hSubKey = keyCreate(hClassKey, TEXT("VersionIndependentProgID"));
\r
211 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
212 (const BYTE*)PROGID_STR, sizeof(PROGID_STR));
\r
213 RegCloseKey(hSubKey);
\r
215 // Version key value
\r
216 hSubKey = keyCreate(hClassKey, TEXT("Version"));
\r
217 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
218 (const BYTE*)VERSION_STR, sizeof(VERSION_STR));
\r
219 RegCloseKey(hSubKey);
\r
221 // TypeLib key value
\r
222 LPCTSTR psz_LIBID = TStrFromGUID(LIBID_AXVLC);
\r
223 if( NULL != psz_LIBID )
\r
225 hSubKey = keyCreate(hClassKey, TEXT("TypeLib"));
\r
226 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
227 (const BYTE*)psz_LIBID, sizeof(TCHAR)*GUID_STRLEN);
\r
228 RegCloseKey(hSubKey);
\r
230 RegCloseKey(hClassKey);
\r
232 RegCloseKey(hBaseKey);
\r
234 hBaseKey = keyCreate(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
\r
235 if( NULL != hBaseKey )
\r
237 // default key value
\r
238 RegSetValueEx(hBaseKey, NULL, 0, REG_SZ,
\r
239 (const BYTE*)DESCRIPTION, sizeof(DESCRIPTION));
\r
241 HKEY hSubKey = keyCreate(hBaseKey, TEXT("CLSID"));
\r
242 if( NULL != hSubKey )
\r
244 // default key value
\r
245 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
246 (const BYTE*)psz_CLSID, sizeof(TCHAR)*GUID_STRLEN);
\r
248 RegCloseKey(hSubKey);
\r
250 hSubKey = keyCreate(hBaseKey, TEXT("CurVer"));
\r
251 if( NULL != hSubKey )
\r
253 // default key value
\r
254 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
255 (const BYTE*)VERS_PROGID_STR, sizeof(VERS_PROGID_STR));
\r
257 RegCloseKey(hSubKey);
\r
259 RegCloseKey(hBaseKey);
\r
262 hBaseKey = keyCreate(HKEY_CLASSES_ROOT, TEXT(VERS_PROGID_STR));
\r
263 if( NULL != hBaseKey )
\r
265 // default key value
\r
266 RegSetValueEx(hBaseKey, NULL, 0, REG_SZ,
\r
267 (const BYTE*)DESCRIPTION, sizeof(DESCRIPTION));
\r
269 HKEY hSubKey = keyCreate(hBaseKey, TEXT("CLSID"));
\r
270 if( NULL != hSubKey )
\r
272 // default key value
\r
273 RegSetValueEx(hSubKey, NULL, 0, REG_SZ,
\r
274 (const BYTE*)psz_CLSID, sizeof(TCHAR)*GUID_STRLEN);
\r
276 RegCloseKey(hSubKey);
\r
278 RegCloseKey(hBaseKey);
\r
281 // indicate which component categories we support
\r
283 if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
\r
284 NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
\r
285 CATID implCategories[] = {
\r
287 CATID_PersistsToPropertyBag,
\r
288 CATID_SafeForInitializing,
\r
289 CATID_SafeForScripting,
\r
292 pcr->RegisterClassImplCategories(CLSID_VLCPlugin,
\r
293 sizeof(implCategories)/sizeof(CATID), implCategories);
\r
297 // register type lib into the registry
\r
300 size_t typeLibPathLen = MultiByteToWideChar(CP_ACP, 0, DllPath, DllPathLen, NULL, 0);
\r
301 if( typeLibPathLen > 0 )
\r
303 LPOLESTR typeLibPath = (LPOLESTR)CoTaskMemAlloc(typeLibPathLen*sizeof(wchar_t));
\r
304 MultiByteToWideChar(CP_ACP, 0, DllPath, DllPathLen, typeLibPath, typeLibPathLen);
\r
305 if( SUCCEEDED(LoadTypeLibEx(typeLibPath, REGKIND_REGISTER, &typeLib)) )
\r
306 typeLib->Release();
\r
307 CoTaskMemFree((void *)typeLibPath);
\r
310 if( SUCCEEDED(LoadTypeLibEx((LPOLESTR)DllPath, REGKIND_REGISTER, &typeLib)) )
\r
311 typeLib->Release();
\r
314 CoTaskMemFree((void *)psz_CLSID);
\r
319 #ifdef BUILD_LOCALSERVER
\r
322 ** easier to debug an application than a DLL on cygwin GDB :)
\r
324 #include <iostream>
\r
326 STDAPI_(int) WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
\r
330 if( FAILED(OleInitialize(NULL)) )
\r
332 cerr << "cannot initialize OLE" << endl;
\r
336 IUnknown *classProc = NULL;
\r
338 if( FAILED(DllGetClassObject(CLSID_VLCPlugin, IID_IUnknown, (LPVOID *)&classProc)) )
\r
341 DWORD dwRegisterClassObject;
\r
343 if( FAILED(CoRegisterClassObject(CLSID_VLCPlugin, classProc,
\r
344 CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegisterClassObject)) )
\r
347 DWORD dwRegisterActiveObject;
\r
349 if( FAILED(RegisterActiveObject(classProc, CLSID_VLCPlugin,
\r
350 ACTIVEOBJECT_WEAK, &dwRegisterActiveObject)) )
\r
353 classProc->Release();
\r
356 * Polling messages from event queue
\r
358 while( S_FALSE == DllCanUnloadNow() )
\r
360 while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
\r
362 if( msg.message == WM_QUIT )
\r
363 break; // Leave the PeekMessage while() loop
\r
365 /*if(TranslateAccelerator(ghwndApp, ghAccel, &msg))
\r
368 TranslateMessage(&msg);
\r
369 DispatchMessage(&msg);
\r
372 if(msg.message == WM_QUIT)
\r
373 break; // Leave the for() loop
\r
378 if( SUCCEEDED(RevokeActiveObject(dwRegisterActiveObject, NULL)) )
\r
379 CoRevokeClassObject(dwRegisterClassObject);
\r
381 // Reached on WM_QUIT message
\r
383 return ((int) msg.wParam);
\r
388 STDAPI_(BOOL) DllMain(HANDLE hModule, DWORD fdwReason, LPVOID lpReserved )
\r
390 switch( fdwReason )
\r
392 case DLL_PROCESS_ATTACH:
\r
393 h_instance = (HINSTANCE)hModule;
\r