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.

328 lines
6.9 KiB

  1. #include <objbase.h>
  2. #include "SyncHndl.h"
  3. #include "reg.h"
  4. // file contains registration and helper reg routines for handlers
  5. // export for how Rundll32 calls us
  6. EXTERN_C void WINAPI RunDllRegister(HWND hwnd,
  7. HINSTANCE hAppInstance,
  8. LPSTR pszCmdLine,
  9. int nCmdShow)
  10. {
  11. DllRegisterServer();
  12. }
  13. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  14. Function: SetRegKeyValue
  15. Summary: Internal utility function to set a Key, Subkey, and value
  16. in the system Registry under HKEY_CLASSES_ROOT.
  17. Args: LPTSTR pszKey,
  18. LPTSTR pszSubkey,
  19. LPTSTR pszValue)
  20. Returns: BOOL
  21. TRUE if success; FALSE if not.
  22. ------------------------------------------------------------------------F-F*/
  23. BOOL SetRegKeyValue(
  24. HKEY hKeyTop,
  25. LPTSTR pszKey,
  26. LPTSTR pszSubkey,
  27. LPTSTR pszValue)
  28. {
  29. BOOL bOk = FALSE;
  30. LONG ec;
  31. HKEY hKey;
  32. TCHAR szKey[MAX_STRING_LENGTH];
  33. lstrcpy(szKey, pszKey);
  34. if (NULL != pszSubkey)
  35. {
  36. lstrcat(szKey, TEXT("\\"));
  37. lstrcat(szKey, pszSubkey);
  38. }
  39. ec = RegCreateKeyEx(
  40. hKeyTop,
  41. szKey,
  42. 0,
  43. NULL,
  44. REG_OPTION_NON_VOLATILE,
  45. KEY_ALL_ACCESS,
  46. NULL,
  47. &hKey,
  48. NULL);
  49. if (NULL != pszValue && ERROR_SUCCESS == ec)
  50. {
  51. ec = RegSetValueEx(
  52. hKey,
  53. NULL,
  54. 0,
  55. REG_SZ,
  56. (BYTE *)pszValue,
  57. (lstrlen(pszValue)+1)*sizeof(TCHAR));
  58. if (ERROR_SUCCESS == ec)
  59. bOk = TRUE;
  60. RegCloseKey(hKey);
  61. }
  62. return bOk;
  63. }
  64. // get the regKeyvalue,
  65. LRESULT GetRegKeyValue(HKEY hkeyParent, PCSTR pcszSubKey,
  66. PCSTR pcszValue, PDWORD pdwValueType,
  67. PBYTE pbyteBuf, PDWORD pdwcbBufLen)
  68. {
  69. LONG lResult;
  70. HKEY hkeySubKey;
  71. lResult = RegOpenKeyEx(hkeyParent, pcszSubKey, 0, KEY_QUERY_VALUE,
  72. &hkeySubKey);
  73. if (lResult == ERROR_SUCCESS)
  74. {
  75. LONG lResultClose;
  76. lResult = RegQueryValueEx(hkeySubKey, pcszValue, NULL, pdwValueType,
  77. pbyteBuf, pdwcbBufLen);
  78. lResultClose = RegCloseKey(hkeySubKey);
  79. if (lResult == ERROR_SUCCESS)
  80. lResult = lResultClose;
  81. }
  82. return(lResult);
  83. }
  84. /*F+F++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  85. Function: AddRegNamedValue
  86. Summary: Internal utility function to add a named data value to an
  87. existing Key (with optional Subkey) in the system Registry
  88. under HKEY_CLASSES_ROOT.
  89. Args: LPTSTR pszKey,
  90. LPTSTR pszSubkey,
  91. LPTSTR pszValueName,
  92. LPTSTR pszValue)
  93. Returns: BOOL
  94. TRUE if success; FALSE if not.
  95. ------------------------------------------------------------------------F-F*/
  96. BOOL AddRegNamedValue(
  97. LPTSTR pszKey,
  98. LPTSTR pszSubkey,
  99. LPTSTR pszValueName,
  100. LPTSTR pszValue)
  101. {
  102. BOOL bOk = FALSE;
  103. LONG ec;
  104. HKEY hKey;
  105. TCHAR szKey[MAX_STRING_LENGTH];
  106. lstrcpy(szKey, pszKey);
  107. if (NULL != pszSubkey)
  108. {
  109. lstrcat(szKey, TEXT("\\"));
  110. lstrcat(szKey, pszSubkey);
  111. }
  112. ec = RegOpenKeyEx(
  113. HKEY_CLASSES_ROOT,
  114. szKey,
  115. 0,
  116. KEY_ALL_ACCESS,
  117. &hKey);
  118. if (NULL != pszValue && ERROR_SUCCESS == ec)
  119. {
  120. ec = RegSetValueEx(
  121. hKey,
  122. pszValueName,
  123. 0,
  124. REG_SZ,
  125. (BYTE *)pszValue,
  126. (lstrlen(pszValue)+1)*sizeof(TCHAR));
  127. if (ERROR_SUCCESS == ec)
  128. bOk = TRUE;
  129. RegCloseKey(hKey);
  130. }
  131. return bOk;
  132. }
  133. #define HANDLERITEMSKEY TEXT("\\HandlerItems")
  134. // routines for storing and retreiving an ItemID associate with a CLSID and TextString
  135. // if the item isn't found an fCreateKey is true the key will be created for the item.
  136. STDMETHODIMP GetItemIdForHandlerItem(CLSID CLSIDServer,WCHAR *pszItemText,SYNCMGRITEMID *ItemID,BOOL fCreateKey)
  137. {
  138. #ifndef _UNICODE
  139. WCHAR wszID[GUID_SIZE+1];
  140. TCHAR wszItemText[GUID_SIZE+1];
  141. WCHAR wszItemdID[GUID_SIZE+1];
  142. #endif // !_UNICODE
  143. TCHAR szID[GUID_SIZE+1];
  144. TCHAR *pszLocalItemText = NULL;
  145. TCHAR szCLSID[GUID_SIZE+1];
  146. TCHAR szItemID[GUID_SIZE+1];
  147. DWORD dwBufLenght = GUID_SIZE+1;
  148. HRESULT lResult;
  149. #ifdef _UNICODE
  150. StringFromGUID2(CLSIDServer, szID, GUID_SIZE);
  151. StringFromGUID2(ItemID, szID, GUID_SIZE);
  152. pszLocalItemText = pszItemText;
  153. #else
  154. BOOL fUsedDefaultChar;
  155. // convert clsidServer
  156. StringFromGUID2(CLSIDServer, wszID, GUID_SIZE);
  157. WideCharToMultiByte(CP_ACP ,0,
  158. wszID,-1,szID,GUID_SIZE + 1,
  159. NULL,&fUsedDefaultChar);
  160. // convert ItemText
  161. WideCharToMultiByte(CP_ACP ,0,
  162. pszItemText,-1,wszItemText,GUID_SIZE + 1,
  163. NULL,&fUsedDefaultChar);
  164. pszLocalItemText = wszItemText;
  165. #endif // _UNICODE
  166. lstrcpy(szCLSID, TEXT("CLSID\\"));
  167. lstrcat(szCLSID, szID);
  168. lstrcat(szCLSID,HANDLERITEMSKEY);
  169. lstrcat(szCLSID,"\\");
  170. lstrcat(szCLSID,pszLocalItemText);
  171. lResult = GetRegKeyValue(HKEY_CLASSES_ROOT,szCLSID,
  172. NULL,NULL,
  173. (PBYTE) szItemID,&dwBufLenght);
  174. if (lResult != ERROR_SUCCESS &&
  175. TRUE == fCreateKey)
  176. {
  177. lResult = CoCreateGuid(ItemID);
  178. SetItemIdForHandlerItem(CLSIDServer,pszItemText
  179. ,*ItemID);
  180. }
  181. else
  182. {
  183. #ifdef _UNICODE
  184. lResult = IIDFromString(szID,ItemID);
  185. #else
  186. MultiByteToWideChar(CP_ACP, 0,
  187. szItemID, -1,
  188. wszItemdID, GUID_SIZE + 1);
  189. lResult = IIDFromString(wszItemdID,ItemID);
  190. #endif // _UNICODE
  191. }
  192. return lResult;
  193. }
  194. // sets the ItemID info so it can be retreived later.
  195. STDMETHODIMP SetItemIdForHandlerItem(CLSID CLSIDServer,WCHAR *pszItemText,SYNCMGRITEMID ItemID)
  196. {
  197. #ifndef _UNICODE
  198. WCHAR wszID[GUID_SIZE+1];
  199. TCHAR wszItemText[GUID_SIZE+1];
  200. WCHAR wszItemdID[GUID_SIZE+1];
  201. #endif // !_UNICODE
  202. TCHAR szID[GUID_SIZE+1];
  203. TCHAR *pszLocalItemText = NULL;
  204. TCHAR szCLSID[GUID_SIZE+1];
  205. TCHAR szItemID[GUID_SIZE+1];
  206. #ifdef _UNICODE
  207. StringFromGUID2(CLSIDServer, szID, GUID_SIZE);
  208. StringFromGUID2(ItemID, szID, GUID_SIZE);
  209. pszLocalItemText = pszItemText;
  210. #else
  211. BOOL fUsedDefaultChar;
  212. // convert clsidServer
  213. StringFromGUID2(CLSIDServer, wszID, GUID_SIZE);
  214. WideCharToMultiByte(CP_ACP ,0,
  215. wszID,-1,szID,GUID_SIZE + 1,
  216. NULL,&fUsedDefaultChar);
  217. // convert ItemID
  218. StringFromGUID2(ItemID, wszItemdID, GUID_SIZE);
  219. WideCharToMultiByte(CP_ACP ,0,
  220. wszItemdID,-1,szItemID,GUID_SIZE + 1,
  221. NULL,&fUsedDefaultChar);
  222. // convert ItemText
  223. WideCharToMultiByte(CP_ACP ,0,
  224. pszItemText,-1,wszItemText,GUID_SIZE + 1,
  225. NULL,&fUsedDefaultChar);
  226. pszLocalItemText = wszItemText;
  227. #endif // _UNICODE
  228. lstrcpy(szCLSID, TEXT("CLSID\\"));
  229. lstrcat(szCLSID, szID);
  230. lstrcat(szCLSID,HANDLERITEMSKEY);
  231. // Create entries under CLSID.
  232. SetRegKeyValue(HKEY_CLASSES_ROOT,
  233. szCLSID,
  234. NULL,
  235. "Handler Items");
  236. SetRegKeyValue(HKEY_CLASSES_ROOT,
  237. szCLSID,
  238. pszLocalItemText,
  239. szItemID);
  240. return NOERROR;
  241. }