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.

319 lines
9.7 KiB

  1. //#--------------------------------------------------------------
  2. //
  3. // File: shortcut.cpp
  4. //
  5. // Synopsis: Implementation the custom action to add/remove the
  6. // "Remote Administration Tools" to/from the start menu
  7. //
  8. // Copyright (C) Microsoft Corporation. All rights reserved.
  9. //
  10. //----------------------------------------------------------------
  11. #include "precomp.h"
  12. #include <satrace.h>
  13. #include <msi.h>
  14. #include <string>
  15. #include <shlobj.h>
  16. #include <shellapi.h>
  17. #include <shortcutresource.h>
  18. #undef _ATL_NO_DEBUG_CRT
  19. #include <atlbase.h>
  20. //
  21. // Web Interface for Remote Administration
  22. // Manage a Web or file server using a Web browser interface
  23. //
  24. using namespace std;
  25. //
  26. // Constants for creating a shortcut to the Administration site
  27. //
  28. WCHAR SHORTCUT_EXT [] = L".lnk";
  29. WCHAR SECURELAUNCH_PATH [] = L"\\ServerAppliance\\SecureLaunch.vbs";
  30. WCHAR WSCRIPT_PATH[] = L"\\wscript.exe";
  31. //
  32. // note - the following is just a file name and does not need to be localized
  33. //
  34. WCHAR SHORTCUT_FILE_NAME [] = L"Remote Administration Tools";
  35. WCHAR RESOURCE_FILE_NAME [] = L"\\sainstall.dll";
  36. WCHAR SYSTEM_32_PATH [] = L"%systemroot%\\system32";
  37. //++--------------------------------------------------------------
  38. //
  39. // Function: CreateSAKShortcut
  40. //
  41. // Synopsis: This is export function to add "Remote Administration Tools" shortcut
  42. // to the start menu
  43. //
  44. // Arguments:
  45. // [in] HANDLE - handle passed in by MSI
  46. //
  47. // Returns: DWORD - success/failure
  48. //
  49. // History: MKarki Created 12/04/2002
  50. //
  51. //----------------------------------------------------------------
  52. DWORD __stdcall
  53. CreateSAKShortcut (
  54. /*[in]*/ MSIHANDLE hInstall
  55. )
  56. {
  57. CSATraceFunc objTraceFunc ("CreateSAKShortCut");
  58. DWORD dwRetVal = -1;
  59. do
  60. {
  61. //
  62. // Get the path to %System32%
  63. //
  64. WCHAR pwsSystemPath[MAX_PATH+1];
  65. HRESULT hr = SHGetFolderPath(
  66. NULL,
  67. CSIDL_SYSTEM,
  68. NULL,
  69. SHGFP_TYPE_CURRENT,
  70. pwsSystemPath);
  71. if (FAILED(hr))
  72. {
  73. SATracePrintf ("SHGetFolderPath failed getting the System32 path with error:%x", hr);
  74. OutputDebugString (L"SHGetFolderPath failed getting the System32 path with error\n");
  75. break;
  76. }
  77. //
  78. // Construct the path to wscript.exe
  79. //
  80. wstring wsWScriptPath(pwsSystemPath);
  81. wsWScriptPath += WSCRIPT_PATH;
  82. SATracePrintf ("WScript Path = %ws", wsWScriptPath.data());
  83. //
  84. // Construct the path to SecureLaunch.vbs
  85. //
  86. wstring wsLaunchPath(pwsSystemPath);
  87. wsLaunchPath += SECURELAUNCH_PATH;
  88. SATracePrintf ("Secure Launch Path = %ws", wsLaunchPath.data());
  89. //
  90. //Construct the path where the shortcut will be stored in the Startup folder
  91. //
  92. //
  93. //Get the path to the Administrators Tools folder
  94. //
  95. WCHAR pwsStartMenuPath[MAX_PATH+1];
  96. hr = SHGetFolderPath(NULL,
  97. CSIDL_COMMON_ADMINTOOLS,
  98. NULL,
  99. SHGFP_TYPE_CURRENT,
  100. pwsStartMenuPath);
  101. if (FAILED(hr))
  102. {
  103. SATracePrintf ("SHGetFolderPath failed getting the Start Menu path with error:%x", hr);
  104. OutputDebugString (L"SHGetFolderPath failed getting the System32 path with error");
  105. break;
  106. }
  107. wstring wsPathLink(pwsStartMenuPath);
  108. wsPathLink += L"\\";
  109. wsPathLink += SHORTCUT_FILE_NAME;
  110. wsPathLink += SHORTCUT_EXT;
  111. SATracePrintf(" PathLink = %ws", wsPathLink.data());
  112. //
  113. // Now that the shortcut information has been constructed,
  114. // create the shortcut object.
  115. //
  116. CComPtr <IShellLink> psl;
  117. //
  118. // Get a pointer to the IShellLink interface.
  119. //
  120. hr = CoCreateInstance (
  121. CLSID_ShellLink,
  122. NULL,
  123. CLSCTX_INPROC_SERVER,
  124. IID_IShellLink,
  125. (LPVOID *)&psl);
  126. if (FAILED(hr))
  127. {
  128. SATracePrintf ("ShellLink CoCreateInstance Failed with error:%x",hr);
  129. OutputDebugString (L"ShellLink CoCreateInstance Failed");
  130. break;
  131. }
  132. WCHAR wszShortcutResourceID [MAX_PATH +1];
  133. _itow (IDS_SAK_SHORTCUT_DESCRIPTION, wszShortcutResourceID, 10);
  134. wstring wsShortcutDescription (L"@");
  135. wsShortcutDescription += pwsSystemPath;
  136. wsShortcutDescription += RESOURCE_FILE_NAME;
  137. wsShortcutDescription += L",-";
  138. wsShortcutDescription += wszShortcutResourceID;
  139. SATracePrintf ("ShortCut Description:%ws", wsShortcutDescription.data ());
  140. //
  141. // Set the information for the shortcut
  142. //
  143. psl->SetPath(wsWScriptPath.data());
  144. psl->SetArguments(wsLaunchPath.data());
  145. psl->SetDescription(wsShortcutDescription.data ());
  146. //
  147. // the following really doesn't get the icon - because there is no icon in this DLL
  148. // it is too late to add an icon for .NET Server
  149. //
  150. psl->SetIconLocation(L"sasetupca.dll", 0);
  151. SATraceString ("Saving shortcut to file");
  152. //
  153. // Query IShellLink for the IPersistFile interface for saving the
  154. // shortcut in persistent storage.
  155. //
  156. CComPtr <IPersistFile> ppf;
  157. hr = psl->QueryInterface(
  158. IID_IPersistFile,
  159. (LPVOID*)&ppf
  160. );
  161. if (FAILED(hr))
  162. {
  163. SATracePrintf ("QueryInterface failed for IPersistFile with error:%x",hr);
  164. OutputDebugString (L"QueryInterface failed for IPersistFile\n");
  165. break;
  166. }
  167. SATraceString ("Pointer to IPersistFile retrieved");
  168. //
  169. // Save the link by calling IPersistFile::Save.
  170. //
  171. hr = ppf->Save(wsPathLink.data(), TRUE);
  172. if (FAILED(hr))
  173. {
  174. SATracePrintf ("Failed to save shortcut with error:%x", hr);
  175. OutputDebugString (L"Failed to save shortcut\n");
  176. break;
  177. }
  178. SATraceString ("Successfully saved shortcut");
  179. OutputDebugString (L"Successfully saved shortcut");
  180. wstring wsLocalizedFileNameResource (SYSTEM_32_PATH);
  181. wsLocalizedFileNameResource += RESOURCE_FILE_NAME;
  182. //
  183. // set the localized name of the shortcut
  184. //
  185. hr = SHSetLocalizedName (
  186. (LPWSTR) wsPathLink.data (),
  187. wsLocalizedFileNameResource.data (),
  188. IDS_SAK_SHORTCUT_NAME
  189. );
  190. if (FAILED (hr))
  191. {
  192. SATracePrintf ("Failed on SHSetLocalizedFilaName with error:%x", hr);
  193. OutputDebugString (L"Failed on SHSetLocalizedFilaName");
  194. break;
  195. }
  196. SATraceString ("Successfully created shortcut");
  197. OutputDebugString (L"Successfully created shortcut");
  198. //
  199. // done creating the shortcut
  200. //
  201. dwRetVal = ERROR_SUCCESS;
  202. }
  203. while (false);
  204. return (dwRetVal);
  205. } // end of CreateSAKShortcut function
  206. //++--------------------------------------------------------------
  207. //
  208. // Function: RemoveSAKShortcut
  209. //
  210. // Synopsis: This is export function to remove "Remote Administration Tools" shortcut
  211. // to the start menu
  212. //
  213. // Arguments:
  214. // [in] HANDLE - handle passed in by MSI
  215. //
  216. // Returns: DWORD - success/failure
  217. //
  218. // History: MKarki Created 12/04/2002
  219. //
  220. //----------------------------------------------------------------
  221. DWORD __stdcall
  222. RemoveSAKShortcut (
  223. /*[in]*/ MSIHANDLE hInstall
  224. )
  225. {
  226. CSATraceFunc objTraceFunc ("RemoveSAKShortcut");
  227. DWORD dwRetVal = -1;
  228. do
  229. {
  230. //
  231. //Construct the path where the shortcut will be stored in the Startup folder
  232. //
  233. //
  234. //Get the path to the Administrators Tools folder
  235. //
  236. WCHAR pwsStartMenuPath[MAX_PATH +1];
  237. HRESULT hr = SHGetFolderPath(NULL,
  238. CSIDL_COMMON_ADMINTOOLS,
  239. NULL,
  240. SHGFP_TYPE_CURRENT,
  241. pwsStartMenuPath);
  242. if (FAILED(hr))
  243. {
  244. SATracePrintf ("SHGetFolderPath failed getting the Start Menu path with error:%x", hr);
  245. break;
  246. }
  247. wstring wsPathLink(pwsStartMenuPath);
  248. wsPathLink += L"\\";
  249. wsPathLink += SHORTCUT_FILE_NAME;
  250. wsPathLink += SHORTCUT_EXT;
  251. SATracePrintf(" PathLink = %ws", wsPathLink.data());
  252. //
  253. // delete the shortcut now -
  254. //
  255. BOOL bRetVal = DeleteFile (wsPathLink.data ());
  256. if (FALSE == bRetVal)
  257. {
  258. SATracePrintf ("Failed to Delete File with error:%x", GetLastError ());
  259. break;
  260. }
  261. //
  262. // success
  263. //
  264. dwRetVal = ERROR_SUCCESS;
  265. }
  266. while (false);
  267. return (dwRetVal);
  268. } // end of RemoveSAKShortcut function