NCContextMenuFactory.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * Copyright (c) 2015 Daniel Molkentin <danimo@owncloud.com>. All rights reserved.
  3. *
  4. * This library is free software; you can redistribute it and/or modify it under
  5. * the terms of the GNU Lesser General Public License as published by the Free
  6. * Software Foundation; either version 2.1 of the License, or (at your option)
  7. * any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  11. * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
  12. * details.
  13. */
  14. #include "NCContextMenuFactory.h"
  15. #include "NCContextMenu.h"
  16. #include <new>
  17. #include <Shlwapi.h>
  18. #pragma comment(lib, "shlwapi.lib")
  19. extern long g_cDllRef;
  20. NCContextMenuFactory::NCContextMenuFactory() : m_cRef(1)
  21. {
  22. InterlockedIncrement(&g_cDllRef);
  23. }
  24. NCContextMenuFactory::~NCContextMenuFactory()
  25. {
  26. InterlockedDecrement(&g_cDllRef);
  27. }
  28. // IUnknown methods
  29. IFACEMETHODIMP NCContextMenuFactory::QueryInterface(REFIID riid, void **ppv)
  30. {
  31. static const QITAB qit[] = { QITABENT(NCContextMenuFactory, IClassFactory), { nullptr }, };
  32. return QISearch(this, qit, riid, ppv);
  33. }
  34. IFACEMETHODIMP_(ULONG) NCContextMenuFactory::AddRef()
  35. {
  36. return InterlockedIncrement(&m_cRef);
  37. }
  38. IFACEMETHODIMP_(ULONG) NCContextMenuFactory::Release()
  39. {
  40. ULONG cRef = InterlockedDecrement(&m_cRef);
  41. if (0 == cRef) {
  42. delete this;
  43. }
  44. return cRef;
  45. }
  46. // IClassFactory methods
  47. IFACEMETHODIMP NCContextMenuFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv)
  48. {
  49. HRESULT hr = CLASS_E_NOAGGREGATION;
  50. // pUnkOuter is used for aggregation. We do not support it in the sample.
  51. if (!pUnkOuter) {
  52. hr = E_OUTOFMEMORY;
  53. // Create the COM component.
  54. auto pExt = new (std::nothrow) NCContextMenu();
  55. if (pExt) {
  56. // Query the specified interface.
  57. hr = pExt->QueryInterface(riid, ppv);
  58. pExt->Release();
  59. }
  60. }
  61. return hr;
  62. }
  63. IFACEMETHODIMP NCContextMenuFactory::LockServer(BOOL fLock)
  64. {
  65. if (fLock) {
  66. InterlockedIncrement(&g_cDllRef);
  67. } else {
  68. InterlockedDecrement(&g_cDllRef);
  69. }
  70. return S_OK;
  71. }