sbWindowsFormatterServer.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 :miv */
3 /*
4  *=BEGIN SONGBIRD GPL
5  *
6  * This file is part of the Songbird web player.
7  *
8  * Copyright(c) 2005-2009 POTI, Inc.
9  * http://www.songbirdnest.com
10  *
11  * This file may be licensed under the terms of of the
12  * GNU General Public License Version 2 (the ``GPL'').
13  *
14  * Software distributed under the License is distributed
15  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
16  * express or implied. See the GPL for the specific language
17  * governing rights and limitations.
18  *
19  * You should have received a copy of the GPL along with this
20  * program. If not, go to http://www.gnu.org/licenses/gpl.html
21  * or write to the Free Software Foundation, Inc.,
22  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23  *
24  *=END SONGBIRD GPL
25  */
26 
27 //------------------------------------------------------------------------------
28 //
29 // Songbird Windows formatter component COM server.
30 //
31 //------------------------------------------------------------------------------
32 
38 //------------------------------------------------------------------------------
39 //
40 // Songbird Windows formatter component COM server imported services.
41 //
42 //------------------------------------------------------------------------------
43 
44 // Self imports.
46 
47 // Local imports.
48 #include "sbWindowsFormatter.h"
50 
51 // Songbird imports.
52 #include <sbWindowsUtils.h>
53 
54 
55 //------------------------------------------------------------------------------
56 //
57 // Songbird Windows formatter component COM server globals.
58 //
59 //------------------------------------------------------------------------------
60 
61 //
62 // gCOMServerLockCount Count of the number of locks on the COM server.
63 // gDLLModule Component DLL module.
64 //
65 
67 HINSTANCE gDLLModule = NULL;
68 
69 
70 //------------------------------------------------------------------------------
71 //
72 // Songbird Windows formatter component COM server DLL services.
73 //
74 //------------------------------------------------------------------------------
75 
87 BOOL APIENTRY
88 DllMain(HINSTANCE aModule,
89  DWORD aReasonForCall,
90  LPVOID aReserved)
91 {
92  // Dispatch processing of call.
93  switch (aReasonForCall)
94  {
95  case DLL_PROCESS_ATTACH :
96  // Get the DLL module.
97  gDLLModule = aModule;
98 
99  // Don't need thread notification callbacks.
100  DisableThreadLibraryCalls(aModule);
101 
102  break;
103  }
104 
105  return TRUE;
106 }
107 
108 
116 STDAPI
118 {
119  // Can only unload DLL if COM server is not locked.
120  if (gCOMServerLockCount > 0)
121  return S_FALSE;
122  return S_OK;
123 }
124 
125 
135 STDAPI
136 DllGetClassObject(REFCLSID aCLSID,
137  REFIID aIID,
138  void** aClassObject)
139 {
140  HRESULT result;
141 
142  // Only sbWindowsFormatter objects are supported.
143  if (!InlineIsEqualGUID(aCLSID, CLSID_sbWindowsFormatter))
144  return CLASS_E_CLASSNOTAVAILABLE;
145 
146  // Validate return arguments.
147  if (IsBadWritePtr(aClassObject, sizeof(void*)))
148  return E_POINTER;
149 
150  // Set result to NULL.
151  *aClassObject = NULL;
152 
153  // Create a new addref'ed sbWindowsFormatter object and set it up for
154  // auto-release.
156  if (!factory)
157  return E_OUTOFMEMORY;
158  factory->AddRef();
159  sbAutoIUnknown autoFactory(factory);
160 
161  // Get the requested interface.
162  result = factory->QueryInterface(aIID, aClassObject);
163  if (!SUCCEEDED(result))
164  return result;
165 
166  return S_OK;
167 }
168 
169 
174 STDAPI
176 {
177  TCHAR appIDGUID[] = _T(CLSIDStr_sbWindowsFormatter);
178  LONG result;
179 
180  // Get the COM server DLL module path.
181  TCHAR dllModulePath[MAX_PATH];
182  DWORD length;
183  length = GetModuleFileName(gDLLModule, dllModulePath, MAX_PATH);
184  if ((length <= 0) || (length == MAX_PATH))
185  return HRESULT_FROM_WIN32(GetLastError());
186 
187 
188  //
189  // HKCR\CLSID\SB_WINDOWS_FORMATTER_CLSID
190  //
191 
192  // Create the Songbird Windows formatter class ID registry key and set it up
193  // to auto-close.
194  HKEY clsidKey;
195  result = RegCreateKeyEx(HKEY_CLASSES_ROOT,
196  _T("CLSID\\") _T(CLSIDStr_sbWindowsFormatter),
197  0,
198  NULL,
199  REG_OPTION_NON_VOLATILE,
200  KEY_SET_VALUE | KEY_CREATE_SUB_KEY,
201  NULL,
202  &clsidKey,
203  NULL);
204  if (result != ERROR_SUCCESS)
205  return HRESULT_FROM_WIN32(result);
206  sbAutoRegKey autoCLSIDKey(clsidKey);
207 
208  // Set the class description.
209  TCHAR classDescription[] = _T("sbWindowsFormatter class");
210  result = RegSetValueEx(clsidKey,
211  NULL,
212  0,
213  REG_SZ,
214  reinterpret_cast<const BYTE*>(classDescription),
215  sizeof(classDescription));
216  if (result != ERROR_SUCCESS)
217  return HRESULT_FROM_WIN32(result);
218 
219  // Set the application ID GUID.
220  result = RegSetValueEx(clsidKey,
221  _T("AppID"),
222  0,
223  REG_SZ,
224  reinterpret_cast<const BYTE*>(appIDGUID),
225  sizeof(appIDGUID));
226  if (result != ERROR_SUCCESS)
227  return HRESULT_FROM_WIN32(result);
228 
229  // Set the localized string. This must be a MUI string. If not, elevation
230  // will fail with an invalid data error.
231  TCHAR localizedString[MAX_PATH + 32];
232  StringCchPrintf(localizedString,
233  sizeof(localizedString) / sizeof(TCHAR),
234  _T("@%s,-1000"),
235  dllModulePath);
236  result = RegSetValueExW(clsidKey,
237  _T("LocalizedString"),
238  0,
239  REG_SZ,
240  reinterpret_cast<const BYTE*>(localizedString),
241  (_tcsclen(localizedString) + 1) * sizeof(TCHAR));
242  if (result != ERROR_SUCCESS)
243  return HRESULT_FROM_WIN32(result);
244 
245 
246  //
247  // HKCR\CLSID\SB_WINDOWS_FORMATTER_CLSID\InProcServer32
248  //
249 
250  // Create the in-process server key and set it up to auto-close.
251  HKEY inProcServerKey;
252  result = RegCreateKeyEx(clsidKey,
253  _T("InProcServer32"),
254  0,
255  NULL,
256  REG_OPTION_NON_VOLATILE,
257  KEY_SET_VALUE,
258  NULL,
259  &inProcServerKey,
260  NULL);
261  if (result != ERROR_SUCCESS)
262  return HRESULT_FROM_WIN32(result);
263  sbAutoRegKey autoInProcServerKey(inProcServerKey);
264 
265  // Set the COM server DLL module path in the registry.
266  result = RegSetValueEx(inProcServerKey,
267  NULL,
268  0,
269  REG_SZ,
270  reinterpret_cast<const BYTE*>(dllModulePath),
271  sizeof(TCHAR) * (lstrlen(dllModulePath) + 1));
272  if (result != ERROR_SUCCESS)
273  return HRESULT_FROM_WIN32(result);
274 
275  // Set the COM server threading model.
276  TCHAR threadingModel[] = _T("Apartment");
277  result = RegSetValueEx(inProcServerKey,
278  _T("ThreadingModel"),
279  0,
280  REG_SZ,
281  reinterpret_cast<const BYTE*>(threadingModel),
282  sizeof(threadingModel));
283  if (result != ERROR_SUCCESS)
284  return HRESULT_FROM_WIN32(result);
285 
286 
287  //
288  // HKCR\CLSID\SB_WINDOWS_FORMATTER_CLSID\Elevation
289  //
290 
291  // Create the elevation key and set it up to auto-close.
292  HKEY elevationKey;
293  result = RegCreateKeyEx(clsidKey,
294  _T("Elevation"),
295  0,
296  NULL,
297  REG_OPTION_NON_VOLATILE,
298  KEY_SET_VALUE,
299  NULL,
300  &elevationKey,
301  NULL);
302  if (result != ERROR_SUCCESS)
303  return HRESULT_FROM_WIN32(result);
304  sbAutoRegKey autoElevationKey(elevationKey);
305 
306  // Set elevation enabled.
307  DWORD elevationEnabled = 1;
308  result = RegSetValueEx(elevationKey,
309  _T("Enabled"),
310  0,
311  REG_DWORD,
312  reinterpret_cast<const BYTE*>(&elevationEnabled),
313  sizeof(elevationEnabled));
314  if (result != ERROR_SUCCESS)
315  return HRESULT_FROM_WIN32(result);
316 
317 
318  //
319  // HKCR\CLSID\SB_WINDOWS_FORMATTER_CLSID\TypeLib
320  //
321 
322  // Create the type library key and set it up to auto-close.
323  HKEY typeLibKey;
324  result = RegCreateKeyEx(clsidKey,
325  _T("TypeLib"),
326  0,
327  NULL,
328  REG_OPTION_NON_VOLATILE,
329  KEY_SET_VALUE,
330  NULL,
331  &typeLibKey,
332  NULL);
333  if (result != ERROR_SUCCESS)
334  return HRESULT_FROM_WIN32(result);
335  sbAutoRegKey autoTypeLibKey(typeLibKey);
336 
337  // Set type library ID.
338  TCHAR typeLibID[] = _T(LIBIDStr_sbWindowsFormatterLibrary);
339  result = RegSetValueEx(typeLibKey,
340  NULL,
341  0,
342  REG_SZ,
343  reinterpret_cast<const BYTE*>(typeLibID),
344  sizeof(typeLibID));
345  if (result != ERROR_SUCCESS)
346  return HRESULT_FROM_WIN32(result);
347 
348 
349  //
350  // HKCR\AppID\SB_WINDOWS_FORMATTER_CLSID
351  //
352 
353  // Create the Songbird Windows formatter application ID GUID registry key and
354  // set it up to auto-close.
355  HKEY appIDGUIDKey;
356  result = RegCreateKeyEx(HKEY_CLASSES_ROOT,
357  _T("AppID\\") _T(CLSIDStr_sbWindowsFormatter),
358  0,
359  NULL,
360  REG_OPTION_NON_VOLATILE,
361  KEY_SET_VALUE | KEY_CREATE_SUB_KEY,
362  NULL,
363  &appIDGUIDKey,
364  NULL);
365  if (result != ERROR_SUCCESS)
366  return HRESULT_FROM_WIN32(result);
367  sbAutoRegKey autoAppIDGUIDKey(appIDGUIDKey);
368 
369  // Set the app description.
370  TCHAR appDescription[] = _T("Songbird Windows Formatter");
371  result = RegSetValueEx(appIDGUIDKey,
372  NULL,
373  0,
374  REG_SZ,
375  reinterpret_cast<const BYTE*>(appDescription),
376  sizeof(appDescription));
377  if (result != ERROR_SUCCESS)
378  return HRESULT_FROM_WIN32(result);
379 
380  // Set the DLL surrogate registry value.
381  TCHAR dllSurrogate[] = _T("");
382  result = RegSetValueEx(appIDGUIDKey,
383  _T("DllSurrogate"),
384  0,
385  REG_SZ,
386  reinterpret_cast<const BYTE*>(dllSurrogate),
387  sizeof(dllSurrogate));
388  if (result != ERROR_SUCCESS)
389  return HRESULT_FROM_WIN32(result);
390 
391 
392  //
393  // HKCR\AppID\sbWindowsFormatter.dll
394  //
395 
396  // Create the Songbird Windows formatter application ID executable registry
397  // key and set it up to auto-close.
398  HKEY appIDExecutableKey;
399  result = RegCreateKeyEx(HKEY_CLASSES_ROOT,
400  _T("AppID\\sbWindowsFormatter.dll"),
401  0,
402  NULL,
403  REG_OPTION_NON_VOLATILE,
404  KEY_SET_VALUE | KEY_CREATE_SUB_KEY,
405  NULL,
406  &appIDExecutableKey,
407  NULL);
408  if (result != ERROR_SUCCESS)
409  return HRESULT_FROM_WIN32(result);
410  sbAutoRegKey autoAppIDExecutableKey(appIDExecutableKey);
411 
412  // Set the application ID GUID.
413  result = RegSetValueEx(appIDExecutableKey,
414  _T("AppID"),
415  0,
416  REG_SZ,
417  reinterpret_cast<const BYTE*>(appIDGUID),
418  sizeof(appIDGUID));
419  if (result != ERROR_SUCCESS)
420  return HRESULT_FROM_WIN32(result);
421 
422 
423  // Register the Songbird Windows formatter type library and set it up for
424  // auto-disposal.
425  ITypeLib* typeLib;
426  result = LoadTypeLib(dllModulePath, &typeLib);
427  if (result != ERROR_SUCCESS)
428  return HRESULT_FROM_WIN32(result);
429  sbAutoIUnknown autoTypeLib(typeLib);
430  result = RegisterTypeLib(typeLib, dllModulePath, NULL);
431  if (result != ERROR_SUCCESS)
432  return HRESULT_FROM_WIN32(result);
433 
434  return S_OK;
435 }
436 
437 
442 STDAPI
444 {
445  // Unregister the Songbird Windows formatter type library.
446  UnRegisterTypeLib(LIBID_sbWindowsFormatterLibrary,
447  1,
448  0,
449  0,
450  SYS_WIN32);
451 
452  // Delete HKCR\CLSID\SB_WINDOWS_FORMATTER_CLSID
453  RegDeleteKey(HKEY_CLASSES_ROOT,
454  _T("CLSID\\")
455  _T(CLSIDStr_sbWindowsFormatter)
456  _T("\\TypeLib"));
457  RegDeleteKey(HKEY_CLASSES_ROOT,
458  _T("CLSID\\")
459  _T(CLSIDStr_sbWindowsFormatter)
460  _T("\\Elevation"));
461  RegDeleteKey(HKEY_CLASSES_ROOT,
462  _T("CLSID\\")
463  _T(CLSIDStr_sbWindowsFormatter)
464  _T("\\InProcServer32"));
465  RegDeleteKey(HKEY_CLASSES_ROOT,
466  _T("CLSID\\") _T(CLSIDStr_sbWindowsFormatter));
467 
468  // Delete HKCR\AppID\SB_WINDOWS_FORMATTER_CLSID
469  RegDeleteKey(HKEY_CLASSES_ROOT,
470  _T("AppID\\") _T(CLSIDStr_sbWindowsFormatter));
471 
472  // Delete HKCR\AppID\sbWindowsFormatter.dll
473  RegDeleteKey(HKEY_CLASSES_ROOT, _T("AppID\\sbWindowsFormatter.dll"));
474 
475  return S_OK;
476 }
477 
478 
479 //------------------------------------------------------------------------------
480 //
481 // Songbird Windows formatter component COM server services.
482 //
483 //------------------------------------------------------------------------------
484 
489 void
491 {
493 }
494 
495 
500 void
502 {
504 }
505 
506 
UINT gCOMServerLockCount
STDAPI DllRegisterServer()
Songbird Windows Formatter Class Factory Definitions.
STDAPI DllGetClassObject(REFCLSID aCLSID, REFIID aIID, void **aClassObject)
Songbird Windows Formatter Definitions.
sbDeviceFirmwareAutoCheckForUpdate prototype classDescription
STDMETHOD() QueryInterface(REFIID aIID, void **aInterface)
long LONG
Definition: MultiMonitor.h:38
HINSTANCE gDLLModule
STDAPI DllUnregisterServer()
STDAPI DllCanUnloadNow()
BOOL APIENTRY DllMain(HINSTANCE aModule, DWORD aReasonForCall, LPVOID aReserved)
Songbird Windows Formatter Component COM Server Definitions.
void COMServerUnlock()
void COMServerLock()