strmbase: Move BasePin implementation to strmbase.
[wine] / dlls / qcap / dllsetup.c
1 /*
2  * DirectX DLL registration and unregistration
3  *
4  * Copyright (C) 2005 Rolf Kalbermatter
5 *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * 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 #include "config.h"
21
22 #include <stdarg.h>
23 #include <assert.h>
24
25 #define COBJMACROS
26 #define NONAMELESSSTRUCT
27 #define NONAMELESSUNION
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "wingdi.h"
32 #include "winerror.h"
33 #include "winreg.h"
34 #include "objbase.h"
35 #include "uuids.h"
36
37 #include "dllsetup.h"
38
39 #include "wine/unicode.h"
40 #include "wine/debug.h"
41
42 WINE_DEFAULT_DEBUG_CHANNEL(qcap);
43
44 /*
45  * defines and constants
46  */
47 #define MAX_KEY_LEN  260
48
49 static WCHAR const clsid_keyname[6] =
50 {'C','L','S','I','D',0 };
51 static WCHAR const ips32_keyname[15] =
52 {'I','n','P','r','o','c','S','e','r','v','e','r','3','2',0};
53 static WCHAR const tmodel_keyname[15] =
54 {'T','h','r','e','a','d','i','n','g','M','o','d','e','l',0};
55 static WCHAR const tmodel_both[] =
56 {'B','o','t','h',0};
57
58 /*
59  * SetupRegisterClass()
60  */
61 static HRESULT SetupRegisterClass(HKEY clsid, LPCWSTR szCLSID,
62                                   LPCWSTR szDescription,
63                                   LPCWSTR szFileName,
64                                   LPCWSTR szServerType,
65                                   LPCWSTR szThreadingModel)
66 {
67     HKEY hkey, hsubkey = NULL;
68     LONG ret = RegCreateKeyW(clsid, szCLSID, &hkey);
69     if (ERROR_SUCCESS != ret)
70         return HRESULT_FROM_WIN32(ret);
71
72     /* set description string */
73     ret = RegSetValueW(hkey, NULL, REG_SZ, szDescription,
74                        sizeof(WCHAR) * (lstrlenW(szDescription) + 1));
75     if (ERROR_SUCCESS != ret)
76         goto err_out;
77
78     /* create CLSID\\{"CLSID"}\\"ServerType" key, using key to CLSID\\{"CLSID"}
79        passed back by last call to RegCreateKeyW(). */
80     ret = RegCreateKeyW(hkey,  szServerType, &hsubkey);
81     if (ERROR_SUCCESS != ret)
82         goto err_out;
83
84     /* set server path */
85     ret = RegSetValueW(hsubkey, NULL, REG_SZ, szFileName,
86                        sizeof(WCHAR) * (lstrlenW(szFileName) + 1));
87     if (ERROR_SUCCESS != ret)
88         goto err_out;
89
90     /* set threading model */
91     ret = RegSetValueExW(hsubkey, tmodel_keyname, 0L, REG_SZ,
92                          (const BYTE*)szThreadingModel, 
93                          sizeof(WCHAR) * (lstrlenW(szThreadingModel) + 1));
94 err_out:
95     if (hsubkey)
96         RegCloseKey(hsubkey);
97     RegCloseKey(hkey);
98     return HRESULT_FROM_WIN32(ret);
99 }
100
101 /*
102  * RegisterAllClasses()
103  */
104 static HRESULT SetupRegisterAllClasses(const CFactoryTemplate * pList, int num,
105                                        LPCWSTR szFileName, BOOL bRegister)
106 {
107     HRESULT hr = NOERROR;
108     HKEY hkey;
109     OLECHAR szCLSID[CHARS_IN_GUID];
110     LONG i, ret = RegCreateKeyW(HKEY_CLASSES_ROOT, clsid_keyname, &hkey);
111     if (ERROR_SUCCESS != ret)
112         return HRESULT_FROM_WIN32(ret);
113
114     for (i = 0; i < num; i++, pList++)
115     {
116         /* (un)register CLSID and InprocServer32 */
117         hr = StringFromGUID2(pList->m_ClsID, szCLSID, CHARS_IN_GUID);
118         if (SUCCEEDED(hr))
119         {
120             if (bRegister )
121                 hr = SetupRegisterClass(hkey, szCLSID,
122                                         pList->m_Name, szFileName,
123                                         ips32_keyname, tmodel_both);
124             else
125                 hr = RegDeleteTreeW(hkey, szCLSID);
126         }
127     }
128     RegCloseKey(hkey);
129     return hr;
130 }
131
132
133 /****************************************************************************
134  * SetupRegisterServers
135  *
136  * This function is table driven using the static members of the
137  * CFactoryTemplate class defined in the Dll.
138  *
139  * It registers the Dll as the InprocServer32 for all the classes in
140  * CFactoryTemplate
141  *
142  ****************************************************************************/
143 HRESULT SetupRegisterServers(const CFactoryTemplate * pList, int num,
144                              BOOL bRegister)
145 {
146     static const WCHAR szFileName[] = {'q','c','a','p','.','d','l','l',0};
147     HRESULT hr = NOERROR;
148     IFilterMapper2 *pIFM2 = NULL;
149
150     /* first register all server classes, just to make sure */
151     if (bRegister)
152         hr = SetupRegisterAllClasses(pList, num, szFileName, TRUE );
153
154     /* next, register/unregister all filters */
155     if (SUCCEEDED(hr))
156     {
157         hr = CoInitialize(NULL);
158
159         TRACE("Getting IFilterMapper2\r\n");
160         hr = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC_SERVER,
161                               &IID_IFilterMapper2, (void **)&pIFM2);
162
163         if (SUCCEEDED(hr))
164         {
165             int i;
166             
167             /* scan through array of CFactoryTemplates registering all filters */
168             for (i = 0; i < num; i++, pList++)
169             {
170                 if (pList->m_pAMovieSetup_Filter.dwVersion)
171                 {
172                     hr = IFilterMapper2_RegisterFilter(pIFM2, pList->m_ClsID, pList->m_Name, NULL, &CLSID_LegacyAmFilterCategory, NULL, &pList->m_pAMovieSetup_Filter);
173                 }
174
175                 /* check final error for this pass and break loop if we failed */
176                 if (FAILED(hr))
177                     break;
178             }
179
180             /* release interface */
181             IFilterMapper2_Release(pIFM2);
182         }
183
184         /* and clear up */
185         CoFreeUnusedLibraries();
186         CoUninitialize();
187     }
188
189     /* if unregistering, unregister all OLE servers */
190     if (SUCCEEDED(hr) && !bRegister)
191         hr = SetupRegisterAllClasses(pList, num, szFileName, FALSE);
192     return hr;
193 }
194
195 /****************************************************************************
196  * SetupInitializeServers
197  *
198  * This function is table driven using the static members of the
199  * CFactoryTemplate class defined in the Dll.
200  *
201  * It calls the initialize function for any class in CFactoryTemplate with
202  * one defined.
203  *
204  ****************************************************************************/
205 void SetupInitializeServers(const CFactoryTemplate * pList, int num,
206                             BOOL bLoading)
207 {
208     int i;
209
210     for (i = 0; i < num; i++, pList++)
211     {
212         if (pList->m_lpfnInit)
213             pList->m_lpfnInit(bLoading, pList->m_ClsID);
214     }
215 }