dllsetup.c 10.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * DirectX DLL registration and unregistration
 *
 * Copyright (C) 2005 Rolf Kalbermatter
*
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
 */
#include "config.h"

#include <stdarg.h>
#include <assert.h>

#define COBJMACROS
#define NONAMELESSSTRUCT
#define NONAMELESSUNION

#include "windef.h"
#include "winbase.h"
#include "winerror.h"
#include "winreg.h"
#include "objbase.h"
#include "uuids.h"

#include "dllsetup.h"

#include "wine/unicode.h"
#include "wine/debug.h"

WINE_DEFAULT_DEBUG_CHANNEL(qcap);

/*
 * defines and constants
 */
#define MAX_KEY_LEN  260

static WCHAR const clsid_keyname[6] =
{'C','L','S','I','D',0 };
static WCHAR const ips32_keyname[15] =
{'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
static WCHAR const tmodel_keyname[15] =
{'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
static WCHAR const tmodel_both[] =
{'B','o','t','h',0};

/*
 * SetupRegisterClass()
 */
static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
                                  LPCWSTR szDescription,
                                  LPCWSTR szFileName,
                                  LPCWSTR szServerType,
                                  LPCWSTR szThreadingModel)
{
66
    HKEY hkey, hsubkey = NULL;
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
    LONG ret = RegCreateKeyW(clsid, szCLSID, &hkey);
    if (ERROR_SUCCESS != ret)
        return HRESULT_FROM_WIN32(ret);

    /* set description string */
    ret = RegSetValueW(hkey, NULL, REG_SZ, szDescription,
                       sizeof(WCHAR) * (lstrlenW(szDescription) + 1));
    if (ERROR_SUCCESS != ret)
        goto err_out;

    /* create CLSID\\{"CLSID"}\\"ServerType" key, using key to CLSID\\{"CLSID"}
       passed back by last call to RegCreateKeyW(). */
    ret = RegCreateKeyW(hkey,  szServerType, &hsubkey);
    if (ERROR_SUCCESS != ret)
        goto err_out;

    /* set server path */
    ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
                       sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
    if (ERROR_SUCCESS != ret)
        goto err_out;

    /* set threading model */
    ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
                         (const BYTE*)szThreadingModel, 
                         sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
err_out:
    if (hsubkey)
        RegCloseKey(hsubkey);
    RegCloseKey(hkey);
    return HRESULT_FROM_WIN32(ret);
}

/*
 * SetupRegisterFilter through IFilterMapper2
 */
static HRESULT SetupRegisterFilter2(const AMOVIESETUP_FILTER * const pSetup,
                                    IFilterMapper2 * pIFM2, BOOL bRegister)
{
    HRESULT hr;

    if (NULL == pSetup)
        return S_FALSE;

    /* unregister filter */
    hr = IFilterMapper2_UnregisterFilter(pIFM2, 0, 0, pSetup->clsID);

    if (bRegister)
    {
        REGFILTER2 rf2;
        rf2.dwVersion = 1;
        rf2.dwMerit = pSetup->dwMerit;
        rf2.u.s.cPins = pSetup->nPins;
        rf2.u.s.rgPins = pSetup->lpPin;
    
        /* register filter */
        hr = IFilterMapper2_RegisterFilter(pIFM2, pSetup->clsID,
                                           pSetup->strName, 0, 0, NULL, &rf2);
    }
    else
    {
        /* filter not found is ignored here,
           but there is no #define for 0x80070002  */
        if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr)
            hr = NOERROR;
    }
    return hr;
}

/*
 * SetupRegisterFilter through IFilterMapper
 */
static HRESULT SetupRegisterFilter(const AMOVIESETUP_FILTER * const pSetup,
                                   IFilterMapper * pIFM, BOOL bRegister)
{
    HRESULT hr;

    if (NULL == pSetup)
        return S_FALSE;

    /* unregister filter */
    hr = IFilterMapper_UnregisterFilter(pIFM, *pSetup->clsID);

    if (bRegister)
    {
        /* register filter */
        hr = IFilterMapper_RegisterFilter(pIFM, *pSetup->clsID,
                                          pSetup->strName, pSetup->dwMerit);
        if (SUCCEEDED(hr))
        {
            const AMOVIESETUP_PIN *lpPin = pSetup->lpPin;
            const AMOVIESETUP_MEDIATYPE *lpType;
            UINT i, j;

            for (i = 0; i < pSetup->nPins; i++, lpPin++)
            {
                hr = IFilterMapper_RegisterPin(pIFM, *(pSetup->clsID),
                                               lpPin->strName,
                                               lpPin->bRendered,
                                               lpPin->bOutput,
                                               lpPin->bZero,
                                               lpPin->bMany,
                                               *(lpPin->clsConnectsToFilter),
                                               lpPin->strConnectsToPin);

                if (SUCCEEDED(hr))
                {
                    lpType = lpPin->lpMediaType;

                    /* and each pin's media types */
                    for (j = 0; j < lpPin->nMediaTypes; j++, lpType++)
                    {
                        hr = IFilterMapper_RegisterPinType(pIFM, *(pSetup->clsID),
                                                           lpPin->strName,
                                                           *(lpType->clsMajorType),
                                                           *(lpType->clsMinorType));
                        if (FAILED(hr)) break;
                    }
                    if (FAILED(hr)) break;
                }
                if (FAILED(hr)) break;
            }
        }
    }
    else
    {
        /* filter not registered is ignored here, there is no definition for 0x80070002  */
        if (HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND) == hr)
           hr = NOERROR;
    }
    return hr;
}

/*
 * RegisterAllClasses()
 */
static HRESULT SetupRegisterAllClasses(const CFactoryTemplate * pList, int num,
                                       LPCWSTR szFileName, BOOL bRegister)
{
    HRESULT hr = NOERROR;
    HKEY hkey;
    OLECHAR szCLSID[CHARS_IN_GUID];
    LONG i, ret = RegCreateKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkey);
    if (ERROR_SUCCESS != ret)
        return HRESULT_FROM_WIN32(ret);

    for (i = 0; i < num; i++, pList++)
    {
        /* (un)register CLSID and InprocServer32 */
        hr = StringFromGUID2(pList->m_ClsID, szCLSID, CHARS_IN_GUID);
        if (SUCCEEDED(hr))
        {
            if (bRegister )
                hr = SetupRegisterClass(hkey, szCLSID,
                                        pList->m_Name, szFileName,
                                        ips32_keyname, tmodel_both);
            else
224
                hr = RegDeleteTreeW(hkey, szCLSID);
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242
        }
    }
    RegCloseKey(hkey);
    return hr;
}


/****************************************************************************
 * SetupRegisterServers
 *
 * This function is table driven using the static members of the
 * CFactoryTemplate class defined in the Dll.
 *
 * It registers the Dll as the InprocServer32 for all the classes in
 * CFactoryTemplate
 *
 ****************************************************************************/
HRESULT SetupRegisterServers(const CFactoryTemplate * pList, int num,
243
                             BOOL bRegister)
244
{
245
    static const WCHAR szFileName[] = {'q','c','a','p','.','d','l','l',0};
246 247 248 249 250 251 252 253 254 255 256
    HRESULT hr = NOERROR;
    IFilterMapper2 *pIFM2 = NULL;
    IFilterMapper *pIFM = NULL;

    /* first register all server classes, just to make sure */
    if (bRegister)
        hr = SetupRegisterAllClasses(pList, num, szFileName, TRUE );

    /* next, register/unregister all filters */
    if (SUCCEEDED(hr))
    {
Francois Gouget's avatar
Francois Gouget committed
257
        hr = CoInitialize(NULL);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315

        TRACE("Getting IFilterMapper2\r\n");
        hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
                              &IID_IFilterMapper2, (void **)&pIFM2);
        if (FAILED(hr))
        {
            TRACE("- trying IFilterMapper instead\r\n");

            hr = CoCreateInstance(&CLSID_FilterMapper, NULL, CLSCTX_INPROC_SERVER,
                                  &IID_IFilterMapper, (void **)&pIFM);
        }

        if (SUCCEEDED(hr))
        {
            int i;
            
            /* scan through array of CFactoryTemplates registering all filters */
            for (i = 0; i < num; i++, pList++)
            {
                if (NULL != pList->m_pAMovieSetup_Filter)
                {
                    if (pIFM2)
                        hr = SetupRegisterFilter2(pList->m_pAMovieSetup_Filter,
                                                  pIFM2, bRegister);
                    else
                        hr = SetupRegisterFilter(pList->m_pAMovieSetup_Filter,
                                                 pIFM, bRegister);
                }

                /* check final error for this pass and break loop if we failed */
                if (FAILED(hr))
                    break;
            }

            /* release interface */
            if (pIFM2)
                IFilterMapper2_Release(pIFM2);
            else
                IFilterMapper_Release(pIFM);
        }

        /* and clear up */
        CoFreeUnusedLibraries();
        CoUninitialize();
    }

    /* if unregistering, unregister all OLE servers */
    if (SUCCEEDED(hr) && !bRegister)
        hr = SetupRegisterAllClasses(pList, num, szFileName, FALSE);
    return hr;
}

/****************************************************************************
 * SetupInitializeServers
 *
 * This function is table driven using the static members of the
 * CFactoryTemplate class defined in the Dll.
 *
316
 * It calls the initialize function for any class in CFactoryTemplate with
317 318 319 320 321 322 323 324 325 326
 * one defined.
 *
 ****************************************************************************/
void SetupInitializeServers(const CFactoryTemplate * pList, int num,
                            BOOL bLoading)
{
    int i;

    for (i = 0; i < num; i++, pList++)
    {
327 328
        if (pList->m_lpfnInit)
            pList->m_lpfnInit(bLoading, pList->m_ClsID);
329 330
    }
}