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.

459 lines
12 KiB

  1. //==========================================================================;
  2. //
  3. // reghelp : registration helpers that share .rgs scripts for various kinds of classes
  4. // Copyright (c) Microsoft Corporation 1999.
  5. //
  6. /////////////////////////////////////////////////////////////////////////////
  7. #pragma once
  8. #ifndef OBJREGHELP_H_
  9. #define OBJREGHELP_H_
  10. #ifndef _ATL_STATIC_REGISTRY
  11. #error these registration helpers only work when _ATL_STATIC_REGISTRY is defined
  12. #endif
  13. #include <atltmp.h>
  14. // much code copied from atl's statreg.h
  15. // atl middle layers filter out the registry return code and turns everything
  16. // into DISP_E_EXCEPTION. by explicitly allocating a CRegObject
  17. // and calling into it instead of through com into atl.dll we can
  18. // trace in and figure out what's wrong with our script when we goof.
  19. #ifndef MAX_GUID_LEN
  20. #define MAX_GUID_LEN (39 * sizeof(OLECHAR))
  21. #endif
  22. __declspec(selectany) LPCOLESTR pszAutoReg = {
  23. L"HKCR { "
  24. L"%COMPONENT%.%PROGID%.1 = s '%DESCRIPTION%' { "
  25. L"CLSID = s '%CLSID%' "
  26. L"} "
  27. L"%COMPONENT%.%PROGID% = s '%DESCRIPTION%' { "
  28. L"CLSID = s '%CLSID%' "
  29. L"CurVer = s '%COMPONENT%.%PROGID%.1' "
  30. L"} "
  31. L"NoRemove CLSID { "
  32. L"ForceRemove %CLSID% = s '%DESCRIPTION%' { "
  33. L"ProgID = s '%COMPONENT%.%PROGID%.1' "
  34. L"VersionIndependentProgID = s '%COMPONENT%.%PROGID%' "
  35. L"ForceRemove 'Programmable' "
  36. L"InprocServer32 = s '%MODULE%' { "
  37. L"val ThreadingModel = s '%THREAD%' "
  38. L"} "
  39. L"'TypeLib' = s '%TYPELIB%' "
  40. L"} "
  41. L"} "
  42. L"} "
  43. };
  44. __declspec(selectany) LPCOLESTR pszNonAutoReg = {
  45. L"HKCR { "
  46. L"NoRemove CLSID { "
  47. L"ForceRemove %CLSID% = s '%DESCRIPTION%' { "
  48. L"InprocServer32 = s '%MODULE%' { "
  49. L"val ThreadingModel = s '%THREAD%' "
  50. L"} "
  51. L"'TypeLib' = s '%TYPELIB%' "
  52. L"} "
  53. L"} "
  54. L"} "
  55. };
  56. __declspec(selectany) LPCOLESTR pszFullControl = {
  57. L"HKCR { "
  58. L"NoRemove CLSID { "
  59. L"%CLSID% { "
  60. L"ForceRemove 'Control' "
  61. L"ForceRemove 'Insertable' "
  62. L"ForceRemove 'ToolboxBitmap32' = s '%MODULE%, 101' "
  63. L"'MiscStatus' = s '0' { "
  64. L"'1' = s '%OLEMISC%' "
  65. L"} "
  66. L"'Version' = s '%VERSION%' "
  67. L"} "
  68. L"} "
  69. L"} "
  70. };
  71. __declspec(selectany) LPCOLESTR pszProtocol = {
  72. L"HKCR { "
  73. L"NoRemove PROTOCOLS { "
  74. L"NoRemove Handler { "
  75. L"ForceRemove %PROTOCOL% { "
  76. L"val '' = s '%DESCRIPTION%' "
  77. L"val 'CLSID' = s '%CLSID%' "
  78. L"} "
  79. L"} "
  80. L"} "
  81. L"} "
  82. L"HKCU { "
  83. L"NoRemove Software { "
  84. L"NoRemove Microsoft { "
  85. L"NoRemove Windows { "
  86. L"NoRemove CurrentVersion { "
  87. L"NoRemove Internet Settings { "
  88. L"NoRemove ZoneMap { "
  89. L"NoRemove ProtocolDefaults { "
  90. L"val '%PROTOCOL%' = d 3 "
  91. L"} "
  92. L"} "
  93. L"} "
  94. L"} "
  95. L"} "
  96. L"} "
  97. L"} "
  98. L"} "
  99. };
  100. enum MacroNameList {
  101. mnlModule,
  102. mnlComponent,
  103. mnlProgID,
  104. mnlCLSID,
  105. mnlDesc,
  106. mnlTypeLib,
  107. mnlVersion,
  108. mnlProtocol,
  109. mnlOleMisc,
  110. mnlThread,
  111. };
  112. __declspec(selectany) LPCOLESTR pszMacroNames[] = {
  113. L"MODULE",
  114. L"COMPONENT",
  115. L"PROGID",
  116. L"CLSID",
  117. L"DESCRIPTION",
  118. L"TYPELIB",
  119. L"VERSION",
  120. L"PROTOCOL",
  121. L"OLEMISC",
  122. L"THREAD",
  123. };
  124. enum ThreadVal {
  125. tvApartment,
  126. tvFree,
  127. tvBoth,
  128. };
  129. __declspec(selectany) LPCOLESTR pszThreadValNames[] = {
  130. L"Apartment",
  131. L"Free",
  132. L"Both",
  133. };
  134. using ::ATL::ATL::CRegObject;
  135. class CObjRegHelp {
  136. public:
  137. ///////////
  138. static HRESULT InsertModuleName(CRegObject& ro) {
  139. /////////// from AtlLModuleupdate
  140. TCHAR szModule[_MAX_PATH];
  141. GetModuleFileName(_Module.m_hInst, szModule, _MAX_PATH);
  142. LPOLESTR pszModule;
  143. USES_CONVERSION;
  144. if ((_Module.m_hInst == NULL) || (_Module.m_hInst == GetModuleHandle(NULL))) { // register as EXE
  145. // Convert to short path to work around bug in NT4's CreateProcess
  146. TCHAR szModuleShort[_MAX_PATH];
  147. int cbShortName = GetShortPathName(szModule, szModuleShort, _MAX_PATH);
  148. if (cbShortName == _MAX_PATH)
  149. return E_OUTOFMEMORY;
  150. pszModule = (cbShortName == 0 || cbShortName == ERROR_INVALID_PARAMETER) ? T2OLE(szModule) : T2OLE(szModuleShort);
  151. } else {
  152. pszModule = T2OLE(szModule);
  153. }
  154. int nLen = ocslen(pszModule);
  155. LPOLESTR pszModuleQuote = new OLECHAR[nLen*2+1];
  156. if (!pszModuleQuote) {
  157. return E_OUTOFMEMORY;
  158. }
  159. CComModule::ReplaceSingleQuote(pszModuleQuote, pszModule);
  160. HRESULT hr = ro.AddReplacement(pszMacroNames[mnlModule], pszModuleQuote);
  161. delete[] pszModuleQuote;
  162. return hr;
  163. }
  164. ///////////
  165. static HRESULT InsertGUID(CRegObject &ro, LPCOLESTR pszMacro, REFCLSID guid) {
  166. OLECHAR szGUID[MAX_GUID_LEN];
  167. int rc = StringFromGUID2(guid, szGUID, MAX_GUID_LEN);
  168. if (!rc) {
  169. return E_UNEXPECTED;
  170. }
  171. return ro.AddReplacement(pszMacro, szGUID);
  172. }
  173. ///////////
  174. static HRESULT RegisterAutomationClass(bool bRegister,
  175. CRegObject& ro,
  176. const UINT nidComponent,
  177. const UINT nidProgID,
  178. const UINT nidDesc,
  179. REFCLSID clsCLSID,
  180. REFCLSID clsTypeLib,
  181. ThreadVal tval = tvApartment) {
  182. USES_CONVERSION;
  183. // module
  184. HRESULT hr = InsertModuleName(ro);
  185. if (FAILED(hr)) {
  186. return hr;
  187. }
  188. // clsid
  189. hr = InsertGUID(ro, pszMacroNames[mnlCLSID], clsCLSID);
  190. if (FAILED(hr)) {
  191. return hr;
  192. }
  193. // typelib
  194. hr = InsertGUID(ro, pszMacroNames[mnlTypeLib], clsTypeLib);
  195. if (FAILED(hr)) {
  196. return hr;
  197. }
  198. // threading model
  199. hr = ro.AddReplacement(pszMacroNames[mnlThread], pszThreadValNames[tval]);
  200. if (FAILED(hr)) {
  201. return hr;
  202. }
  203. // component
  204. CString cs;
  205. if (!cs.LoadString(nidComponent)) {
  206. return E_INVALIDARG;
  207. }
  208. hr = ro.AddReplacement(pszMacroNames[mnlComponent], T2COLE(cs));
  209. if (FAILED(hr)) {
  210. return hr;
  211. }
  212. // progid
  213. if (!cs.LoadString(nidProgID)) {
  214. return E_INVALIDARG;
  215. }
  216. hr = ro.AddReplacement(pszMacroNames[mnlProgID], T2COLE(cs));
  217. if (FAILED(hr)) {
  218. return hr;
  219. }
  220. // desc
  221. if (!cs.LoadString(nidDesc)) {
  222. return E_INVALIDARG;
  223. }
  224. hr = ro.AddReplacement(pszMacroNames[mnlDesc], T2COLE(cs));
  225. if (FAILED(hr)) {
  226. return hr;
  227. }
  228. if (bRegister) {
  229. return ro.StringRegister(pszAutoReg);
  230. } else {
  231. return ro.StringUnregister(pszAutoReg);
  232. }
  233. }
  234. ///////////
  235. static HRESULT RegisterNonAutomationClass(bool bRegister,
  236. CRegObject& ro,
  237. const UINT nidDesc,
  238. REFCLSID clsCLSID,
  239. REFCLSID clsTypeLib,
  240. ThreadVal tval = tvApartment) {
  241. // module
  242. HRESULT hr = InsertModuleName(ro);
  243. if (FAILED(hr)) {
  244. return hr;
  245. }
  246. // clsid
  247. hr = InsertGUID(ro, pszMacroNames[mnlCLSID], clsCLSID);
  248. if (FAILED(hr)) {
  249. return hr;
  250. }
  251. // typelib
  252. hr = InsertGUID(ro, pszMacroNames[mnlTypeLib], clsTypeLib);
  253. if (FAILED(hr)) {
  254. return hr;
  255. }
  256. // threading model
  257. hr = ro.AddReplacement(pszMacroNames[mnlThread], pszThreadValNames[tval]);
  258. if (FAILED(hr)) {
  259. return hr;
  260. }
  261. CString cs;
  262. USES_CONVERSION;
  263. // desc
  264. if (!cs.LoadString(nidDesc)) {
  265. return E_INVALIDARG;
  266. }
  267. hr = ro.AddReplacement(pszMacroNames[mnlDesc], T2COLE(cs));
  268. if (FAILED(hr)) {
  269. return hr;
  270. }
  271. if (bRegister) {
  272. return ro.StringRegister(pszNonAutoReg);
  273. } else {
  274. return ro.StringUnregister(pszAutoReg);
  275. }
  276. }
  277. ///////////
  278. static HRESULT RegisterFullControl(bool bRegister,
  279. CRegObject& ro,
  280. const int iMajor,
  281. const int iMinor,
  282. const DWORD dwOleMiscStatusBits) {
  283. CString cs;
  284. USES_CONVERSION;
  285. // version
  286. cs.Format(_T("%d.%d"), iMajor, iMinor);
  287. HRESULT hr = ro.AddReplacement(pszMacroNames[mnlVersion], T2COLE(cs));
  288. if (FAILED(hr)) {
  289. return hr;
  290. }
  291. cs.Format(_T("%ld"), dwOleMiscStatusBits);
  292. hr = ro.AddReplacement(pszMacroNames[mnlOleMisc], T2COLE(cs));
  293. if (FAILED(hr)) {
  294. return hr;
  295. }
  296. if (bRegister) {
  297. return ro.StringRegister(pszFullControl);
  298. } else {
  299. return ro.StringUnregister(pszFullControl);
  300. }
  301. }
  302. ///////////
  303. static HRESULT RegisterProtocol(bool bRegister,
  304. CRegObject& ro,
  305. LPCOLESTR szProtocol) {
  306. HRESULT hr = ro.AddReplacement(pszMacroNames[mnlProtocol], szProtocol);
  307. if (FAILED(hr)) {
  308. return hr;
  309. }
  310. if (bRegister) {
  311. return ro.StringRegister(pszProtocol);
  312. } else {
  313. return ro.StringUnregister(pszProtocol);
  314. }
  315. }
  316. ///////////
  317. static HRESULT RegisterExtraScript(CRegObject &ro, bool bRegister,
  318. const UINT nidExtraScriptID) {
  319. TCHAR szModule[_MAX_PATH];
  320. USES_CONVERSION;
  321. GetModuleFileName(_Module.m_hInst, szModule, _MAX_PATH);
  322. if (bRegister) {
  323. return ro.ResourceRegister(T2OLE(szModule), nidExtraScriptID, OLESTR("REGISTRY"));
  324. } else {
  325. return ro.ResourceUnregister(T2OLE(szModule), nidExtraScriptID, OLESTR("REGISTRY"));
  326. }
  327. }
  328. };
  329. ///////////
  330. #define REGISTER_AUTOMATION_OBJECT(nidComponent, nidProgID, nidDESC, clsTypeLib, clsCLSID) \
  331. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  332. CRegObject ro; \
  333. return CObjRegHelp::RegisterAutomationClass(bRegister ? true : false, ro, nidComponent, \
  334. nidProgID, nidDESC, clsCLSID, clsTypeLib); \
  335. }
  336. #define REGISTER_AUTOMATION_OBJECT_WITH_TM(nidComponent, nidProgID, nidDESC, clsTypeLib, clsCLSID, tvVal) \
  337. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  338. CRegObject ro; \
  339. return CObjRegHelp::RegisterAutomationClass(bRegister ? true : false, ro, nidComponent, \
  340. nidProgID, nidDESC, clsCLSID, clsTypeLib, tvVal); \
  341. }
  342. ///////////
  343. #define REGISTER_AUTOMATION_OBJECT_AND_RGS(nidExtraScript, nidComponent, nidProgID, nidDESC, clsTypeLib, clsCLSID) \
  344. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  345. CRegObject ro; \
  346. HRESULT hr = CObjRegHelp::RegisterAutomationClass(bRegister ? true : false, ro, nidComponent, \
  347. nidProgID, nidDESC, clsCLSID, \
  348. clsTypeLib); \
  349. if (FAILED(hr)) { \
  350. return hr; \
  351. } \
  352. return CObjRegHelp::RegisterExtraScript(ro, nidExtraScript); \
  353. }
  354. ///////////
  355. #define REGISTER_NONAUTOMATION_OBJECT(nidComponent, nidDESC, clsTypeLib, clsCLSID) \
  356. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  357. CRegObject ro; \
  358. return CObjRegHelp::RegisterNonAutomationClass(bRegister ? true : false, ro, nidDESC, clsCLSID, clsTypeLib); \
  359. }
  360. #define REGISTER_NONAUTOMATION_OBJECT_WITH_TM(nidComponent, nidDESC, clsTypeLib, clsCLSID, tvVal) \
  361. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  362. CRegObject ro; \
  363. return CObjRegHelp::RegisterNonAutomationClass(bRegister ? true : false, ro, nidDESC, clsCLSID, clsTypeLib, tvVal); \
  364. }
  365. ///////////
  366. #define REGISTER_NONAUTOMATION_OBJECT_AND_RGS(nidExtraScript, nidDESC, clsTypeLib, clsCLSID) \
  367. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  368. CRegObject ro; \
  369. HRESULT hr = CObjRegHelp::RegisterNonAutomationClass(bRegister ? true : false, ro, \
  370. nidDESC, clsCLSID, \
  371. clsTypeLib); \
  372. if (FAILED(hr)) { \
  373. return hr; \
  374. } \
  375. return CObjRegHelp::RegisterExtraScript(ro, nidExtraScript); \
  376. }
  377. ///////////
  378. #define REGISTER_FULL_CONTROL(nidComponent, nidProgID, nidDESC, clsTypeLib, clsCLSID, wMajor, wMinor, OleMisc) \
  379. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  380. CRegObject ro; \
  381. HRESULT hr = CObjRegHelp::RegisterAutomationClass(bRegister ? true : false, ro, nidComponent, \
  382. nidProgID, nidDESC, clsCLSID, \
  383. clsTypeLib); \
  384. if (FAILED(hr)) { \
  385. return hr; \
  386. } \
  387. return CObjRegHelp::RegisterFullControl(bRegister ? true : false, ro, wMajor, wMinor, OleMisc); \
  388. }
  389. ///////////
  390. #define REGISTER_PROTOCOL(nidComponent, nidDESC, clsTypeLib, clsCLSID, szProtocol) \
  391. static HRESULT WINAPI UpdateRegistry(BOOL bRegister) { \
  392. CRegObject ro; \
  393. HRESULT hr = CObjRegHelp::RegisterNonAutomationClass(bRegister ? true : false, \
  394. ro, \
  395. nidDESC, \
  396. clsCLSID, \
  397. clsTypeLib); \
  398. if (FAILED(hr)) { \
  399. return hr; \
  400. } \
  401. return CObjRegHelp::RegisterProtocol(bRegister ? true : false, ro, szProtocol); \
  402. }
  403. #endif
  404. //end of file objreghelp.h