dllmain.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 <windows.h>
  16. #include <Guiddef.h>
  17. #include "OCContextMenuRegHandler.h"
  18. #include "OCContextMenuFactory.h"
  19. // {841A0AAD-AA11-4B50-84D9-7F8E727D77D7}
  20. static const GUID CLSID_FileContextMenuExt = { 0x841a0aad, 0xaa11, 0x4b50, { 0x84, 0xd9, 0x7f, 0x8e, 0x72, 0x7d, 0x77, 0xd7 } };
  21. HINSTANCE g_hInst = nullptr;
  22. long g_cDllRef = 0;
  23. BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
  24. {
  25. switch (dwReason)
  26. {
  27. case DLL_PROCESS_ATTACH:
  28. // Hold the instance of this DLL module, we will use it to get the
  29. // path of the DLL to register the component.
  30. g_hInst = hModule;
  31. DisableThreadLibraryCalls(hModule);
  32. break;
  33. case DLL_THREAD_ATTACH:
  34. case DLL_THREAD_DETACH:
  35. case DLL_PROCESS_DETACH:
  36. break;
  37. }
  38. return TRUE;
  39. }
  40. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
  41. {
  42. HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
  43. if (IsEqualCLSID(CLSID_FileContextMenuExt, rclsid)) {
  44. hr = E_OUTOFMEMORY;
  45. OCContextMenuFactory *pClassFactory = new OCContextMenuFactory();
  46. if (pClassFactory) {
  47. hr = pClassFactory->QueryInterface(riid, ppv);
  48. pClassFactory->Release();
  49. }
  50. }
  51. return hr;
  52. }
  53. STDAPI DllCanUnloadNow(void)
  54. {
  55. return g_cDllRef > 0 ? S_FALSE : S_OK;
  56. }
  57. STDAPI DllRegisterServer(void)
  58. {
  59. HRESULT hr;
  60. wchar_t szModule[MAX_PATH];
  61. if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0) {
  62. hr = HRESULT_FROM_WIN32(GetLastError());
  63. return hr;
  64. }
  65. // Register the component.
  66. hr = OCContextMenuRegHandler::RegisterInprocServer(szModule, CLSID_FileContextMenuExt,
  67. L"OCContextMenuHandler Class", L"Apartment");
  68. if (SUCCEEDED(hr)) {
  69. // Register the context menu handler. The context menu handler is
  70. // associated with the .cpp file class.
  71. hr = OCContextMenuRegHandler::RegisterShellExtContextMenuHandler(L"AllFileSystemObjects", CLSID_FileContextMenuExt, L"OCContextMenuHandler");
  72. }
  73. return hr;
  74. }
  75. STDAPI DllUnregisterServer(void)
  76. {
  77. HRESULT hr = S_OK;
  78. wchar_t szModule[MAX_PATH];
  79. if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0) {
  80. hr = HRESULT_FROM_WIN32(GetLastError());
  81. return hr;
  82. }
  83. // Unregister the component.
  84. hr = OCContextMenuRegHandler::UnregisterInprocServer(CLSID_FileContextMenuExt);
  85. if (SUCCEEDED(hr)) {
  86. // Unregister the context menu handler.
  87. hr = OCContextMenuRegHandler::UnregisterShellExtContextMenuHandler(L"AllFileSystemObjects", L"OCContextMenuHandler");
  88. }
  89. return hr;
  90. }