main.cpp 14.5 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
 *****************************************************************************/

#include "plugin.h"
24 25 26
#include "utils.h"

#include <stdio.h>
27

Rafaël Carré's avatar
Rafaël Carré committed
28
#include <objsafe.h>
29 30 31 32
#include <comcat.h>
#include <windows.h>
#include <shlwapi.h>

33
#include <tchar.h>
34

35 36 37 38 39 40
#ifdef __MINGW32__
# include <_mingw.h>
# if defined(__MINGW32_MAJOR_VERSION) && !defined(__MINGW64_VERSION_MAJOR)
#  undef GUID_EXT
#  define GUID_EXT
#  include <initguid.h>
41 42
DEFINE_GUID(CATID_InternetAware, \
    0x0DE86A58, 0x2BAA, 0x11CF, 0xA2, 0x29, 0x00,0xAA,0x00,0x3D,0x73,0x52);
43 44 45 46
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);
47 48
#  endif /* __MINGW32_MAJOR_VERSION && !__MINGW64_VERSION_MAJOR */
#endif /* __MINGW32__ */
49

50 51 52 53
using namespace std;

#define COMPANY_STR "VideoLAN"
#define PROGRAM_STR "VLCPlugin"
54
#define DESCRIPTION "VLC ActiveX Plugin and IE Web Plugin"
55

56 57 58
#define THREADING_MODEL "Apartment"
#define MISC_STATUS     "131473"

59
#define PROGID_STR COMPANY_STR"."PROGRAM_STR
60 61 62 63 64 65

#define GUID_STRLEN 39

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

66 67 68 69 70
HMODULE DllGetModule()
{
    return h_instance;
};

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

    *ppv = NULL;

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

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

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

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

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

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

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

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

137
    StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
138

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

142
    SHDeleteKey(HKEY_CLASSES_ROOT, progId);
143 144

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

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

158 159 160 161 162 163 164
STDAPI DllUnregisterServer(VOID)
{
    // unregister type lib from the registry
    UnRegisterTypeLib(LIBID_AXVLC, 1, 0, LOCALE_NEUTRAL, SYS_WIN32);

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

        pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin,
                sizeof(implCategories)/sizeof(CATID), implCategories);
177 178
        pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin2,
                sizeof(implCategories)/sizeof(CATID), implCategories);
179 180 181
        pcr->Release();
    }

182
    SHDeleteKey(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
183

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

186
    UnregisterProgID(CLSID_VLCPlugin, 2);
187
    UnregisterProgID(CLSID_VLCPlugin2, 1);
188 189 190 191

    return S_OK;
};

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

197
    TCHAR description[sizeof(DESCRIPTION)+16];
198
    _stprintf(description, TEXT("%s v%u"), DESCRIPTION, version);
199

200 201
    HKEY hClassKey;
    {
202
        OLECHAR szCLSID[GUID_STRLEN];
203

204
        StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
205

206 207 208 209 210 211
        HKEY hProgKey = keyCreate(HKEY_CLASSES_ROOT, progId);
        if( NULL != hProgKey )
        {
            // default key value
            keySetDef(hProgKey, description);

212
            keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
213
                               szCLSID, sizeof(szCLSID)));
214

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

227
                keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
228
                                   szCLSID, sizeof(szCLSID)));
229

230
                keyClose(keySetDef(keyCreate(hProgKey, TEXT("CurVer")),
231
                                   progId));
232
            }
233 234 235 236 237 238 239 240 241 242 243
            
            // 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)));
            }
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262

            // 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));
                    }
                }
            }
263
        }
264
        hClassKey = keyCreate(hParent, szCLSID);
265
    }
266 267 268
    if( NULL != hClassKey )
    {
        // default key value
269
        keySetDef(hClassKey, description);
270 271

        // Control key value
272
        keyClose(keyCreate(hClassKey, TEXT("Control")));
273

Damien Fouilleul's avatar
Damien Fouilleul committed
274
        // Insertable key value
275
        //keyClose(keyCreate(hClassKey, TEXT("Insertable")));
Damien Fouilleul's avatar
Damien Fouilleul committed
276

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

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

        // MiscStatus key value
306 307
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("MiscStatus\\1")),
                           TEXT(MISC_STATUS), sizeof(TEXT(MISC_STATUS))));
308 309

        // Programmable key value
310
        keyClose(keyCreate(hClassKey, TEXT("Programmable")));
311 312

        // ProgID key value
313
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("ProgID")),progId));
314 315

        // VersionIndependentProgID key value
316
        keyClose(keySetDef(keyCreate(hClassKey,
317
                                     TEXT("VersionIndependentProgID")),
318
                           TEXT(PROGID_STR), sizeof(TEXT(PROGID_STR))));
319 320

        // Version key value
321
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("Version")),TEXT("1.0")));
322 323

        // TypeLib key value
324 325 326 327
        OLECHAR szLIBID[GUID_STRLEN];

        StringFromGUID2(LIBID_AXVLC, szLIBID, GUID_STRLEN);

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

STDAPI DllRegisterServer(VOID)
{
    DllUnregisterServer();

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

    HKEY hBaseKey;

348 349
    if( ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"),
                                      0, KEY_CREATE_SUB_KEY, &hBaseKey) )
350
        return SELFREG_E_CLASS;
351

352 353
    RegisterClassID(hBaseKey, CLSID_VLCPlugin, 1, FALSE, DllPath, DllPathLen);
    RegisterClassID(hBaseKey, CLSID_VLCPlugin2, 2, TRUE, DllPath, DllPathLen);
354 355 356

    RegCloseKey(hBaseKey);

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

        pcr->RegisterClassImplCategories(CLSID_VLCPlugin,
                sizeof(implCategories)/sizeof(CATID), implCategories);
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