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.

394 lines
10 KiB

  1. /*===================================================================
  2. Microsoft ASP
  3. Microsoft Confidential.
  4. Copyright 1997 Microsoft Corporation. All Rights Reserved.
  5. Component: Package Management Tool
  6. File: main.cpp
  7. Owner: leijin
  8. Note:
  9. ===================================================================*/
  10. #include <tchar.h>
  11. #include "admutil.h"
  12. #include <stdio.h>
  13. #include <objbase.h>
  14. #include <initguid.h>
  15. //#ifdef _WAMREG_LINK_DIRECT
  16. //#include "..\wmrgexp.h"
  17. //#endif
  18. #include <iwamreg.h>
  19. #define SIZE_STRING_BUFFER 1024
  20. void CAdmUtil::OpenWamAdm (const CString & strComputer)
  21. {
  22. IClassFactory * pcsfFactory = NULL;
  23. COSERVERINFO csiMachineName;
  24. COSERVERINFO *pcsiParam = NULL;
  25. OLECHAR rgchMachineName[MAX_PATH];
  26. //release previous interface if needed
  27. if(pIWamAdm!=0)
  28. {
  29. //reuse the interface
  30. return;
  31. //pIWamAdm->Release();
  32. //pIWamAdm=0;
  33. }
  34. //convert to OLECHAR[];
  35. #if UNICODE
  36. wsprintfW( rgchMachineName, L"%s", LPCTSTR(strComputer));
  37. #else
  38. wsprintfW( rgchMachineName, L"%S", LPCTSTR(strComputer));
  39. #endif
  40. //fill the structure for CoGetClassObject
  41. ZeroMemory( &csiMachineName, sizeof(csiMachineName) );
  42. // csiMachineName.pAuthInfo = NULL;
  43. // csiMachineName.dwFlags = 0;
  44. // csiMachineName.pServerInfoExt = NULL;
  45. pcsiParam = &csiMachineName;
  46. csiMachineName.pwszName = (strComputer.IsEmpty())?NULL:rgchMachineName;
  47. hresError = CoGetClassObject(CLSID_WamAdmin, CLSCTX_SERVER, pcsiParam,
  48. IID_IClassFactory, (void**) &pcsfFactory);
  49. if (FAILED(hresError))
  50. {
  51. Error(_T("Failed to CoGetClassObject of WamAdm object"));
  52. }
  53. else {
  54. hresError = pcsfFactory->CreateInstance(NULL, IID_IWamAdmin, (void **) &pIWamAdm);
  55. if (FAILED(hresError)) Error(_T("Failed to CreateInstance of WamAdm object"));
  56. pcsfFactory->Release();
  57. }
  58. }
  59. void CAdmUtil::CloseWamAdm (void)
  60. {
  61. if(pIWamAdm!=0)
  62. {
  63. pIWamAdm->Release();
  64. pIWamAdm=0;
  65. }
  66. }
  67. void CAdmUtil::AppCreateInProc(const _TCHAR* szPath,const CString & strComputer)
  68. {
  69. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  70. HRESULT hr = NOERROR;
  71. //
  72. INT cSize = 0;
  73. INT cch = 0;
  74. #ifndef UNICODE
  75. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  76. if (cSize == 0)
  77. {
  78. hresError = GetLastError();
  79. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  80. {
  81. Error(_T("metabase path exceeds 1024 chars"));
  82. }
  83. else
  84. Error("");
  85. return;
  86. }
  87. #else
  88. wcscpy(wszMetabasePath,szPath);
  89. #endif
  90. OpenWamAdm(strComputer);
  91. if (SUCCEEDED(hresError) && pIWamAdm!=0)
  92. {
  93. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  94. hresError = pIWamAdm->AppCreate(wszMetabasePath, TRUE);
  95. if (FAILED(hresError))
  96. {
  97. Error(_T("create failed"));
  98. }
  99. else
  100. {
  101. Print(_T("application created\n"));
  102. }
  103. }
  104. //CloseWamAdm();
  105. return;
  106. }
  107. void CAdmUtil::AppCreateOutProc(const _TCHAR* szPath,const CString & strComputer)
  108. {
  109. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  110. INT cSize = 0;
  111. INT cch = 0;
  112. #ifndef UNICODE
  113. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  114. if (cSize == 0)
  115. {
  116. hresError = GetLastError();
  117. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  118. {
  119. Error(_T("metabase path exceeds 1024 chars"));
  120. }
  121. else
  122. Error("");
  123. return;
  124. }
  125. #else
  126. wcscpy(wszMetabasePath,szPath);
  127. #endif
  128. OpenWamAdm(strComputer);
  129. if (SUCCEEDED(hresError)&& pIWamAdm!=0)
  130. {
  131. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  132. hresError = pIWamAdm->AppCreate(wszMetabasePath, FALSE);
  133. if (FAILED(hresError))
  134. {
  135. Error(_T("create failed"));
  136. }
  137. else
  138. {
  139. Print(_T("application created\n"));
  140. }
  141. }
  142. //CloseWamAdm();
  143. return;
  144. }
  145. void CAdmUtil::AppDelete(const _TCHAR* szPath,const CString & strComputer)
  146. {
  147. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  148. INT cSize = 0;
  149. INT cch = 0;
  150. #ifndef UNICODE
  151. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  152. if (cSize == 0)
  153. {
  154. hresError = GetLastError();
  155. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  156. {
  157. Error(_T("metabase path exceeds 1024 chars"));
  158. }
  159. else
  160. Error("");
  161. return;
  162. }
  163. #else
  164. wcscpy(wszMetabasePath,szPath);
  165. #endif
  166. OpenWamAdm(strComputer);
  167. if (SUCCEEDED(hresError)&& pIWamAdm!=0)
  168. {
  169. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  170. hresError = pIWamAdm->AppDelete(wszMetabasePath,FALSE);
  171. if (FAILED(hresError))
  172. {
  173. Error(_T("delete failed"));
  174. }
  175. else
  176. {
  177. Print(_T("application deleted\n"));
  178. }
  179. }
  180. //CloseWamAdm();
  181. return;
  182. }
  183. void CAdmUtil::AppRename(CAdmNode& a_AdmNode, CAdmNode& a_AdmDstNode, const CString & strComputer)
  184. {
  185. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  186. WCHAR wszMetabaseDstPath[SIZE_STRING_BUFFER];
  187. INT cSize = 0;
  188. INT cch = 0;
  189. #ifndef UNICODE
  190. cSize = MultiByteToWideChar(0, 0, LPCTSTR(a_AdmNode.GetLMNodePath()), -1, wszMetabasePath, SIZE_STRING_BUFFER);
  191. if (cSize == 0)
  192. {
  193. hresError = GetLastError();
  194. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  195. {
  196. Error(_T("metabase path exceeds 1024 chars"));
  197. }
  198. else
  199. Error("");
  200. return;
  201. }
  202. wcscat(wszMetabaseDstPath,L"/");
  203. cSize = MultiByteToWideChar(0, 0, LPCTSTR(a_AdmDstNode.GetLMNodePath()), -1, wszMetabaseDstPath, SIZE_STRING_BUFFER);
  204. if (cSize == 0)
  205. {
  206. hresError = GetLastError();
  207. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  208. {
  209. Error(_T("metabase path exceeds 1024 chars"));
  210. }
  211. else
  212. Error("");
  213. return;
  214. }
  215. wcscat(wszMetabaseDstPath,L"/");
  216. #else
  217. wcscpy(wszMetabasePath,LPCTSTR(a_AdmNode.GetLMNodePath()));
  218. wcscpy(wszMetabaseDstPath,LPCTSTR(a_AdmDstNode.GetLMNodePath()));
  219. #endif
  220. OpenWamAdm(strComputer);
  221. if (SUCCEEDED(hresError)&& pIWamAdm!=0)
  222. {
  223. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  224. hresError = pIWamAdm->AppDeleteRecoverable(wszMetabasePath,TRUE /*Recursive*/);
  225. if (FAILED(hresError))
  226. {
  227. Error(_T("AppDeleteRecoverable() failed"));
  228. }
  229. else
  230. {
  231. RenameObject(a_AdmNode,a_AdmDstNode);
  232. if(FAILED(QueryLastHresError()))
  233. {
  234. Error(_T("node failed to be renamed"));
  235. }
  236. else
  237. {
  238. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  239. hresError = pIWamAdm->AppRecover(wszMetabaseDstPath,TRUE /*Recursive*/);
  240. if (FAILED(hresError))
  241. {
  242. Error(_T("AppRecover() failed"));
  243. }
  244. else
  245. {
  246. Print(_T("application renamed\n"));
  247. }
  248. }
  249. }
  250. }
  251. //CloseWamAdm();
  252. return;
  253. }
  254. void CAdmUtil::AppUnLoad(const _TCHAR* szPath,const CString & strComputer)
  255. {
  256. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  257. INT cSize = 0;
  258. INT cch = 0;
  259. #ifndef UNICODE
  260. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  261. if (cSize == 0)
  262. {
  263. hresError = GetLastError();
  264. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  265. {
  266. Error(_T("metabase path exceeds 1024 chars"));
  267. }
  268. else
  269. Error("");
  270. return;
  271. }
  272. #else
  273. wcscpy(wszMetabasePath,szPath);
  274. #endif
  275. OpenWamAdm(strComputer);
  276. if (SUCCEEDED(hresError)&& pIWamAdm!=0)
  277. {
  278. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  279. hresError = pIWamAdm->AppUnLoad(wszMetabasePath,FALSE);
  280. if (FAILED(hresError))
  281. {
  282. Error(_T("Unload application failed"));
  283. }
  284. else
  285. {
  286. Print(_T("application unloaded\n"));
  287. }
  288. }
  289. //CloseWamAdm();
  290. return;
  291. }
  292. void CAdmUtil::AppGetStatus(const _TCHAR* szPath,const CString & strComputer)
  293. {
  294. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  295. INT cSize = 0;
  296. INT cch = 0;
  297. #ifndef UNICODE
  298. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  299. if (cSize == 0)
  300. {
  301. hresError = GetLastError();
  302. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  303. {
  304. Error(_T("The Metabase path exceeds 1024 chars"));
  305. }
  306. else
  307. Error("");
  308. return;
  309. }
  310. #else
  311. wcscpy(wszMetabasePath,szPath);
  312. #endif
  313. OpenWamAdm(strComputer);
  314. if (SUCCEEDED(hresError)&& pIWamAdm!=0)
  315. {
  316. DWORD dwStatus;
  317. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  318. hresError = pIWamAdm->AppGetStatus(wszMetabasePath, &dwStatus);
  319. if (FAILED(hresError))
  320. {
  321. Error(_T("GetStatus of application %s failed"));
  322. }
  323. else
  324. {
  325. if (dwStatus == APPSTATUS_NOTDEFINED)
  326. {
  327. Print(_T("Application is not defined\n"));
  328. }
  329. else if (dwStatus == APPSTATUS_STOPPED)
  330. {
  331. Print(_T("Application is stopped\n"));
  332. }
  333. else if (dwStatus == APPSTATUS_RUNNING)
  334. {
  335. Print(_T("Application is running\n"));
  336. }
  337. else
  338. {
  339. Print(_T("Application is in unknown state\n"));
  340. }
  341. }
  342. }
  343. //CloseWamAdm();
  344. return;
  345. }