main.cpp 14.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * main.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22
 *****************************************************************************/

23 24 25 26
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

27
#include "plugin.h"
28 29 30
#include "utils.h"

#include <stdio.h>
31

Rafaël Carré's avatar
Rafaël Carré committed
32
#include <objsafe.h>
33 34 35 36
#include <comcat.h>
#include <windows.h>
#include <shlwapi.h>

37
#include <tchar.h>
38

39 40 41 42 43 44
#ifdef __MINGW32__
# include <_mingw.h>
# if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64_VERSION_MAJOR)
#  undef GUID_EXT
#  define GUID_EXT
#  include <initguid.h>
45 46
DEFINE_GUID(CATID_InternetAware, \
    0x0DE86A58, 0x2BAA, 0x11CF, 0xA2, 0x29, 0x00,0xAA,0x00,0x3D,0x73,0x52);
47 48 49 50
DEFINE_GUID(CATID_SafeForInitializing, \
    0x7DD95802, 0x9882, 0x11CF, 0x9F, 0xA9, 0x00,0xAA,0x00,0x6C,0x42,0xC4);
DEFINE_GUID(CATID_SafeForScripting, \
    0x7DD95801, 0x9882, 0x11CF, 0x9F, 0xA9, 0x00,0xAA,0x00,0x6C,0x42,0xC4);
51 52
#  endif /* __MINGW32_MAJOR_VERSION && !__MINGW64_VERSION_MAJOR */
#endif /* __MINGW32__ */
53

54 55
using namespace std;

56
#define DESCRIPTION "VLC ActiveX Plugin and IE Web Plugin"
57

58 59 60
#define THREADING_MODEL "Apartment"
#define MISC_STATUS     "131473"

61
#define PROGID_STR "VideoLAN.VLCPlugin"
62 63 64 65 66 67

#define GUID_STRLEN 39

static LONG i_class_ref= 0;
static HINSTANCE h_instance= 0;

68 69 70 71 72
HMODULE DllGetModule()
{
    return h_instance;
};

73 74 75 76 77 78
STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
{
    HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;

    *ppv = NULL;

79
    if( CLSID_VLCPlugin2 == rclsid )
80
    {
81 82
        VLCPluginClass *plugin =
            new VLCPluginClass(&i_class_ref, h_instance, rclsid);
83 84 85 86 87 88 89 90 91 92 93
        hr = plugin->QueryInterface(riid, ppv);
        plugin->Release();
    }
    return hr;
};

STDAPI DllCanUnloadNow(VOID)
{
    return (0 == i_class_ref) ? S_OK: S_FALSE;
};

94
static inline HKEY keyCreate(HKEY parentKey, LPCTSTR keyName)
95
{
96
    HKEY childKey;
97
    if( ERROR_SUCCESS == RegCreateKeyEx(parentKey, keyName, 0, NULL,
98
             REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &childKey, NULL) )
99
    {
100
        return childKey;
101
    }
102 103 104
    return NULL;
};

105 106
static inline HKEY keySet(HKEY hKey, LPCTSTR valueName,
                          const void *s, size_t len, DWORD dwType = REG_SZ)
107 108
{
    if( NULL != hKey )
109
    {
110
        RegSetValueEx(hKey, valueName, 0, dwType, (const BYTE*)s, len);
111
    }
112
    return hKey;
113 114
};

115 116
static inline HKEY keySetDef(HKEY hKey,
                             const void *s, size_t len, DWORD dwType = REG_SZ)
117
{
118
    return keySet(hKey, NULL, s, len, dwType);
119 120
};

121
static inline HKEY keySetDef(HKEY hKey, LPCTSTR s)
122
{
123
    return keySetDef(hKey, s, sizeof(TCHAR)*(_tcslen(s)+1), REG_SZ);
124 125 126 127 128
};

static inline HKEY keyClose(HKEY hKey)
{
    if( NULL != hKey )
129
    {
130
        RegCloseKey(hKey);
131 132 133 134
    }
    return NULL;
};

135
static void UnregisterProgID(REFCLSID rclsid, unsigned int version)
136
{
137
    OLECHAR szCLSID[GUID_STRLEN];
138

139
    StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
140

141
    TCHAR progId[sizeof(PROGID_STR)+16];
142
    _stprintf(progId, TEXT("%s.%u"), PROGID_STR, version);
143

144
    SHDeleteKey(HKEY_CLASSES_ROOT, progId);
145 146

    HKEY hClsIDKey;
147
    if( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"), 0, KEY_WRITE, &hClsIDKey) )
148
    {
149
        SHDeleteKey(hClsIDKey, szCLSID);
150 151
        RegCloseKey(hClsIDKey);
    }
152 153 154 155 156 157

    if( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Ext\\Stats"), 0, KEY_WRITE, &hClsIDKey) )
    {
        SHDeleteKey(hClsIDKey, szCLSID);
        RegCloseKey(hClsIDKey);
    }
158 159
};

160 161 162
STDAPI DllUnregisterServer(VOID)
{
    // unregister type lib from the registry
163
#if !defined(_WIN64)
164
    UnRegisterTypeLib(LIBID_AXVLC, 1, 0, LOCALE_NEUTRAL, SYS_WIN32);
165 166 167
#else
    UnRegisterTypeLib(LIBID_AXVLC, 1, 0, LOCALE_NEUTRAL, SYS_WIN64);
#endif
168 169 170

    // remove component categories we supports
    ICatRegister *pcr;
171
    if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
172 173 174 175
            NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
        CATID implCategories[] = {
            CATID_Control,
            CATID_PersistsToPropertyBag,
176 177 178
            CATID_InternetAware,
            CATID_SafeForInitializing,
            CATID_SafeForScripting,
179 180
        };

181 182
        pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin2,
                sizeof(implCategories)/sizeof(CATID), implCategories);
183 184 185
        pcr->Release();
    }

186
    SHDeleteKey(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
187

188 189
    SHDeleteKey(HKEY_CLASSES_ROOT, TEXT("MIME\\Database\\Content Type\\application/x-vlc-plugin"));

190
    UnregisterProgID(CLSID_VLCPlugin2, 1);
191 192 193 194

    return S_OK;
};

195
static HRESULT RegisterClassID(HKEY hParent, REFCLSID rclsid, unsigned int version, BOOL isDefault, LPCTSTR path, size_t pathLen)
196
{
197
    TCHAR progId[sizeof(PROGID_STR)+16];
198
    _stprintf(progId, TEXT("%s.%u"), PROGID_STR, version);
199

200
    TCHAR description[sizeof(DESCRIPTION)+16];
201
    _stprintf(description, TEXT("%s v%u"), DESCRIPTION, version);
202

203 204
    HKEY hClassKey;
    {
205
        OLECHAR szCLSID[GUID_STRLEN];
206

207
        StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
208

209 210 211 212 213 214
        HKEY hProgKey = keyCreate(HKEY_CLASSES_ROOT, progId);
        if( NULL != hProgKey )
        {
            // default key value
            keySetDef(hProgKey, description);

215
            keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
216
                               szCLSID, sizeof(szCLSID)));
217

218
            //hSubKey = keyClose(keyCreate(hBaseKey, "Insertable"));
219
 
220 221 222 223
            RegCloseKey(hProgKey);
        }
        if( isDefault )
        {
224
            hProgKey = keyCreate(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
225 226 227 228 229
            if( NULL != hProgKey )
            {
                // default key value
                keySetDef(hProgKey, description);

230
                keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
231
                                   szCLSID, sizeof(szCLSID)));
232

233
                keyClose(keySetDef(keyCreate(hProgKey, TEXT("CurVer")),
234
                                   progId));
235
            }
236 237 238 239 240 241 242 243 244 245 246
            
            // register the mime type for the type attribute
            hProgKey = keyCreate(HKEY_CLASSES_ROOT, TEXT("MIME\\Database\\Content Type\\application/x-vlc-plugin"));
            if( NULL != hProgKey )
            {
                // default key value
                keySetDef(hProgKey, description);

                keyClose(keySet(hProgKey, TEXT("CLSID"),
                                szCLSID, sizeof(szCLSID)));
            }
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265

            // entry for addons list
            hProgKey = keyCreate(HKEY_CURRENT_USER, TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Ext\\Stats"));
            if( NULL != hProgKey )
            {
                HKEY hSubKey = keyCreate(hProgKey, szCLSID);
                if( NULL != hSubKey )
                {
                    HKEY hSubKey2 = keyCreate(hSubKey, TEXT("iexplore"));
                    if( NULL != hSubKey2 )
                    {
                        keyClose(keyCreate(hSubKey2, TEXT("AllowedDomains\\*")));

                        DWORD value = 1;
                        keyClose(keySet(hSubKey2, TEXT("Type"),
                                        (const BYTE*)&value, sizeof(value), REG_DWORD));
                    }
                }
            }
266
        }
267
        hClassKey = keyCreate(hParent, szCLSID);
268
    }
269 270 271
    if( NULL != hClassKey )
    {
        // default key value
272
        keySetDef(hClassKey, description);
273 274

        // Control key value
275
        keyClose(keyCreate(hClassKey, TEXT("Control")));
276

277
        // Insertable key value
278
        //keyClose(keyCreate(hClassKey, TEXT("Insertable")));
279

280
        // ToolboxBitmap32 key value
281
        {
282 283
            std::vector<TCHAR> iconPathBuf(pathLen+3, 0);
            TCHAR* iconPath = &iconPathBuf[0];
284 285
            memcpy(iconPath, path, sizeof(TCHAR)*pathLen);
            _tcscpy(iconPath+pathLen, TEXT(",1"));
286
            keyClose(keySetDef(keyCreate(hClassKey,
287
                TEXT("ToolboxBitmap32")),
288
                iconPath, sizeof(TCHAR)*(_tcslen(iconPath)+1)));
289
        }
290

291 292
#ifdef BUILD_LOCALSERVER
        // LocalServer32 key value
293
        keyClose(keySetDef(keyCreate(hClassKey,
294
            TEXT("LocalServer32"), path, sizeof(TCHAR)*(pathLen+1))));
295
#else
296
        // InprocServer32 key value
297 298
        {
            HKEY hSubKey = keySetDef(keyCreate(hClassKey,
299 300
                TEXT("InprocServer32")),
                path, sizeof(TCHAR)*(pathLen+1));
301
            keySet(hSubKey,
302 303
                TEXT("ThreadingModel"),
                TEXT(THREADING_MODEL), sizeof(TEXT(THREADING_MODEL)));
304 305
            keyClose(hSubKey);
        }
306
#endif
307 308

        // MiscStatus key value
309 310
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("MiscStatus\\1")),
                           TEXT(MISC_STATUS), sizeof(TEXT(MISC_STATUS))));
311 312

        // Programmable key value
313
        keyClose(keyCreate(hClassKey, TEXT("Programmable")));
314 315

        // ProgID key value
316
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("ProgID")),progId));
317 318

        // VersionIndependentProgID key value
319
        keyClose(keySetDef(keyCreate(hClassKey,
320
                                     TEXT("VersionIndependentProgID")),
321
                           TEXT(PROGID_STR), sizeof(TEXT(PROGID_STR))));
322 323

        // Version key value
324
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("Version")),TEXT("1.0")));
325 326

        // TypeLib key value
327 328 329 330
        OLECHAR szLIBID[GUID_STRLEN];

        StringFromGUID2(LIBID_AXVLC, szLIBID, GUID_STRLEN);

331 332
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("TypeLib")),
                           szLIBID, sizeof(szLIBID)));
333
 
334 335
        RegCloseKey(hClassKey);
    }
336 337 338 339 340 341 342
    return S_OK;
}

STDAPI DllRegisterServer(VOID)
{
    DllUnregisterServer();

343 344
    TCHAR DllPath[MAX_PATH];
    DWORD DllPathLen=GetModuleFileName(h_instance, DllPath, MAX_PATH) ;
345
    if( 0 == DllPathLen )
346
        return E_UNEXPECTED;
347
    DllPath[MAX_PATH-1] = '\0';
348 349 350

    HKEY hBaseKey;

351 352
    if( ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"),
                                      0, KEY_CREATE_SUB_KEY, &hBaseKey) )
353
        return SELFREG_E_CLASS;
354

355
    RegisterClassID(hBaseKey, CLSID_VLCPlugin2, 2, TRUE, DllPath, DllPathLen);
356 357 358

    RegCloseKey(hBaseKey);

359 360
    // indicate which component categories we support
    ICatRegister *pcr;
361
    if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
362 363 364 365
            NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
        CATID implCategories[] = {
            CATID_Control,
            CATID_PersistsToPropertyBag,
366 367 368
            CATID_InternetAware,
            CATID_SafeForInitializing,
            CATID_SafeForScripting,
369 370
        };

371 372
        pcr->RegisterClassImplCategories(CLSID_VLCPlugin2,
                sizeof(implCategories)/sizeof(CATID), implCategories);
373 374 375
        pcr->Release();
    }

376 377
#ifdef BUILD_LOCALSERVER
    // replace .exe by .tlb
378
    _tcscpy(DllPath+DllPathLen-4, TEXT(".tlb"));
379
#endif
380

381 382 383 384 385
    // register type lib into the registry
    ITypeLib *typeLib;

    HRESULT result = LoadTypeLibEx(DllPath, REGKIND_REGISTER, &typeLib);
    if( SUCCEEDED(result) )
386
        typeLib->Release();
387

388
    return result;
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
};

#ifdef BUILD_LOCALSERVER

/*
** easier to debug an application than a DLL on cygwin GDB :)
*/
#include <iostream>

STDAPI_(int) WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
{
    MSG msg;

    if( FAILED(OleInitialize(NULL)) )
    {
        cerr << "cannot initialize OLE" << endl;
        return 1;
    }

408 409 410 411 412 413 414 415
    h_instance = hInst;

    if( FAILED(DllRegisterServer()) )
    {
        cerr << "cannot register Local Server" << endl;
        return 1;
    }

416 417
    IUnknown *classProc = NULL;

418 419
    if( FAILED(DllGetClassObject(CLSID_VLCPlugin, IID_IUnknown,
                                 (LPVOID *)&classProc)) )
420 421 422
        return 0;
 
    DWORD dwRegisterClassObject;
423
    DWORD dwRegisterClassObject2;
424 425 426 427 428 429 430 431 432 433 434

    if( FAILED(CoRegisterClassObject(CLSID_VLCPlugin, classProc,
        CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegisterClassObject)) )
        return 0;

    DWORD dwRegisterActiveObject;

    if( FAILED(RegisterActiveObject(classProc, CLSID_VLCPlugin,
                    ACTIVEOBJECT_WEAK, &dwRegisterActiveObject)) )
        return 0;

435 436 437 438
    if( FAILED(RegisterActiveObject(classProc, CLSID_VLCPlugin2,
                    ACTIVEOBJECT_WEAK, &dwRegisterActiveObject2)) )
        return 0;

439 440 441 442 443 444 445 446 447 448
    classProc->Release();

    /*
    * Polling messages from event queue
    */
    while( S_FALSE == DllCanUnloadNow() )
    {
        while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
        {
            if( msg.message == WM_QUIT )
449
                break;  // break out PeekMessage loop
450 451 452 453 454 455 456 457 458

            /*if(TranslateAccelerator(ghwndApp, ghAccel, &msg))
                continue;*/

            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(msg.message == WM_QUIT)
459
            break;  // break out main loop
460 461 462 463 464 465 466

        WaitMessage();
    }

    if( SUCCEEDED(RevokeActiveObject(dwRegisterActiveObject, NULL)) )
        CoRevokeClassObject(dwRegisterClassObject);

467 468 469
    if( SUCCEEDED(RevokeActiveObject(dwRegisterActiveObject2, NULL)) )
        CoRevokeClassObject(dwRegisterClassObject2);

470
    // Reached on WM_QUIT message
471
    OleUninitialize();
472 473 474 475 476 477 478
    return ((int) msg.wParam);
};

#else

STDAPI_(BOOL) DllMain(HANDLE hModule, DWORD fdwReason, LPVOID lpReserved )
{
479
    (void)lpReserved;
480 481 482 483 484 485 486 487 488 489 490 491 492 493
    switch( fdwReason )
    {
        case DLL_PROCESS_ATTACH:
            h_instance = (HINSTANCE)hModule;
            break;

        default:
            break;
    }
    return TRUE;
};

#endif