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.

259 lines
6.3 KiB

  1. //
  2. // Copyright (C) 2000 Microsoft Corporation. All Rights Reserved.
  3. //
  4. //
  5. //
  6. //==============================================================;
  7. #include <windows.h>
  8. #include <objbase.h>
  9. #include <olectl.h>
  10. extern GUID CLSID_CTSRemotePage;
  11. LPCWSTR g_szExtKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Controls Folder\\"
  12. L"System\\shellex\\PropertySheetHandlers\\Remote Sessions CPL Extension";
  13. LPCWSTR g_szApprovedKey = L"Software\\Microsoft\\Windows\\CurrentVersion\\Shell Extensions\\Approved";
  14. const WCHAR g_szExtName[] = L"Remote Sessions CPL Extension";
  15. //*************************************************************
  16. //
  17. // RegisterServer()
  18. //
  19. // Purpose: Register the component in the registry
  20. //
  21. // Parameters: hModule - handle to this dll module
  22. //
  23. //
  24. // Return: S_OK if success, error code otherwise
  25. //
  26. // Comments:
  27. //
  28. // History: Date Author Comment
  29. // 5/26/00 a-skuzin Created
  30. // 10/27/00 skuzin Added registration of the
  31. // component as "Approved" CPL
  32. // extension
  33. //
  34. //
  35. //*************************************************************
  36. HRESULT
  37. RegisterServer(HMODULE hModule)
  38. {
  39. // Get server location.
  40. WCHAR szModule[MAX_PATH+1] ;
  41. if(!GetModuleFileName(hModule, szModule, MAX_PATH))
  42. {
  43. return E_UNEXPECTED;
  44. }
  45. // Get CLSID
  46. LPOLESTR szCLSID = NULL ;
  47. HRESULT hr = StringFromCLSID(CLSID_CTSRemotePage, &szCLSID) ;
  48. if(FAILED(hr))
  49. {
  50. return hr;
  51. }
  52. // Build the key CLSID\\{...}
  53. LPWSTR szKey = new WCHAR[wcslen(L"CLSID\\")+wcslen(szCLSID)+1];
  54. if(!szKey)
  55. {
  56. CoTaskMemFree(szCLSID);
  57. return E_OUTOFMEMORY;
  58. }
  59. wcscpy(szKey, L"CLSID\\") ;
  60. wcscat(szKey, szCLSID) ;
  61. HKEY hKey1,hKey2;
  62. LONG Err, TotalErr = 0;
  63. // Create "CLSID\{...}" key
  64. Err = RegCreateKeyExW(HKEY_CLASSES_ROOT, szKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey1, NULL);
  65. delete szKey;
  66. TotalErr |= Err;
  67. if(Err == ERROR_SUCCESS )
  68. {
  69. Err = RegSetValueExW(hKey1, NULL, 0, REG_SZ,
  70. (CONST BYTE *)g_szExtName,
  71. sizeof(g_szExtName));
  72. TotalErr |= Err;
  73. // Create "CLSID\{...}\InprocServer32" key
  74. Err = RegCreateKeyExW(hKey1, L"InprocServer32", 0, NULL, 0, KEY_WRITE, NULL, &hKey2, NULL);
  75. TotalErr |= Err;
  76. RegCloseKey(hKey1);
  77. if(Err == ERROR_SUCCESS)
  78. {
  79. Err = RegSetValueExW(hKey2, NULL, 0, REG_SZ,
  80. (CONST BYTE *)szModule,
  81. (wcslen(szModule)+1)*sizeof(WCHAR));
  82. TotalErr |= Err;
  83. Err = RegSetValueExW(hKey2, L"ThreadingModel", 0, REG_SZ,
  84. (CONST BYTE *)L"Apartment",
  85. (wcslen(L"Apartment")+1)*sizeof(WCHAR));
  86. TotalErr |= Err;
  87. RegCloseKey(hKey2);
  88. }
  89. }
  90. //Register the component as System property sheet extension
  91. Err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, g_szExtKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey1, NULL);
  92. TotalErr |= Err;
  93. if(Err == ERROR_SUCCESS )
  94. {
  95. Err = RegSetValueExW(hKey1, NULL, 0, REG_SZ,
  96. (CONST BYTE *)szCLSID,
  97. (wcslen(szCLSID)+1)*sizeof(WCHAR));
  98. TotalErr |= Err;
  99. RegCloseKey(hKey1);
  100. }
  101. //Make this property sheet extension "Approved"
  102. Err = RegCreateKeyExW(HKEY_LOCAL_MACHINE, g_szApprovedKey, 0, NULL, 0, KEY_WRITE, NULL, &hKey1, NULL);
  103. TotalErr |= Err;
  104. if(Err == ERROR_SUCCESS )
  105. {
  106. Err = RegSetValueExW(hKey1, szCLSID, 0, REG_SZ,
  107. (CONST BYTE *)g_szExtName,
  108. sizeof(g_szExtName));
  109. TotalErr |= Err;
  110. RegCloseKey(hKey1);
  111. }
  112. // Free memory.
  113. CoTaskMemFree(szCLSID) ;
  114. if( TotalErr == ERROR_SUCCESS )
  115. {
  116. return S_OK;
  117. }
  118. else
  119. {
  120. return SELFREG_E_CLASS;
  121. }
  122. }
  123. //*************************************************************
  124. //
  125. // UnregisterServer()
  126. //
  127. // Purpose: Deletes the component registration values
  128. // from the registry
  129. //
  130. // Parameters: NONE
  131. //
  132. //
  133. // Return: S_OK if success, error code otherwise
  134. //
  135. // Comments:
  136. //
  137. // History: Date Author Comment
  138. // 5/26/00 a-skuzin Created
  139. // 10/27/00 skuzin Modifyed to reflect
  140. // changes in RegisterServer()
  141. //
  142. //*************************************************************
  143. HRESULT
  144. UnregisterServer()
  145. {
  146. // Get CLSID
  147. LPOLESTR szCLSID = NULL ;
  148. HRESULT hr = StringFromCLSID(CLSID_CTSRemotePage, &szCLSID) ;
  149. if(FAILED(hr))
  150. {
  151. return hr;
  152. }
  153. // Build the key CLSID\\{...}\\InprocServer32
  154. LPWSTR szKey = new WCHAR[wcslen(L"CLSID\\")+wcslen(szCLSID)+wcslen(L"\\InprocServer32")+1];
  155. if(!szKey)
  156. {
  157. CoTaskMemFree(szCLSID);
  158. return E_OUTOFMEMORY;
  159. }
  160. wcscpy(szKey, L"CLSID\\");
  161. wcscat(szKey, szCLSID);
  162. wcscat(szKey, L"\\InprocServer32");
  163. LONG Wrn, Err, TotalErr = ERROR_SUCCESS;
  164. // Delete "CLSID\{...}\InprocServer32" key
  165. Err = RegDeleteKey(HKEY_CLASSES_ROOT, szKey) ;
  166. TotalErr |= Err;
  167. //Try to delete "CLSID\{...}" key
  168. //It is not an error if we cannot do this.
  169. if(Err == ERROR_SUCCESS )
  170. {
  171. szKey[wcslen(szKey)-wcslen(L"\\InprocServer32")] = 0;
  172. Wrn = RegDeleteKey(HKEY_CLASSES_ROOT, szKey);
  173. }
  174. delete szKey;
  175. //Delete Property Sheet Handler registration
  176. TotalErr |= RegDeleteKey(HKEY_LOCAL_MACHINE, g_szExtKey);
  177. //Remove component from list of "Approved" extensions
  178. HKEY hKey;
  179. Err = RegOpenKeyEx(HKEY_LOCAL_MACHINE,g_szApprovedKey,0,KEY_WRITE,&hKey);
  180. TotalErr |= Err;
  181. if( Err == ERROR_SUCCESS )
  182. {
  183. TotalErr|= RegDeleteValue(hKey,szCLSID);
  184. RegCloseKey(hKey);
  185. }
  186. // Free memory.
  187. CoTaskMemFree(szCLSID);
  188. if( TotalErr == ERROR_SUCCESS )
  189. {
  190. if(Wrn == ERROR_SUCCESS)
  191. {
  192. return S_OK;
  193. }
  194. else
  195. {
  196. //we could not delete "CLSID\{...}" key
  197. //probably it has subkeys created by a user.
  198. return S_FALSE;
  199. }
  200. }
  201. else
  202. {
  203. return SELFREG_E_CLASS;
  204. }
  205. }