Source code of Windows XP (NT5)
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.

215 lines
8.6 KiB

  1. /*--------------------------------------------------------------------------*
  2. *
  3. * Microsoft Windows
  4. * Copyright (C) Microsoft Corporation, 1992 - 00
  5. *
  6. * File: classreg.cpp
  7. *
  8. * Contents: Class registration code
  9. *
  10. * History: 3-Feb-2000 jeffro Created
  11. *
  12. *--------------------------------------------------------------------------*/
  13. #include "stdafx.h"
  14. #ifdef DBG
  15. CTraceTag tagDllRegistration (_T("MMC Dll Registration"), _T("MMC Dll Registration"));
  16. #endif //DBG
  17. /*+-------------------------------------------------------------------------*
  18. * szObjScript
  19. *
  20. * Standard registration script template for all objects. '%' characters
  21. * that we want to end up in the final script must be doubled (i.e. "%%")
  22. * because this string is used as a format string for sprintf. sprintf
  23. * will convert "%%" to "%" during formatting.
  24. *--------------------------------------------------------------------------*/
  25. static const WCHAR szObjScript[] =
  26. L"HKCR" L"\n"
  27. L"{" L"\n"
  28. L" %%VProgID%% = s '%%VClassName%%'" L"\n"
  29. L" {" L"\n"
  30. L" CLSID = s '%%VCLSID%%'" L"\n"
  31. L" }" L"\n"
  32. L" %%VVersionIndependentProgID%% = s '%%VClassName%%'" L"\n"
  33. L" {" L"\n"
  34. L" CLSID = s '%%VCLSID%%'" L"\n"
  35. L" CurVer = s '%%VProgID%%'" L"\n"
  36. L" }" L"\n"
  37. L" NoRemove CLSID" L"\n"
  38. L" {" L"\n"
  39. L" ForceRemove %%VCLSID%% = s '%%VClassName%%'" L"\n"
  40. L" {" L"\n"
  41. L" ProgID = s '%%VProgID%%'" L"\n"
  42. L" VersionIndependentProgID = s '%%VVersionIndependentProgID%%'" L"\n"
  43. L" %%ServerType%% = s '%%VFileName%%'" L"\n"
  44. L" {" L"\n"
  45. L" val ThreadingModel = s 'Apartment'" L"\n"
  46. L" }" L"\n"
  47. L" %s" /* szCtlScript substituted here if necessary */ L"\n"
  48. L" }" L"\n"
  49. L" }" L"\n"
  50. L"}";
  51. /*+-------------------------------------------------------------------------*
  52. * szCtlScript
  53. *
  54. * Additional registration script elements for controls. Note that '%'
  55. * characters we want to end up in the final script DO NOT need to be
  56. * doubled, because this string is used as a sprintf replacement parameter
  57. * (which are substituted as-is) and not the format string (where "%%"'s
  58. * are converted to "%").
  59. *--------------------------------------------------------------------------*/
  60. static const WCHAR szCtlScript[] =
  61. L" ForceRemove 'Programmable'" L"\n"
  62. L" ForceRemove 'Control'" L"\n"
  63. L" ForceRemove 'ToolboxBitmap32' = s '%VFileName%, %VBitmapID%'" L"\n"
  64. L" 'MiscStatus' = s '0'" L"\n"
  65. L" {" L"\n"
  66. L" '1' = s '131473'" L"\n"
  67. L" }" L"\n"
  68. L" 'TypeLib' = s '%VLIBID%'" L"\n"
  69. L" 'Version' = s '%VVersion%'";
  70. /*+-------------------------------------------------------------------------*
  71. * MMCUpdateRegistry
  72. *
  73. * Registers a COM object or control. This function typically isn't used
  74. * directly, but indirectly via DECLARE_MMC_OBJECT_REGISTRATION or
  75. * DECLARE_MMC_CONTROL_REGISTRATION.
  76. *
  77. * This function uses a class (ATL::CRegObject) that ATL only documents
  78. * indirectly. Search MSDN for "StringRegister" to find sketchy details.
  79. *--------------------------------------------------------------------------*/
  80. HRESULT WINAPI MMCUpdateRegistry (
  81. BOOL bRegister, // I:register or unregister?
  82. const CObjectRegParams* pObjParams, // I:object registration parameters
  83. const CControlRegParams* pCtlParams) // I:control registration parameters (optional)
  84. {
  85. DECLARE_SC(sc, TEXT("MMCUpdateRegistry"));
  86. /*
  87. * validate required inputs
  88. */
  89. sc = ScCheckPointers (pObjParams, E_FAIL);
  90. if(sc)
  91. return sc.ToHr();
  92. /*
  93. * string-ify the CLSID
  94. */
  95. CCoTaskMemPtr<WCHAR> spszClsid;
  96. sc = StringFromCLSID (pObjParams->m_clsid, &spszClsid);
  97. if (sc)
  98. return sc.ToHr();
  99. /*
  100. * specify the standard object substitution parameters for CRegObject
  101. */
  102. ::ATL::ATL::CRegObject ro; // hack around nested namespace bug in ATL30
  103. _ATL_REGMAP_ENTRY rgObjEntries[] =
  104. {
  105. { L"VCLSID", spszClsid },
  106. { L"VFileName", pObjParams->m_strModuleName.data() },
  107. { L"VClassName", pObjParams->m_strClassName.data() },
  108. { L"VProgID", pObjParams->m_strProgID.data() },
  109. { L"VVersionIndependentProgID", pObjParams->m_strVersionIndependentProgID.data() },
  110. { L"ServerType", pObjParams->m_strServerType.data() },
  111. };
  112. #ifdef DBG
  113. std::wstring strReplacements;
  114. #endif
  115. for (int i = 0; i < countof (rgObjEntries); i++)
  116. {
  117. sc = ro.AddReplacement (rgObjEntries[i].szKey, rgObjEntries[i].szData);
  118. if (sc)
  119. return (sc.ToHr());
  120. AddReplacementTrace (strReplacements,
  121. rgObjEntries[i].szKey,
  122. rgObjEntries[i].szData);
  123. }
  124. /*
  125. * if we're registering a control, add its substitution parameters for CRegObject
  126. */
  127. if (pCtlParams != NULL)
  128. {
  129. /*
  130. * string-ify the LIBID
  131. */
  132. CCoTaskMemPtr<WCHAR> spszLibid;
  133. sc = StringFromCLSID (pCtlParams->m_libid, &spszLibid);
  134. if (sc)
  135. return (sc.ToHr());
  136. _ATL_REGMAP_ENTRY rgCtlEntries[] =
  137. {
  138. { L"VLIBID", spszLibid },
  139. { L"VBitmapID", pCtlParams->m_strToolboxBitmapID.data() },
  140. { L"VVersion", pCtlParams->m_strVersion.data() },
  141. };
  142. for (int i = 0; i < countof (rgCtlEntries); i++)
  143. {
  144. sc = ro.AddReplacement (rgCtlEntries[i].szKey, rgCtlEntries[i].szData);
  145. if (sc)
  146. return (sc.ToHr());
  147. AddReplacementTrace (strReplacements,
  148. rgCtlEntries[i].szKey,
  149. rgCtlEntries[i].szData);
  150. }
  151. }
  152. /*
  153. * format the registration script
  154. */
  155. WCHAR szRegScript[countof(szObjScript) + countof(szCtlScript)];
  156. swprintf (szRegScript, szObjScript, (pCtlParams != NULL) ? szCtlScript : L"");
  157. USES_CONVERSION;
  158. Trace (tagDllRegistration, _T("Registration script:\n%s"), W2T(szRegScript));
  159. Trace (tagDllRegistration, W2CT(strReplacements.data()));
  160. /*
  161. * (un)register!
  162. */
  163. sc = (bRegister) ? ro.StringRegister (szRegScript)
  164. : ro.StringUnregister (szRegScript);
  165. if (sc)
  166. return sc.ToHr();
  167. // change the module path to the absolute one, if we know it
  168. if ( bRegister && pObjParams->m_strModulePath.length() != 0 )
  169. {
  170. // format class ID key.
  171. tstring strKey = tstring(_T("CLSID\\")) + W2CT( spszClsid );
  172. strKey += tstring(_T("\\")) + W2CT( pObjParams->m_strServerType.c_str() );
  173. // see what type of value we need to put
  174. DWORD dwValueType = CModulePath::PlatformSupports_REG_EXPAND_SZ_Values() ?
  175. REG_EXPAND_SZ : REG_SZ;
  176. CRegKey keyServer;
  177. LONG lRet = keyServer.Open(HKEY_CLASSES_ROOT, strKey.c_str() , KEY_WRITE);
  178. if (lRet == ERROR_SUCCESS)
  179. {
  180. RegSetValueEx( keyServer, NULL, 0, dwValueType,
  181. (CONST BYTE *)( W2CT( pObjParams->m_strModulePath.c_str() ) ),
  182. (pObjParams->m_strModulePath.length() + 1) * sizeof(TCHAR) );
  183. }
  184. }
  185. return sc.ToHr();
  186. }