OCContextMenuFactory.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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 "stdafx.h"
  15. #include "OCContextMenuFactory.h"
  16. #include "OCContextMenu.h"
  17. #include <new>
  18. #include <Shlwapi.h>
  19. #pragma comment(lib, "shlwapi.lib")
  20. extern long g_cDllRef;
  21. OCContextMenuFactory::OCContextMenuFactory() : m_cRef(1)
  22. {
  23. InterlockedIncrement(&g_cDllRef);
  24. }
  25. OCContextMenuFactory::~OCContextMenuFactory()
  26. {
  27. InterlockedDecrement(&g_cDllRef);
  28. }
  29. // IUnknown methods
  30. IFACEMETHODIMP OCContextMenuFactory::QueryInterface(REFIID riid, void **ppv)
  31. {
  32. static const QITAB qit[] = { QITABENT(OCContextMenuFactory, IClassFactory), { 0 }, };
  33. return QISearch(this, qit, riid, ppv);
  34. }
  35. IFACEMETHODIMP_(ULONG) OCContextMenuFactory::AddRef()
  36. {
  37. return InterlockedIncrement(&m_cRef);
  38. }
  39. IFACEMETHODIMP_(ULONG) OCContextMenuFactory::Release()
  40. {
  41. ULONG cRef = InterlockedDecrement(&m_cRef);
  42. if (0 == cRef) {
  43. delete this;
  44. }
  45. return cRef;
  46. }
  47. // IClassFactory methods
  48. IFACEMETHODIMP OCContextMenuFactory::CreateInstance(IUnknown *pUnkOuter, REFIID riid, void **ppv)
  49. {
  50. HRESULT hr = CLASS_E_NOAGGREGATION;
  51. // pUnkOuter is used for aggregation. We do not support it in the sample.
  52. if (!pUnkOuter) {
  53. hr = E_OUTOFMEMORY;
  54. // Create the COM component.
  55. OCContextMenu *pExt = new (std::nothrow) OCContextMenu();
  56. if (pExt) {
  57. // Query the specified interface.
  58. hr = pExt->QueryInterface(riid, ppv);
  59. pExt->Release();
  60. }
  61. }
  62. return hr;
  63. }
  64. IFACEMETHODIMP OCContextMenuFactory::LockServer(BOOL fLock)
  65. {
  66. if (fLock) {
  67. InterlockedIncrement(&g_cDllRef);
  68. } else {
  69. InterlockedDecrement(&g_cDllRef);
  70. }
  71. return S_OK;
  72. }