main.cpp 12.9 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 28 29 30 31

#include <comcat.h>
#include <windows.h>
#include <shlwapi.h>

32
#include <tchar.h>
33

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

49 50 51 52
using namespace std;

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

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

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

#define GUID_STRLEN 39

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

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

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

    *ppv = NULL;

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

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

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

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

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

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

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

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

136
    StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
137

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

141
    SHDeleteKey(HKEY_CLASSES_ROOT, progId);
142 143

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

151 152 153 154 155 156 157
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;
158
    if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
159 160 161 162
            NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
        CATID implCategories[] = {
            CATID_Control,
            CATID_PersistsToPropertyBag,
163 164 165
            CATID_InternetAware,
            CATID_SafeForInitializing,
            CATID_SafeForScripting,
166 167 168 169
        };

        pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin,
                sizeof(implCategories)/sizeof(CATID), implCategories);
170 171
        pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin2,
                sizeof(implCategories)/sizeof(CATID), implCategories);
172 173 174
        pcr->Release();
    }

175
    SHDeleteKey(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
176

177
    UnregisterProgID(CLSID_VLCPlugin, 2);
178
    UnregisterProgID(CLSID_VLCPlugin2, 1);
179 180 181 182

    return S_OK;
};

183
static HRESULT RegisterClassID(HKEY hParent, REFCLSID rclsid, unsigned int version, BOOL isDefault, LPCTSTR path, size_t pathLen)
184
{
185
    TCHAR progId[sizeof(PROGID_STR)+16];
186
    _stprintf(progId, TEXT("%s.%u"), PROGID_STR, version);
187

188
    TCHAR description[sizeof(DESCRIPTION)+16];
189
    _stprintf(description, TEXT("%s v%u"), DESCRIPTION, version);
190

191 192
    HKEY hClassKey;
    {
193
        OLECHAR szCLSID[GUID_STRLEN];
194

195
        StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
196

197 198 199 200 201 202
        HKEY hProgKey = keyCreate(HKEY_CLASSES_ROOT, progId);
        if( NULL != hProgKey )
        {
            // default key value
            keySetDef(hProgKey, description);

203
            keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
204
                               szCLSID, sizeof(szCLSID)));
205

206
            //hSubKey = keyClose(keyCreate(hBaseKey, "Insertable"));
207
 
208 209 210 211
            RegCloseKey(hProgKey);
        }
        if( isDefault )
        {
212
            hProgKey = keyCreate(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
213 214 215 216 217
            if( NULL != hProgKey )
            {
                // default key value
                keySetDef(hProgKey, description);

218
                keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
219
                                   szCLSID, sizeof(szCLSID)));
220

221
                keyClose(keySetDef(keyCreate(hProgKey, TEXT("CurVer")),
222
                                   progId));
223 224
            }
        }
225
        hClassKey = keyCreate(hParent, szCLSID);
226
    }
227 228 229
    if( NULL != hClassKey )
    {
        // default key value
230
        keySetDef(hClassKey, description);
231 232

        // Control key value
233
        keyClose(keyCreate(hClassKey, TEXT("Control")));
234

235
        // Insertable key value
236
        //keyClose(keyCreate(hClassKey, TEXT("Insertable")));
237

238
        // ToolboxBitmap32 key value
239
        {
240 241
            std::vector<TCHAR> iconPathBuf(pathLen+3, 0);
            TCHAR* iconPath = &iconPathBuf[0];
242 243
            memcpy(iconPath, path, sizeof(TCHAR)*pathLen);
            _tcscpy(iconPath+pathLen, TEXT(",1"));
244
            keyClose(keySetDef(keyCreate(hClassKey,
245
                TEXT("ToolboxBitmap32")),
246
                iconPath, sizeof(TCHAR)*(_tcslen(iconPath)+1)));
247
        }
248

249 250
#ifdef BUILD_LOCALSERVER
        // LocalServer32 key value
251
        keyClose(keySetDef(keyCreate(hClassKey,
252
            TEXT("LocalServer32"), path, sizeof(TCHAR)*(pathLen+1))));
253
#else
254
        // InprocServer32 key value
255 256
        {
            HKEY hSubKey = keySetDef(keyCreate(hClassKey,
257 258
                TEXT("InprocServer32")),
                path, sizeof(TCHAR)*(pathLen+1));
259
            keySet(hSubKey,
260 261
                TEXT("ThreadingModel"),
                TEXT(THREADING_MODEL), sizeof(TEXT(THREADING_MODEL)));
262 263
            keyClose(hSubKey);
        }
264
#endif
265 266

        // MiscStatus key value
267 268
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("MiscStatus\\1")),
                           TEXT(MISC_STATUS), sizeof(TEXT(MISC_STATUS))));
269 270

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

        // ProgID key value
274
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("ProgID")),progId));
275 276

        // VersionIndependentProgID key value
277
        keyClose(keySetDef(keyCreate(hClassKey,
278
                                     TEXT("VersionIndependentProgID")),
279
                           TEXT(PROGID_STR), sizeof(TEXT(PROGID_STR))));
280 281

        // Version key value
282
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("Version")),TEXT("1.0")));
283 284

        // TypeLib key value
285 286 287 288
        OLECHAR szLIBID[GUID_STRLEN];

        StringFromGUID2(LIBID_AXVLC, szLIBID, GUID_STRLEN);

289 290
        keyClose(keySetDef(keyCreate(hClassKey,TEXT("TypeLib")),
                           szLIBID, sizeof(szLIBID)));
291
 
292 293
        RegCloseKey(hClassKey);
    }
294 295 296 297 298 299 300
    return S_OK;
}

STDAPI DllRegisterServer(VOID)
{
    DllUnregisterServer();

301 302
    TCHAR DllPath[MAX_PATH];
    DWORD DllPathLen=GetModuleFileName(h_instance, DllPath, MAX_PATH) ;
303
    if( 0 == DllPathLen )
304
        return E_UNEXPECTED;
305
    DllPath[MAX_PATH-1] = '\0';
306 307 308

    HKEY hBaseKey;

309 310
    if( ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"),
                                      0, KEY_CREATE_SUB_KEY, &hBaseKey) )
311
        return SELFREG_E_CLASS;
312

313 314
    RegisterClassID(hBaseKey, CLSID_VLCPlugin, 1, FALSE, DllPath, DllPathLen);
    RegisterClassID(hBaseKey, CLSID_VLCPlugin2, 2, TRUE, DllPath, DllPathLen);
315 316 317

    RegCloseKey(hBaseKey);

318 319
    // indicate which component categories we support
    ICatRegister *pcr;
320
    if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
321 322 323 324
            NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
        CATID implCategories[] = {
            CATID_Control,
            CATID_PersistsToPropertyBag,
325 326 327
            CATID_InternetAware,
            CATID_SafeForInitializing,
            CATID_SafeForScripting,
328 329 330 331
        };

        pcr->RegisterClassImplCategories(CLSID_VLCPlugin,
                sizeof(implCategories)/sizeof(CATID), implCategories);
332 333
        pcr->RegisterClassImplCategories(CLSID_VLCPlugin2,
                sizeof(implCategories)/sizeof(CATID), implCategories);
334 335 336
        pcr->Release();
    }

337 338
#ifdef BUILD_LOCALSERVER
    // replace .exe by .tlb
339
    _tcscpy(DllPath+DllPathLen-4, TEXT(".tlb"));
340
#endif
341

342 343 344 345 346
    // register type lib into the registry
    ITypeLib *typeLib;

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

349
    return result;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
};

#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;
    }

369 370 371 372 373 374 375 376
    h_instance = hInst;

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

377 378
    IUnknown *classProc = NULL;

379 380
    if( FAILED(DllGetClassObject(CLSID_VLCPlugin, IID_IUnknown,
                                 (LPVOID *)&classProc)) )
381 382 383
        return 0;
 
    DWORD dwRegisterClassObject;
384
    DWORD dwRegisterClassObject2;
385 386 387 388 389 390 391 392 393 394 395

    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;

396 397 398 399
    if( FAILED(RegisterActiveObject(classProc, CLSID_VLCPlugin2,
                    ACTIVEOBJECT_WEAK, &dwRegisterActiveObject2)) )
        return 0;

400 401 402 403 404 405 406 407 408 409
    classProc->Release();

    /*
    * Polling messages from event queue
    */
    while( S_FALSE == DllCanUnloadNow() )
    {
        while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
        {
            if( msg.message == WM_QUIT )
410
                break;  // break out PeekMessage loop
411 412 413 414 415 416 417 418 419

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

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

        if(msg.message == WM_QUIT)
420
            break;  // break out main loop
421 422 423 424 425 426 427

        WaitMessage();
    }

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

428 429 430
    if( SUCCEEDED(RevokeActiveObject(dwRegisterActiveObject2, NULL)) )
        CoRevokeClassObject(dwRegisterClassObject2);

431
    // Reached on WM_QUIT message
432
    OleUninitialize();
433 434 435 436 437 438 439
    return ((int) msg.wParam);
};

#else

STDAPI_(BOOL) DllMain(HANDLE hModule, DWORD fdwReason, LPVOID lpReserved )
{
440
    (void)lpReserved;
441 442 443 444 445 446 447 448 449 450 451 452 453 454
    switch( fdwReason )
    {
        case DLL_PROCESS_ATTACH:
            h_instance = (HINSTANCE)hModule;
            break;

        default:
            break;
    }
    return TRUE;
};

#endif