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.

445 lines
12 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 && pIWamAdm2!=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. hresError = pcsfFactory->CreateInstance(NULL, IID_IWamAdmin2, (void **) &pIWamAdm2);
  57. if (FAILED(hresError)) Error(_T("Failed to CreateInstance of WamAdm2 object"));
  58. pcsfFactory->Release();
  59. }
  60. }
  61. void CAdmUtil::CloseWamAdm (void)
  62. {
  63. if(pIWamAdm!=0)
  64. {
  65. pIWamAdm->Release();
  66. pIWamAdm2->Release();
  67. pIWamAdm=0; pIWamAdm2=0;
  68. }
  69. }
  70. void CAdmUtil::AppCreateInProc(const _TCHAR* szPath,const CString & strComputer)
  71. {
  72. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  73. HRESULT hr = NOERROR;
  74. //
  75. INT cSize = 0;
  76. INT cch = 0;
  77. #ifndef UNICODE
  78. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  79. if (cSize == 0)
  80. {
  81. hresError = GetLastError();
  82. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  83. {
  84. Error(_T("metabase path exceeds 1024 chars"));
  85. }
  86. else
  87. Error("");
  88. return;
  89. }
  90. #else
  91. wcscpy(wszMetabasePath,szPath);
  92. #endif
  93. OpenWamAdm(strComputer);
  94. if (pIWamAdm!=0)
  95. {
  96. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  97. hresError = pIWamAdm2->AppCreate2(wszMetabasePath, eAppRunInProc);
  98. if (FAILED(hresError))
  99. {
  100. Error(_T("create failed"));
  101. }
  102. else
  103. {
  104. Print(_T("inproc application created\n"));
  105. }
  106. }
  107. //CloseWamAdm();
  108. return;
  109. }
  110. void CAdmUtil::AppCreateOutPool(const _TCHAR* szPath,const CString & strComputer)
  111. {
  112. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  113. HRESULT hr = NOERROR;
  114. //
  115. INT cSize = 0;
  116. INT cch = 0;
  117. #ifndef UNICODE
  118. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  119. if (cSize == 0)
  120. {
  121. hresError = GetLastError();
  122. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  123. {
  124. Error(_T("metabase path exceeds 1024 chars"));
  125. }
  126. else
  127. Error("");
  128. return;
  129. }
  130. #else
  131. wcscpy(wszMetabasePath,szPath);
  132. #endif
  133. OpenWamAdm(strComputer);
  134. if (pIWamAdm!=0)
  135. {
  136. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  137. hresError = pIWamAdm2->AppCreate2(wszMetabasePath, eAppRunOutProcInDefaultPool);
  138. if (FAILED(hresError))
  139. {
  140. Error(_T("pooled create failed"));
  141. }
  142. else
  143. {
  144. Print(_T("pooled application created\n"));
  145. }
  146. }
  147. //CloseWamAdm();
  148. return;
  149. }
  150. void CAdmUtil::AppCreateOutProc(const _TCHAR* szPath,const CString & strComputer)
  151. {
  152. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  153. INT cSize = 0;
  154. INT cch = 0;
  155. #ifndef UNICODE
  156. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  157. if (cSize == 0)
  158. {
  159. hresError = GetLastError();
  160. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  161. {
  162. Error(_T("metabase path exceeds 1024 chars"));
  163. }
  164. else
  165. Error("");
  166. return;
  167. }
  168. #else
  169. wcscpy(wszMetabasePath,szPath);
  170. #endif
  171. OpenWamAdm(strComputer);
  172. if (pIWamAdm!=0)
  173. {
  174. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  175. hresError = pIWamAdm->AppCreate(wszMetabasePath, FALSE);
  176. if (FAILED(hresError))
  177. {
  178. Error(_T("create failed"));
  179. }
  180. else
  181. {
  182. Print(_T("application created\n"));
  183. }
  184. }
  185. //CloseWamAdm();
  186. return;
  187. }
  188. void CAdmUtil::AppDelete(const _TCHAR* szPath,const CString & strComputer)
  189. {
  190. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  191. INT cSize = 0;
  192. INT cch = 0;
  193. #ifndef UNICODE
  194. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  195. if (cSize == 0)
  196. {
  197. hresError = GetLastError();
  198. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  199. {
  200. Error(_T("metabase path exceeds 1024 chars"));
  201. }
  202. else
  203. Error("");
  204. return;
  205. }
  206. #else
  207. wcscpy(wszMetabasePath,szPath);
  208. #endif
  209. OpenWamAdm(strComputer);
  210. if (pIWamAdm!=0)
  211. {
  212. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  213. hresError = pIWamAdm->AppDelete(wszMetabasePath,FALSE);
  214. if (FAILED(hresError))
  215. {
  216. Error(_T("delete failed"));
  217. }
  218. else
  219. {
  220. Print(_T("application deleted\n"));
  221. }
  222. }
  223. //CloseWamAdm();
  224. return;
  225. }
  226. void CAdmUtil::AppRename(CAdmNode& a_AdmNode, CAdmNode& a_AdmDstNode, const CString & strComputer)
  227. {
  228. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  229. WCHAR wszMetabaseDstPath[SIZE_STRING_BUFFER];
  230. INT cSize = 0;
  231. INT cch = 0;
  232. #ifndef UNICODE
  233. cSize = MultiByteToWideChar(0, 0, LPCTSTR(a_AdmNode.GetLMNodePath()), -1, wszMetabasePath, SIZE_STRING_BUFFER);
  234. if (cSize == 0)
  235. {
  236. hresError = GetLastError();
  237. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  238. {
  239. Error(_T("metabase path exceeds 1024 chars"));
  240. }
  241. else
  242. Error("");
  243. return;
  244. }
  245. wcscat(wszMetabaseDstPath,L"/");
  246. cSize = MultiByteToWideChar(0, 0, LPCTSTR(a_AdmDstNode.GetLMNodePath()), -1, wszMetabaseDstPath, SIZE_STRING_BUFFER);
  247. if (cSize == 0)
  248. {
  249. hresError = GetLastError();
  250. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  251. {
  252. Error(_T("metabase path exceeds 1024 chars"));
  253. }
  254. else
  255. Error("");
  256. return;
  257. }
  258. wcscat(wszMetabaseDstPath,L"/");
  259. #else
  260. wcscpy(wszMetabasePath,LPCTSTR(a_AdmNode.GetLMNodePath()));
  261. wcscpy(wszMetabaseDstPath,LPCTSTR(a_AdmDstNode.GetLMNodePath()));
  262. #endif
  263. OpenWamAdm(strComputer);
  264. if (pIWamAdm!=0)
  265. {
  266. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  267. hresError = pIWamAdm->AppDeleteRecoverable(wszMetabasePath,TRUE /*Recursive*/);
  268. if (FAILED(hresError))
  269. {
  270. Error(_T("AppDeleteRecoverable() failed"));
  271. }
  272. else
  273. {
  274. RenameObject(a_AdmNode,a_AdmDstNode);
  275. if(FAILED(QueryLastHresError()))
  276. {
  277. Error(_T("node failed to be renamed"));
  278. }
  279. else
  280. {
  281. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  282. hresError = pIWamAdm->AppRecover(wszMetabaseDstPath,TRUE /*Recursive*/);
  283. if (FAILED(hresError))
  284. {
  285. Error(_T("AppRecover() failed"));
  286. }
  287. else
  288. {
  289. Print(_T("application renamed\n"));
  290. }
  291. }
  292. }
  293. }
  294. //CloseWamAdm();
  295. return;
  296. }
  297. void CAdmUtil::AppUnLoad(const _TCHAR* szPath,const CString & strComputer)
  298. {
  299. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  300. INT cSize = 0;
  301. INT cch = 0;
  302. #ifndef UNICODE
  303. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  304. if (cSize == 0)
  305. {
  306. hresError = GetLastError();
  307. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  308. {
  309. Error(_T("metabase path exceeds 1024 chars"));
  310. }
  311. else
  312. Error("");
  313. return;
  314. }
  315. #else
  316. wcscpy(wszMetabasePath,szPath);
  317. #endif
  318. OpenWamAdm(strComputer);
  319. if (pIWamAdm!=0)
  320. {
  321. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  322. hresError = pIWamAdm->AppUnLoad(wszMetabasePath,FALSE);
  323. if (FAILED(hresError))
  324. {
  325. Error(_T("Unload application failed"));
  326. }
  327. else
  328. {
  329. Print(_T("application unloaded\n"));
  330. }
  331. }
  332. //CloseWamAdm();
  333. return;
  334. }
  335. void CAdmUtil::AppGetStatus(const _TCHAR* szPath,const CString & strComputer)
  336. {
  337. WCHAR wszMetabasePath[SIZE_STRING_BUFFER];
  338. INT cSize = 0;
  339. INT cch = 0;
  340. #ifndef UNICODE
  341. cSize = MultiByteToWideChar(0, 0, szPath, -1, wszMetabasePath, SIZE_STRING_BUFFER);
  342. if (cSize == 0)
  343. {
  344. hresError = GetLastError();
  345. if (hresError == ERROR_INSUFFICIENT_BUFFER)
  346. {
  347. Error(_T("The Metabase path exceeds 1024 chars"));
  348. }
  349. else
  350. Error("");
  351. return;
  352. }
  353. #else
  354. wcscpy(wszMetabasePath,szPath);
  355. #endif
  356. OpenWamAdm(strComputer);
  357. if (pIWamAdm!=0)
  358. {
  359. DWORD dwStatus;
  360. CloseObject_hmd(); //close reusable handle; it may conflict with WAM
  361. hresError = pIWamAdm->AppGetStatus(wszMetabasePath, &dwStatus);
  362. if (FAILED(hresError))
  363. {
  364. Error(_T("GetStatus of application %s failed"));
  365. }
  366. else
  367. {
  368. if (dwStatus == APPSTATUS_NOTDEFINED)
  369. {
  370. Print(_T("Application is not defined\n"));
  371. }
  372. else if (dwStatus == APPSTATUS_STOPPED)
  373. {
  374. Print(_T("Application is stopped\n"));
  375. }
  376. else if (dwStatus == APPSTATUS_RUNNING)
  377. {
  378. Print(_T("Application is running\n"));
  379. }
  380. else
  381. {
  382. Print(_T("Application is in unknown state\n"));
  383. }
  384. }
  385. }
  386. //CloseWamAdm();
  387. return;
  388. }