Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

191 lines
6.3 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1997-2002.
  5. //
  6. // File: SendCMsg.cpp
  7. //
  8. // Contents:
  9. //
  10. //----------------------------------------------------------------------------
  11. // SendCMsg.cpp : Implementation of DLL Exports.
  12. // Note: Proxy/Stub Information
  13. // To build a separate proxy/stub DLL,
  14. // run nmake -f SendCMsgps.mk in the project directory.
  15. #include "stdafx.h"
  16. #define STRSAFE_NO_DEPRECATE
  17. #include <strsafe.h>
  18. #include "initguid.h"
  19. #include "SendCMsg.h"
  20. #include "SendCMsg_i.c"
  21. #include "debug.h"
  22. #include "util.h"
  23. #include "resource.h"
  24. #include "App.h"
  25. #include <atlimpl.cpp>
  26. HINSTANCE g_hInstance;
  27. CComModule _Module;
  28. BEGIN_OBJECT_MAP(ObjectMap)
  29. OBJECT_ENTRY(CLSID_SendConsoleMessageApp, CSendConsoleMessageApp)
  30. END_OBJECT_MAP()
  31. // GUID for the CSendConsoleMessageApp class
  32. #define d_szGuidSendConsoleMessageApp _T("{B1AFF7D0-0C49-11D1-BB12-00C04FC9A3A3}")
  33. #if 0
  34. // To have sendcmsg.dll to extend your context menu, add the following
  35. // key into the registry
  36. //
  37. [HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\MMC\NodeTypes\
  38. {476e6448-aaff-11d0-b944-00c04fd8d5b0}\Extensions\ContextMenu]
  39. "{B1AFF7D0-0C49-11D1-BB12-00C04FC9A3A3}"="Send Console Message"
  40. // where {476e6448-aaff-11d0-b944-00c04fd8d5b0} is
  41. // the GUID for the nodetype which you want to be extended.
  42. #endif
  43. // The following is an array of GUIDs of snapins that wants to be
  44. // automatically extended by the Send Console Message Snapin.
  45. // When the snapin registers itself, it will extend those nodetypes.
  46. const PCWSTR rgzpszGuidNodetypeContextMenuExtensions[] =
  47. {
  48. _T("{476e6446-aaff-11d0-b944-00c04fd8d5b0}"), // Computer Management
  49. _T("{4e410f0e-abc1-11d0-b944-00c04fd8d5b0}"), // Root of File Service Management subtree
  50. _T("{4e410f0f-abc1-11d0-b944-00c04fd8d5b0}"), // FSM - Shares
  51. _T("{4e410f12-abc1-11d0-b944-00c04fd8d5b0}"), // System Service Management
  52. };
  53. // The following is an array of GUIDs of snapins that no longer want
  54. // to be automatically extended by the Send Console Message Snapin.
  55. const PCWSTR rgzpszRemoveContextMenuExtensions[] =
  56. {
  57. _T("{476e6448-aaff-11d0-b944-00c04fd8d5b0}"), // Computer Management -> SystemTools
  58. _T("{0eeeeeee-d390-11cf-b607-00c04fd8d565}"), // invalid
  59. _T("{1eeeeeee-d390-11cf-b607-00c04fd8d565}"), // invalid
  60. _T("{7eeeeeee-d390-11cf-b607-00c04fd8d565}"), // invalid
  61. };
  62. /////////////////////////////////////////////////////////////////////////////
  63. // DLL Entry Point
  64. extern "C"
  65. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  66. {
  67. g_hInstance = hInstance;
  68. if (dwReason == DLL_PROCESS_ATTACH)
  69. {
  70. _Module.Init(ObjectMap, hInstance);
  71. DisableThreadLibraryCalls(hInstance);
  72. }
  73. else if (dwReason == DLL_PROCESS_DETACH)
  74. _Module.Term();
  75. return TRUE; // ok
  76. }
  77. /////////////////////////////////////////////////////////////////////////////
  78. // Used to determine whether the DLL can be unloaded by OLE
  79. STDAPI DllCanUnloadNow(void)
  80. {
  81. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  82. }
  83. /////////////////////////////////////////////////////////////////////////////
  84. // Returns a class factory to create an object of the requested type
  85. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  86. {
  87. return _Module.GetClassObject(rclsid, riid, ppv);
  88. }
  89. /////////////////////////////////////////////////////////////////////////////
  90. // DllRegisterServer - Adds entries to the system registry
  91. STDAPI DllRegisterServer(void)
  92. {
  93. // registers object, typelib and all interfaces in typelib
  94. HRESULT hr = _Module.RegisterServer(TRUE);
  95. HKEY hkey = RegOpenOrCreateKey(
  96. HKEY_LOCAL_MACHINE,
  97. _T("Software\\Microsoft\\MMC\\SnapIns\\") d_szGuidSendConsoleMessageApp);
  98. if (hkey == NULL)
  99. {
  100. Assert(FALSE && "DllRegisterServer() - Unable to create key from registry.");
  101. return SELFREG_E_CLASS;
  102. }
  103. RegWriteString(hkey, _T("NameString"), IDS_CAPTION);
  104. RegCloseKey(hkey);
  105. const PWSTR pwszREG_FORMAT_KEY = L"Software\\Microsoft\\MMC\\NodeTypes\\%s\\Extensions\\ContextMenu";
  106. for (int i = 0; i < LENGTH(rgzpszGuidNodetypeContextMenuExtensions); i++)
  107. {
  108. WCHAR szRegistryKey[256];
  109. Assert(rgzpszGuidNodetypeContextMenuExtensions[i] != NULL);
  110. // security review 3/1/2002 BryanWal
  111. // ISSUE - potential buffer overflow - use wsnprintf or strsafe
  112. hr = ::StringCchPrintf (OUT szRegistryKey, sizeof (szRegistryKey)/sizeof (szRegistryKey[0]),
  113. pwszREG_FORMAT_KEY,
  114. rgzpszGuidNodetypeContextMenuExtensions[i]);
  115. Assert (SUCCEEDED (hr));
  116. if ( SUCCEEDED (hr) )
  117. {
  118. Assert(wcslen(szRegistryKey) < LENGTH(szRegistryKey));
  119. hkey = ::RegOpenOrCreateKey(HKEY_LOCAL_MACHINE, szRegistryKey);
  120. if (hkey == NULL)
  121. {
  122. Assert(FALSE && "DllRegisterServer() - Unable to create key from registry.");
  123. continue;
  124. }
  125. ::RegWriteString(hkey, d_szGuidSendConsoleMessageApp, IDS_CAPTION);
  126. ::RegCloseKey(hkey);
  127. }
  128. } // for
  129. for (i = 0; i < LENGTH(rgzpszRemoveContextMenuExtensions); i++)
  130. {
  131. WCHAR szRegistryKey[256];
  132. Assert(rgzpszRemoveContextMenuExtensions[i] != NULL);
  133. // security review 3/1/2002 BryanWal
  134. // ISSUE - potential buffer overflow - use wsnprintf or strsafe
  135. hr = ::StringCchPrintf (OUT szRegistryKey, sizeof (szRegistryKey)/sizeof (szRegistryKey[0]),
  136. pwszREG_FORMAT_KEY,
  137. rgzpszRemoveContextMenuExtensions[i]);
  138. Assert (SUCCEEDED (hr));
  139. if ( SUCCEEDED (hr) )
  140. {
  141. Assert(wcslen(szRegistryKey) < LENGTH(szRegistryKey));
  142. (void) RegOpenKey(HKEY_LOCAL_MACHINE, szRegistryKey, &hkey);
  143. if (hkey == NULL)
  144. {
  145. // not a problem
  146. continue;
  147. }
  148. (void) RegDeleteValue(hkey, d_szGuidSendConsoleMessageApp);
  149. // ignore error code, the only likely code is ERROR_FILE_NOT_FOUND
  150. RegCloseKey(hkey);
  151. hkey = NULL;
  152. }
  153. } // for
  154. return hr;
  155. } // DllRegisterServer()
  156. /////////////////////////////////////////////////////////////////////////////
  157. // DllUnregisterServer - Removes entries from the system registry
  158. STDAPI DllUnregisterServer(void)
  159. {
  160. _Module.UnregisterServer();
  161. return S_OK;
  162. }