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.

350 lines
12 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
  4. // ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
  5. // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
  6. // PARTICULAR PURPOSE.
  7. //
  8. // Copyright 1998 - 2001 Microsoft Corporation. All Rights Reserved.
  9. //
  10. // File: langreg.hxx
  11. //
  12. // PURPOSE: Helper classes for sample programs
  13. //
  14. // PLATFORM: Windows 2000 and later
  15. //
  16. //--------------------------------------------------------------------------
  17. #pragma once
  18. //
  19. // Structure to define language resource class
  20. //
  21. struct SLangClassEntry
  22. {
  23. WCHAR const * pwszClassId;
  24. WCHAR const * pwszClassIdDescription;
  25. WCHAR const * pwszDLL;
  26. WCHAR const * pwszThreadingModel;
  27. };
  28. struct SLangRegistry
  29. {
  30. WCHAR const * pwszLangName;
  31. LONG lcid;
  32. SLangClassEntry WordBreaker;
  33. SLangClassEntry Stemmer;
  34. };
  35. //
  36. // Sample use of the structures
  37. //
  38. //
  39. // SLangClassEntry const NeutralWordBreaker =
  40. // { L"{369647e0-17b0-11ce-9950-00aa004bbb1f}",
  41. // L"Neutral Word Breaker",
  42. // L"query.dll",
  43. // L"both" };
  44. //
  45. // SLangRegistry const English_US_LangRes =
  46. // { L"English_US", 1033,
  47. // { L"{59e09780-8099-101b-8df3-00000b65c3b5}",
  48. // L"English_US Word Breaker",
  49. // L"infosoft.dll",
  50. // L"both" },
  51. // { L"{eeed4c20-7f1b-11ce-be57-00aa0051fe20}",
  52. // L"English_US Stemmer",
  53. // L"infosoft.dll",
  54. // L"both" }
  55. // };
  56. //
  57. //
  58. // Function prototypes
  59. //
  60. inline long RegisterALanguageResource( SLangRegistry const & LangRes );
  61. inline long RegisterALanguageClass( SLangClassEntry const & LangClass );
  62. inline long UnRegisterALanguageResource( SLangRegistry const & LangRes );
  63. inline long UnRegisterALanguageClass( SLangClassEntry const & LangClass );
  64. //+---------------------------------------------------------------------------
  65. //
  66. // Function: RegisterALanguageResource, private
  67. //
  68. // Synopsis: Registers a language resource.
  69. //
  70. // Arguments: [LangRes] -- Language resource description
  71. //
  72. // Returns: ERROR_SUCCESS on success
  73. //
  74. // History: 05-Jan-99 AlanW Created
  75. //
  76. //----------------------------------------------------------------------------
  77. inline long RegisterALanguageResource( SLangRegistry const & LangRes )
  78. {
  79. WCHAR wcTemp[MAX_PATH];
  80. long dwError;
  81. HKEY hKey = (HKEY)INVALID_HANDLE_VALUE;
  82. wcscpy( wcTemp, L"System\\CurrentControlSet\\Control\\ContentIndex\\Language\\" );
  83. if ( ( wcslen( wcTemp ) + wcslen( LangRes.pwszLangName ) ) >= MAX_PATH )
  84. return ERROR_INSUFFICIENT_BUFFER;
  85. wcscat( wcTemp, LangRes.pwszLangName );
  86. do
  87. {
  88. DWORD dwDisposition;
  89. dwError = RegCreateKeyExW( HKEY_LOCAL_MACHINE, // Root
  90. wcTemp, // Sub key
  91. 0, // Reserved
  92. 0, // Class
  93. 0, // Flags
  94. KEY_ALL_ACCESS, // Access
  95. 0, // Security
  96. &hKey, // Handle
  97. &dwDisposition ); // Disposition
  98. if ( ERROR_SUCCESS != dwError )
  99. break;
  100. //
  101. // Write the locale ID
  102. //
  103. dwError = RegSetValueExW( hKey, // Key
  104. L"Locale", // Name
  105. 0, // Reserved
  106. REG_DWORD, // Type
  107. (BYTE *)&LangRes.lcid, // Value
  108. sizeof DWORD );
  109. if ( ERROR_SUCCESS != dwError )
  110. break;
  111. //
  112. // Create the word breaker class description
  113. //
  114. if (LangRes.WordBreaker.pwszClassId != 0)
  115. {
  116. dwError = RegisterALanguageClass( LangRes.WordBreaker );
  117. if ( ERROR_SUCCESS != dwError )
  118. break;
  119. dwError = RegSetValueExW( hKey, // Key
  120. L"WBreakerClass", // Name
  121. 0, // Reserved
  122. REG_SZ, // Type
  123. (BYTE *)LangRes.WordBreaker.pwszClassId, // Value
  124. (1 + wcslen(LangRes.WordBreaker.pwszClassId) ) * sizeof(WCHAR) );
  125. if ( ERROR_SUCCESS != dwError )
  126. break;
  127. }
  128. //
  129. // Create the stemmer class description
  130. //
  131. if (LangRes.Stemmer.pwszClassId != 0)
  132. {
  133. dwError = RegisterALanguageClass( LangRes.Stemmer );
  134. if ( ERROR_SUCCESS != dwError )
  135. break;
  136. dwError = RegSetValueExW( hKey, // Key
  137. L"StemmerClass", // Name
  138. 0, // Reserved
  139. REG_SZ, // Type
  140. (BYTE *)LangRes.Stemmer.pwszClassId, // Value
  141. (1 + wcslen(LangRes.Stemmer.pwszClassId) ) * sizeof(WCHAR) );
  142. if ( ERROR_SUCCESS != dwError )
  143. break;
  144. }
  145. } while( FALSE );
  146. if ( (HKEY)INVALID_HANDLE_VALUE != hKey )
  147. {
  148. RegCloseKey( hKey );
  149. hKey = (HKEY)INVALID_HANDLE_VALUE;
  150. }
  151. return dwError;
  152. }
  153. //+---------------------------------------------------------------------------
  154. //
  155. // Function: UnRegisterALanguageResource, private
  156. //
  157. // Synopsis: Unregisters a language resource.
  158. //
  159. // Arguments: [LangRes] -- Language resource description
  160. //
  161. // Returns: ERROR_SUCCESS on success
  162. //
  163. // History: 05-Jan-99 AlanW Created
  164. //
  165. //----------------------------------------------------------------------------
  166. inline long UnRegisterALanguageResource( SLangRegistry const & LangRes )
  167. {
  168. WCHAR wcTemp[MAX_PATH];
  169. wcscpy( wcTemp, L"System\\CurrentControlSet\\Control\\ContentIndex\\Language\\" );
  170. if ( ( wcslen( wcTemp ) + wcslen( LangRes.pwszLangName ) ) >= MAX_PATH )
  171. return ERROR_INSUFFICIENT_BUFFER;
  172. wcscat( wcTemp, LangRes.pwszLangName );
  173. HKEY hKey = (HKEY)INVALID_HANDLE_VALUE;
  174. long dwError = RegOpenKeyExW( HKEY_LOCAL_MACHINE, // Root
  175. wcTemp, // Sub key
  176. 0, // Reserved
  177. KEY_ALL_ACCESS, // Access
  178. &hKey ); // Handle
  179. //
  180. // Delete the word breaker class description
  181. //
  182. if (LangRes.WordBreaker.pwszClassId != 0)
  183. {
  184. dwError = UnRegisterALanguageClass( LangRes.WordBreaker );
  185. if (hKey != INVALID_HANDLE_VALUE)
  186. dwError = RegDeleteValueW( hKey, // Key
  187. L"WBreakerClass" ); // Name
  188. }
  189. //
  190. // Create the stemmer class description
  191. //
  192. if (LangRes.Stemmer.pwszClassId != 0)
  193. {
  194. dwError = UnRegisterALanguageClass( LangRes.Stemmer );
  195. if (hKey != INVALID_HANDLE_VALUE)
  196. dwError = RegDeleteValueW( hKey, // Key
  197. L"StemmerClass" ); // Name
  198. }
  199. if (hKey != INVALID_HANDLE_VALUE)
  200. {
  201. DWORD dwNumofKeys = 0;
  202. DWORD dwNumofValues = 0;
  203. dwError = RegQueryInfoKeyW( hKey, // Hkey
  204. 0, // Buffer for class string
  205. 0, // Size of class string buffer
  206. 0, // reserved
  207. &dwNumofKeys,// number of subkeys
  208. 0, // longest subkey name length
  209. 0, // longest class string length
  210. &dwNumofValues,// number of value entries
  211. 0, // longest value name length
  212. 0, // longest value data length
  213. 0, // security descriptor length
  214. 0 ); // last write time);
  215. if ( ERROR_SUCCESS == dwError )
  216. {
  217. if ( (dwNumofValues == 1) && (dwNumofKeys==0) )
  218. {
  219. //
  220. // There is only one value and no sub-keys under this key,
  221. // Delete the Locale value and then the sub-key for this Lang
  222. // if that succeeded.
  223. //
  224. RegDeleteValueW( hKey, // Key
  225. L"Locale" ); // Name
  226. dwError = RegQueryInfoKeyW( hKey, // Hkey
  227. 0, // class string
  228. 0, // Size of class string
  229. 0, // reserved
  230. &dwNumofKeys,
  231. 0, // max subkey name len
  232. 0, // max class string len
  233. &dwNumofValues,
  234. 0, // max value name len
  235. 0, // max value data len
  236. 0, // security desc len
  237. 0 ); // last write time);
  238. }
  239. }
  240. RegCloseKey( hKey );
  241. if ( ERROR_SUCCESS == dwError &&
  242. (0 == dwNumofValues) &&
  243. (0 == dwNumofKeys) )
  244. dwError = RegDeleteKeyW( HKEY_LOCAL_MACHINE, // Root
  245. wcTemp ); // Sub key
  246. }
  247. return dwError;
  248. }
  249. //+---------------------------------------------------------------------------
  250. //
  251. // Function: RegisterALanguageClass, private
  252. //
  253. // Synopsis: Registers a language resource classID in registry
  254. //
  255. // Arguments: [LangClass] -- IWordBreaker or IStemmer description
  256. //
  257. // Returns: ERROR_SUCCESS on success
  258. //
  259. // History: 05-Jan-99 AlanW Created
  260. //
  261. //----------------------------------------------------------------------------
  262. inline long RegisterALanguageClass( SLangClassEntry const & LangClass )
  263. {
  264. WCHAR const * aKeyValues[4] = { LangClass.pwszClassId,
  265. LangClass.pwszClassIdDescription,
  266. L"InprocServer32",
  267. LangClass.pwszDLL };
  268. long retVal = BuildKeyValues( aKeyValues, sizeof(aKeyValues)/sizeof(aKeyValues[0]) );
  269. if ( ERROR_SUCCESS == retVal )
  270. retVal = AddThreadingModel( LangClass.pwszClassId, LangClass.pwszThreadingModel );
  271. return retVal;
  272. }
  273. //+---------------------------------------------------------------------------
  274. //
  275. // Function: UnRegisterALanguageClass, private
  276. //
  277. // Synopsis: Unregisters a language resource classID
  278. //
  279. // Arguments: [LangClass] -- IWordBreaker or IStemmer description
  280. //
  281. // Returns: ERROR_SUCCESS on success
  282. //
  283. // History: 05-Jan-99 AlanW Created
  284. //
  285. //----------------------------------------------------------------------------
  286. inline long UnRegisterALanguageClass( SLangClassEntry const & LangClass )
  287. {
  288. WCHAR const * aKeyValues[4] = { LangClass.pwszClassId,
  289. LangClass.pwszClassIdDescription,
  290. L"InprocServer32",
  291. LangClass.pwszDLL };
  292. return DestroyKeyValues( aKeyValues, sizeof(aKeyValues)/sizeof(aKeyValues[0]) );
  293. }