dllmain.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 <windows.h>
  15. #include <Guiddef.h>
  16. #include "NCContextMenuRegHandler.h"
  17. #include "NCContextMenuFactory.h"
  18. #include "WinShellExtConstants.h"
  19. HINSTANCE g_hInst = nullptr;
  20. long g_cDllRef = 0;
  21. BOOL APIENTRY DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpReserved)
  22. {
  23. switch (dwReason)
  24. {
  25. case DLL_PROCESS_ATTACH:
  26. // Hold the instance of this DLL module, we will use it to get the
  27. // path of the DLL to register the component.
  28. g_hInst = hModule;
  29. DisableThreadLibraryCalls(hModule);
  30. break;
  31. case DLL_THREAD_ATTACH:
  32. case DLL_THREAD_DETACH:
  33. case DLL_PROCESS_DETACH:
  34. break;
  35. }
  36. return TRUE;
  37. }
  38. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
  39. {
  40. HRESULT hr = 0;
  41. GUID guid;
  42. hr = CLSIDFromString(CONTEXT_MENU_GUID, (LPCLSID)&guid);
  43. if (!SUCCEEDED(hr)) {
  44. return hr;
  45. }
  46. hr = CLASS_E_CLASSNOTAVAILABLE;
  47. if (IsEqualCLSID(guid, rclsid)) {
  48. hr = E_OUTOFMEMORY;
  49. auto pClassFactory = new NCContextMenuFactory();
  50. if (pClassFactory) {
  51. hr = pClassFactory->QueryInterface(riid, ppv);
  52. pClassFactory->Release();
  53. }
  54. }
  55. return hr;
  56. }
  57. STDAPI DllCanUnloadNow(void)
  58. {
  59. return g_cDllRef > 0 ? S_FALSE : S_OK;
  60. }
  61. STDAPI DllRegisterServer(void)
  62. {
  63. HRESULT hr = 0;
  64. GUID guid;
  65. hr = CLSIDFromString(CONTEXT_MENU_GUID, (LPCLSID)&guid);
  66. if (!SUCCEEDED(hr)) {
  67. return hr;
  68. }
  69. wchar_t szModule[MAX_PATH];
  70. if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0) {
  71. hr = HRESULT_FROM_WIN32(GetLastError());
  72. return hr;
  73. }
  74. // Register the component.
  75. hr = NCContextMenuRegHandler::RegisterInprocServer(szModule, guid,
  76. CONTEXT_MENU_DESCRIPTION, L"Apartment");
  77. if (SUCCEEDED(hr)) {
  78. // Register the context menu handler. The context menu handler is
  79. // associated with the .cpp file class.
  80. hr = NCContextMenuRegHandler::RegisterShellExtContextMenuHandler(L"AllFileSystemObjects", guid, CONTEXT_MENU_REGKEY_NAME);
  81. }
  82. return hr;
  83. }
  84. STDAPI DllUnregisterServer(void)
  85. {
  86. HRESULT hr = S_OK;
  87. GUID guid;
  88. hr = CLSIDFromString(CONTEXT_MENU_GUID, (LPCLSID)&guid);
  89. if (!SUCCEEDED(hr)) {
  90. return hr;
  91. }
  92. wchar_t szModule[MAX_PATH];
  93. if (GetModuleFileName(g_hInst, szModule, ARRAYSIZE(szModule)) == 0) {
  94. hr = HRESULT_FROM_WIN32(GetLastError());
  95. return hr;
  96. }
  97. // Unregister the component.
  98. hr = NCContextMenuRegHandler::UnregisterInprocServer(guid);
  99. if (SUCCEEDED(hr)) {
  100. // Unregister the context menu handler.
  101. hr = NCContextMenuRegHandler::UnregisterShellExtContextMenuHandler(L"AllFileSystemObjects", CONTEXT_MENU_REGKEY_NAME);
  102. }
  103. return hr;
  104. }