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.

401 lines
9.8 KiB

  1. /*
  2. * REGDB.C
  3. *
  4. * Functions to query the registration database
  5. *
  6. * OleStdGetMiscStatusOfClass
  7. * OleStdGetDefaultFileFormatOfClass
  8. * OleStdGetAuxUserType
  9. * OleStdGetUserTypeOfClass
  10. *
  11. * (c) Copyright Microsoft Corp. 1992-1993 All Rights Reserved
  12. *
  13. */
  14. #define STRICT 1
  15. #include "ole2ui.h"
  16. #include "common.h"
  17. #include <ctype.h>
  18. OLEDBGDATA
  19. // Replacement for stdlib atol,
  20. // which didn't work and doesn't take far pointers.
  21. // Must be tolerant of leading spaces.
  22. //
  23. //
  24. static LONG Atol(LPTSTR lpsz)
  25. {
  26. signed int sign = +1;
  27. UINT base = 10;
  28. LONG l = 0;
  29. if (NULL==lpsz)
  30. {
  31. OleDbgAssert (0);
  32. return 0;
  33. }
  34. while (isspace(*lpsz))
  35. lpsz++;
  36. if (*lpsz=='-')
  37. {
  38. lpsz++;
  39. sign = -1;
  40. }
  41. if (lpsz[0]==TEXT('0') && lpsz[1]==TEXT('x'))
  42. {
  43. base = 16;
  44. lpsz+=2;
  45. }
  46. if (base==10)
  47. {
  48. while (isdigit(*lpsz))
  49. {
  50. l = l * base + *lpsz - '0';
  51. lpsz++;
  52. }
  53. }
  54. else
  55. {
  56. OleDbgAssert (base==16);
  57. while (isxdigit(*lpsz))
  58. {
  59. l = l * base + isdigit(*lpsz) ? *lpsz - '0' : toupper(*lpsz) - 'A' + 10;
  60. lpsz++;
  61. }
  62. }
  63. return l * sign;
  64. }
  65. /*
  66. * OleStdGetUserTypeOfClass(REFCLSID, LPSTR, UINT, HKEY)
  67. *
  68. * Purpose:
  69. * Returns the user type (human readable class name) of the specified class.
  70. *
  71. * Parameters:
  72. * rclsid pointer to the clsid to retrieve user type of.
  73. * lpszUserType pointer to buffer to return user type in.
  74. * cch length of buffer pointed to by lpszUserType
  75. * hKey hKey for reg db - if this is NULL, then we
  76. * open and close the reg db within this function. If it
  77. * is non-NULL, then we assume it's a valid key to the
  78. * \ root and use it without closing it. (useful
  79. * if you're doing lots of reg db stuff).
  80. *
  81. * Return Value:
  82. * UINT Number of characters in returned string. 0 on error.
  83. *
  84. */
  85. STDAPI_(UINT) OleStdGetUserTypeOfClass(REFCLSID rclsid, LPTSTR lpszUserType, UINT cch, HKEY hKey)
  86. {
  87. LONG dw;
  88. LONG lRet;
  89. LPSTR lpszCLSID, lpszProgID;
  90. BOOL fFreeProgID = FALSE;
  91. BOOL bCloseRegDB = FALSE;
  92. TCHAR szKey[128];
  93. LPMALLOC lpIMalloc;
  94. if (!lpszUserType)
  95. return 0;
  96. *lpszUserType = TEXT('\0');
  97. if (hKey == NULL)
  98. {
  99. //Open up the root key.
  100. lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
  101. if ((LONG)ERROR_SUCCESS!=lRet)
  102. return (UINT)FALSE;
  103. bCloseRegDB = TRUE;
  104. }
  105. // Get a string containing the class name
  106. StringFromCLSIDA(rclsid, &lpszCLSID);
  107. wsprintf(szKey, TEXT("CLSID\\%s"), lpszCLSID);
  108. dw=cch;
  109. lRet = RegQueryValue(hKey, szKey, lpszUserType, &dw);
  110. if ((LONG)ERROR_SUCCESS!=lRet) {
  111. // Load 'Unknown Source' and 'Unknown Type' strings
  112. dw = (LONG)LoadString(ghInst, IDS_PSUNKNOWNTYPE, lpszUserType, cch);
  113. }
  114. if ( ((LONG)ERROR_SUCCESS!=lRet) && (CoIsOle1Class(rclsid)) )
  115. {
  116. // We've got an OLE 1.0 class, so let's try to get the user type
  117. // name from the ProgID entry.
  118. ProgIDFromCLSIDA(rclsid, &lpszProgID);
  119. fFreeProgID = TRUE;
  120. dw = cch;
  121. lRet = RegQueryValue(hKey, lpszProgID, lpszUserType, &dw);
  122. if ((LONG)ERROR_SUCCESS != lRet)
  123. dw = 0;
  124. }
  125. if (NOERROR == CoGetMalloc(MEMCTX_TASK, &lpIMalloc))
  126. {
  127. if (fFreeProgID)
  128. lpIMalloc->lpVtbl->Free(lpIMalloc, (LPVOID)lpszProgID);
  129. lpIMalloc->lpVtbl->Free(lpIMalloc, (LPVOID)lpszCLSID);
  130. lpIMalloc->lpVtbl->Release(lpIMalloc);
  131. }
  132. if (bCloseRegDB)
  133. RegCloseKey(hKey);
  134. return (UINT)dw;
  135. }
  136. /*
  137. * OleStdGetAuxUserType(RCLSID, WORD, LPSTR, int, HKEY)
  138. *
  139. * Purpose:
  140. * Returns the specified AuxUserType from the reg db.
  141. *
  142. * Parameters:
  143. * rclsid pointer to the clsid to retrieve aux user type of.
  144. * hKey hKey for reg db - if this is NULL, then we
  145. * open and close the reg db within this function. If it
  146. * is non-NULL, then we assume it's a valid key to the
  147. * \ root and use it without closing it. (useful
  148. * if you're doing lots of reg db stuff).
  149. * wAuxUserType which aux user type field to look for. In 4/93 release
  150. * 2 is short name and 3 is exe name.
  151. * lpszUserType pointer to buffer to return user type in.
  152. * cch length of buffer pointed to by lpszUserType
  153. *
  154. * Return Value:
  155. * UINT Number of characters in returned string. 0 on error.
  156. *
  157. */
  158. STDAPI_(UINT) OleStdGetAuxUserType(REFCLSID rclsid,
  159. WORD wAuxUserType,
  160. LPTSTR lpszAuxUserType,
  161. int cch,
  162. HKEY hKey)
  163. {
  164. HKEY hThisKey;
  165. BOOL fCloseRegDB = FALSE;
  166. LONG dw;
  167. LRESULT lRet;
  168. LPTSTR lpszCLSID;
  169. LPMALLOC lpIMalloc;
  170. TCHAR szKey[OLEUI_CCHKEYMAX];
  171. TCHAR szTemp[32];
  172. lpszAuxUserType[0] = TEXT('\0');
  173. if (NULL == hKey)
  174. {
  175. lRet = RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hThisKey);
  176. if (ERROR_SUCCESS != lRet)
  177. return 0;
  178. }
  179. else
  180. hThisKey = hKey;
  181. StringFromCLSIDA(rclsid, &lpszCLSID);
  182. lstrcpy(szKey, TEXT("CLSID\\"));
  183. lstrcat(szKey, lpszCLSID);
  184. wsprintf(szTemp, TEXT("\\AuxUserType\\%d"), wAuxUserType);
  185. lstrcat(szKey, szTemp);
  186. dw = cch;
  187. lRet = RegQueryValue(hThisKey, szKey, lpszAuxUserType, &dw);
  188. if (ERROR_SUCCESS != lRet) {
  189. dw = 0;
  190. lpszAuxUserType[0] = TEXT('\0');
  191. }
  192. if (fCloseRegDB)
  193. RegCloseKey(hThisKey);
  194. if (NOERROR == CoGetMalloc(MEMCTX_TASK, &lpIMalloc))
  195. {
  196. lpIMalloc->lpVtbl->Free(lpIMalloc, (LPVOID)lpszCLSID);
  197. lpIMalloc->lpVtbl->Release(lpIMalloc);
  198. }
  199. return (UINT)dw;
  200. }
  201. /*
  202. * OleStdGetMiscStatusOfClass(REFCLSID, HKEY)
  203. *
  204. * Purpose:
  205. * Returns the value of the misc status for the given clsid.
  206. *
  207. * Parameters:
  208. * rclsid pointer to the clsid to retrieve user type of.
  209. * hKey hKey for reg db - if this is NULL, then we
  210. * open and close the reg db within this function. If it
  211. * is non-NULL, then we assume it's a valid key to the
  212. * \\CLSID root and use it without closing it. (useful
  213. * if you're doing lots of reg db stuff).
  214. *
  215. * Return Value:
  216. * BOOL TRUE on success, FALSE on failure.
  217. *
  218. */
  219. STDAPI_(BOOL) OleStdGetMiscStatusOfClass(REFCLSID rclsid, HKEY hKey, DWORD FAR * lpdwValue)
  220. {
  221. DWORD dw;
  222. LONG lRet;
  223. LPTSTR lpszCLSID;
  224. TCHAR szKey[64];
  225. TCHAR szMiscStatus[OLEUI_CCHKEYMAX];
  226. BOOL bCloseRegDB = FALSE;
  227. if (hKey == NULL)
  228. {
  229. //Open up the root key.
  230. lRet=RegOpenKey(HKEY_CLASSES_ROOT, TEXT("CLSID"), &hKey);
  231. if ((LONG)ERROR_SUCCESS!=lRet)
  232. return FALSE;
  233. bCloseRegDB = TRUE;
  234. }
  235. // Get a string containing the class name
  236. StringFromCLSIDA(rclsid, &lpszCLSID);
  237. // Construct key
  238. lstrcpy(szKey, lpszCLSID);
  239. lstrcat(szKey, TEXT("\\MiscStatus"));
  240. dw=OLEUI_CCHKEYMAX_SIZE;
  241. lRet = RegQueryValue(hKey, szKey, (LPTSTR)szMiscStatus, &dw);
  242. if ((LONG)ERROR_SUCCESS!=lRet)
  243. {
  244. OleStdFreeString(lpszCLSID, NULL);
  245. if (bCloseRegDB)
  246. RegCloseKey(hKey);
  247. return FALSE;
  248. }
  249. *lpdwValue = Atol((LPTSTR)szMiscStatus);
  250. OleStdFreeString(lpszCLSID, NULL);
  251. if (bCloseRegDB)
  252. RegCloseKey(hKey);
  253. return TRUE;
  254. }
  255. /*
  256. * CLIPFORMAT OleStdGetDefaultFileFormatOfClass(REFCLSID, HKEY)
  257. *
  258. * Purpose:
  259. * Returns the default file format of the specified class.
  260. * this is entered in REGDB as follows:
  261. * CLSID\{...}\DataFormats\DefaultFile = <cfFmt>
  262. *
  263. * Parameters:
  264. * rclsid pointer to the clsid to retrieve user type of.
  265. * hKey hKey for reg db- if this is NULL, then we
  266. * open and close the reg db within this function. If it
  267. * is non-NULL, then we assume it's a valid key to the
  268. * \ root and use it without closing it. (useful
  269. * if you're doing lots of reg db stuff).
  270. *
  271. * Return Value:
  272. * cfFmt -- DefaultFile format
  273. * NULL -- failed to get default file format
  274. *
  275. */
  276. STDAPI_(CLIPFORMAT) OleStdGetDefaultFileFormatOfClass(
  277. REFCLSID rclsid,
  278. HKEY hKey
  279. )
  280. {
  281. CLIPFORMAT cfFmt = 0;
  282. DWORD dw;
  283. LONG lRet;
  284. LPTSTR lpszCLSID;
  285. BOOL bCloseRegDB = FALSE;
  286. TCHAR szKey[128];
  287. TCHAR szDefaultFile[OLEUI_CCHKEYMAX];
  288. BOOL bStatus = TRUE;
  289. if (hKey == NULL)
  290. {
  291. //Open up the root key.
  292. lRet=RegOpenKey(HKEY_CLASSES_ROOT, NULL, &hKey);
  293. if ((LONG)ERROR_SUCCESS!=lRet)
  294. return 0;
  295. bCloseRegDB = TRUE;
  296. }
  297. // Get a string containing the class name
  298. StringFromCLSIDA(rclsid, &lpszCLSID);
  299. // Construct key
  300. wsprintf(szKey, TEXT("CLSID\\%s\\DataFormats\\DefaultFile"), lpszCLSID);
  301. OleStdFreeString(lpszCLSID, NULL);
  302. dw=OLEUI_CCHKEYMAX_SIZE;
  303. lRet = RegQueryValue(hKey, szKey, (LPTSTR)szDefaultFile, (LONG FAR *)&dw);
  304. if ((LONG)ERROR_SUCCESS!=lRet)
  305. bStatus = FALSE;
  306. else {
  307. /* if the format is a number, then it should refer to one of the
  308. ** standard Windows formats.
  309. */
  310. if (isdigit(szDefaultFile[0]))
  311. cfFmt = (CLIPFORMAT)Atol(szDefaultFile);
  312. else
  313. cfFmt = RegisterClipboardFormat(szDefaultFile);
  314. }
  315. if (bCloseRegDB)
  316. RegCloseKey(hKey);
  317. return cfFmt;
  318. }
  319.