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.

229 lines
7.0 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1999 - 1999
  6. //
  7. // File: cic.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // cic.cpp : Implementation of DLL Exports.
  11. // Note: Proxy/Stub Information
  12. // To build a separate proxy/stub DLL,
  13. // run nmake -f cicps.mk in the project directory.
  14. #include "stdafx.h"
  15. #include "resource.h"
  16. #include "initguid.h"
  17. #include "cic.h"
  18. #include "cic_i.c"
  19. #include "MMCCtrl.h"
  20. #include "MMCTask.h"
  21. #include "MMClpi.h"
  22. #include "ListPad.h"
  23. #include "SysColorCtrl.h"
  24. CComModule _Module;
  25. BEGIN_OBJECT_MAP(ObjectMap)
  26. OBJECT_ENTRY(CLSID_MMCCtrl, CMMCCtrl)
  27. OBJECT_ENTRY(CLSID_MMCTask, CMMCTask)
  28. OBJECT_ENTRY(CLSID_MMCListPadInfo, CMMCListPadInfo)
  29. OBJECT_ENTRY(CLSID_ListPad, CListPad)
  30. OBJECT_ENTRY(CLSID_SysColorCtrl, CSysColorCtrl)
  31. END_OBJECT_MAP()
  32. // cut from ndmgr_i.c (yuck) !!!
  33. const IID IID_ITaskPadHost = {0x4f7606d0,0x5568,0x11d1,{0x9f,0xea,0x00,0x60,0x08,0x32,0xdb,0x4a}};
  34. #ifdef DBG
  35. CTraceTag tagCicGetClassObject(TEXT("Cic"), TEXT("DllGetClassObject"));
  36. #endif
  37. /////////////////////////////////////////////////////////////////////////////
  38. // DLL Entry Point
  39. extern "C"
  40. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  41. {
  42. if (dwReason == DLL_PROCESS_ATTACH)
  43. {
  44. _Module.Init(ObjectMap, hInstance);
  45. DisableThreadLibraryCalls(hInstance);
  46. }
  47. else if (dwReason == DLL_PROCESS_DETACH)
  48. _Module.Term();
  49. return TRUE; // ok
  50. }
  51. /////////////////////////////////////////////////////////////////////////////
  52. // Used to determine whether the DLL can be unloaded by OLE
  53. STDAPI DllCanUnloadNow(void)
  54. {
  55. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  56. }
  57. //***************************************************************************
  58. //
  59. // ScGetSystemWow64Directory
  60. //
  61. // PURPOSE: Calls GetSystemWow64DirectoryW using a late bind, to avoid
  62. // requiring the XP version of kernel32.dll
  63. //
  64. // PARAMETERS:
  65. // LPTSTR lpBuffer :
  66. // UINT uSize :
  67. //
  68. // RETURNS:
  69. // SC
  70. //
  71. //****************************************************************************
  72. SC ScGetSystemWow64Directory(LPTSTR lpBuffer, UINT uSize )
  73. {
  74. DECLARE_SC(sc, TEXT("ScGetSystemWow64Directory"));
  75. sc = ScCheckPointers(lpBuffer);
  76. if(sc)
  77. return sc;
  78. HMODULE hmod = GetModuleHandle (_T("kernel32.dll"));
  79. if (hmod == NULL)
  80. return (sc = E_FAIL);
  81. UINT (WINAPI* pfnGetSystemWow64Directory)(LPTSTR, UINT);
  82. (FARPROC&)pfnGetSystemWow64Directory = GetProcAddress (hmod, "GetSystemWow64DirectoryW");
  83. sc = ScCheckPointers(pfnGetSystemWow64Directory, E_FAIL);
  84. if(sc)
  85. return sc;
  86. if ((pfnGetSystemWow64Directory)(lpBuffer, uSize) == 0)
  87. return (sc = E_FAIL);
  88. return sc;
  89. }
  90. //***************************************************************************
  91. //
  92. // DllGetClassObject
  93. //
  94. // PURPOSE: Returns a class factory to create an object of the requested type
  95. // For security reasons, these COM objects can only be instantiated
  96. // within the context of MMC.EXE. If they are instantiated by any
  97. // other host, such as IE, they will fail.
  98. //
  99. // PARAMETERS:
  100. // REFCLSID rclsid :
  101. // REFIID riid :
  102. // LPVOID* ppv :
  103. //
  104. // RETURNS:
  105. // STDAPI - S_OK if the call succeeds
  106. //
  107. //
  108. //****************************************************************************
  109. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  110. {
  111. DECLARE_SC(sc, TEXT("CIC.DLL:DllGetClassObject"));
  112. TCHAR szFileName[MAX_PATH] = {0};
  113. DWORD cchFilename = MAX_PATH;
  114. // 1. Get the filename for the .exe associated with the process
  115. DWORD dw = GetModuleFileName(GetModuleHandle(NULL), szFileName, cchFilename);
  116. Trace(tagCicGetClassObject, TEXT("Process Filename: %s"), szFileName);
  117. if(0==dw)
  118. return sc.FromLastError().ToHr();
  119. // 2. Build the path to where MMC.EXE should be
  120. const int cchMMCPathName = MAX_PATH;
  121. TCHAR szMMCPathName[cchMMCPathName] = {0};
  122. UINT nLength = GetSystemDirectory(szMMCPathName, cchMMCPathName);
  123. if(0==nLength)
  124. return (sc = E_FAIL).ToHr();
  125. LPCTSTR szMMC = TEXT("\\MMC.EXE");
  126. sc = StringCchCat(szMMCPathName, cchMMCPathName, szMMC);
  127. if(sc)
  128. return sc.ToHr();
  129. // 3. Canonicalize by converting both paths to long path names
  130. const DWORD cchLongPath1 = MAX_PATH;
  131. const DWORD cchLongPath2 = MAX_PATH;
  132. TCHAR szLongPath1[cchLongPath1], szLongPath2[cchLongPath2];
  133. DWORD dw1 = GetLongPathName(szMMCPathName, szLongPath1, cchLongPath1);
  134. if(0==dw1)
  135. return sc.FromLastError().ToHr();
  136. DWORD dw2 = GetLongPathName(szFileName, szLongPath2, cchLongPath2);
  137. if(0==dw2)
  138. return sc.FromLastError().ToHr();
  139. // 4. Compare (case-insensitive) both parts to ensure that they are the same.
  140. // If they are not, some other .exe is trying to instantiate an object. Do
  141. // not allow this.
  142. Trace(tagCicGetClassObject, TEXT("Comparing %s to %s"), szLongPath1, szLongPath2);
  143. if(0 != _tcsicmp(szLongPath1, szLongPath2))
  144. {
  145. // try one more test (in case this is a 64-bit machine) - check for the SysWow64 directory
  146. const int cchMMCSysWow64PathName = MAX_PATH;
  147. TCHAR szMMCSysWow64PathName[cchMMCSysWow64PathName] = {0};
  148. sc = ScGetSystemWow64Directory(szMMCSysWow64PathName, cchMMCSysWow64PathName);
  149. if(sc)
  150. return sc.ToHr();
  151. sc = StringCchCat(szMMCSysWow64PathName, cchMMCSysWow64PathName, szMMC);
  152. if(sc)
  153. return sc.ToHr();
  154. const DWORD cchLongPathSysWow64 = MAX_PATH;
  155. TCHAR szLongPathSysWow64[cchLongPathSysWow64] = {0};
  156. DWORD dw3 = GetLongPathName(szMMCSysWow64PathName, szLongPathSysWow64, cchLongPathSysWow64);
  157. if(0==dw3)
  158. return sc.FromLastError().ToHr();
  159. Trace(tagCicGetClassObject, TEXT("Comparing %s to %s"), szLongPathSysWow64, szLongPath2);
  160. if(0 != _tcsicmp(szLongPath2, szLongPathSysWow64))
  161. {
  162. Trace(tagCicGetClassObject, TEXT("Invalid exe - must be %s or %s. Did not instantiate object."), szMMCPathName, szMMCSysWow64PathName);
  163. return (sc = CLASS_E_CLASSNOTAVAILABLE).ToHr();
  164. }
  165. }
  166. return _Module.GetClassObject(rclsid, riid, ppv);
  167. }
  168. /////////////////////////////////////////////////////////////////////////////
  169. // DllRegisterServer - Adds entries to the system registry
  170. STDAPI DllRegisterServer(void)
  171. {
  172. // registers object, typelib and all interfaces in typelib
  173. return _Module.RegisterServer(TRUE);
  174. }
  175. /////////////////////////////////////////////////////////////////////////////
  176. // DllUnregisterServer - Removes entries from the system registry
  177. STDAPI DllUnregisterServer(void)
  178. {
  179. _Module.UnregisterServer();
  180. return S_OK;
  181. }