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.

258 lines
6.0 KiB

  1. //----------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 1996
  5. //
  6. // File: get.cxx
  7. //
  8. // Contents: Get Object
  9. //
  10. // History: 04-23-96 KrishnaG created
  11. // 08-01-96 t-danal add to oledscmd w/getrobj
  12. //
  13. //----------------------------------------------------------------------------
  14. #include "main.hxx"
  15. #include "macro.hxx"
  16. #include "sconv.hxx"
  17. //
  18. // Local functions
  19. //
  20. HRESULT
  21. GetObject(
  22. LPWSTR szLocation
  23. );
  24. HRESULT
  25. GetRelativeObject(
  26. LPWSTR szLocation,
  27. LPWSTR szClass,
  28. LPWSTR szName
  29. );
  30. //
  31. // Local function definitions
  32. //
  33. HRESULT
  34. GetObject(
  35. LPWSTR szPath
  36. )
  37. {
  38. HRESULT hr, hr_return;
  39. IADs * pADs = NULL;
  40. DWORD dwLastError;
  41. WCHAR szErrorBuf[MAX_PATH];
  42. WCHAR szNameBuf[MAX_PATH];
  43. hr = ADsGetObject(
  44. szPath,
  45. IID_IADs,
  46. (void **)&pADs
  47. );
  48. BAIL_ON_FAILURE(hr);
  49. error:
  50. if(HRESULT_FROM_WIN32(ERROR_EXTENDED_ERROR)== hr){
  51. //
  52. // get extended error value
  53. //
  54. hr_return = ADsGetLastError( &dwLastError,
  55. szErrorBuf,
  56. MAX_PATH-1,
  57. szNameBuf,
  58. MAX_PATH -1 );
  59. if (SUCCEEDED(hr_return)){
  60. printf("%d:%ws in provider %ws\n", dwLastError, szErrorBuf, szNameBuf);
  61. }
  62. }
  63. if (SUCCEEDED(hr)) {
  64. printf("getobj: Successfully bound to object %ws\n",
  65. szPath);
  66. }else {
  67. printf("getobj: Failed to bind to the object with error code %.8x\n",
  68. hr);
  69. }
  70. if (pADs){
  71. pADs->Release();
  72. }
  73. return(hr);
  74. }
  75. HRESULT
  76. GetRelativeObject(
  77. LPWSTR szLocation,
  78. LPWSTR szClass,
  79. LPWSTR szName
  80. )
  81. {
  82. HRESULT hr;
  83. IADsContainer * pADsContainer = NULL;
  84. IDispatch * pDispatch = NULL;
  85. hr = ADsGetObject(
  86. szLocation,
  87. IID_IADsContainer,
  88. (void **)&pADsContainer
  89. );
  90. BAIL_ON_FAILURE(hr);
  91. hr = pADsContainer->GetObject(
  92. szClass,
  93. szName,
  94. &pDispatch
  95. );
  96. BAIL_ON_FAILURE(hr);
  97. error:
  98. if (SUCCEEDED(hr)) {
  99. printf("getrobj: Successfully bound to relative object "
  100. "%ws from container object %ws\n",
  101. szName, szLocation);
  102. }
  103. if (pDispatch) {
  104. pDispatch->Release();
  105. }
  106. if (pADsContainer) {
  107. pADsContainer->Release();
  108. }
  109. return(hr);
  110. }
  111. HRESULT
  112. GetTransientObjects(
  113. LPWSTR szContainer,
  114. LPWSTR szType,
  115. IADs **ppADs,
  116. IADsCollection **ppCollection
  117. )
  118. {
  119. HRESULT hr;
  120. IADs * pADs = NULL;
  121. IADsCollection * pCollection = NULL;
  122. IADsPrintQueueOperations * pPrintQueueOperation = NULL;
  123. IADsFileServiceOperations * pFileServiceOperation = NULL;
  124. if (ppADs)
  125. *ppADs = NULL;
  126. if (ppCollection)
  127. *ppCollection = NULL;
  128. hr = ADsGetObject(
  129. szContainer,
  130. IID_IADs,
  131. (void **)&pADs
  132. );
  133. BAIL_ON_FAILURE(hr);
  134. //
  135. // printf("ADs Get objects succeeded \n");
  136. //
  137. if (_wcsicmp(szType, L"Job") == 0){
  138. hr = pADs->QueryInterface(IID_IADsPrintQueueOperations,
  139. (void **)&pPrintQueueOperation );
  140. BAIL_ON_FAILURE(hr);
  141. hr = pPrintQueueOperation->PrintJobs(&pCollection);
  142. BAIL_ON_FAILURE(hr);
  143. pPrintQueueOperation -> Release();
  144. pPrintQueueOperation = NULL;
  145. } else if (_wcsicmp(szType, L"Session") == 0){
  146. hr = pADs->QueryInterface(IID_IADsFileServiceOperations,
  147. (void **)&pFileServiceOperation );
  148. BAIL_ON_FAILURE(hr);
  149. hr = pFileServiceOperation->Sessions(&pCollection);
  150. BAIL_ON_FAILURE(hr);
  151. pFileServiceOperation -> Release();
  152. pFileServiceOperation = NULL;
  153. } else if (_wcsicmp(szType, L"Resource") == 0){
  154. hr = pADs->QueryInterface(IID_IADsFileServiceOperations,
  155. (void **)&pFileServiceOperation );
  156. BAIL_ON_FAILURE(hr);
  157. hr = pFileServiceOperation->Resources(&pCollection);
  158. BAIL_ON_FAILURE(hr);
  159. pFileServiceOperation -> Release();
  160. pFileServiceOperation = NULL;
  161. } else {
  162. hr = E_FAIL;
  163. BAIL_ON_FAILURE(hr);
  164. }
  165. if (ppADs)
  166. *ppADs = pADs;
  167. if (ppCollection)
  168. *ppCollection = pCollection;
  169. return S_OK;
  170. error:
  171. if (pADs)
  172. pADs->Release();
  173. if (pPrintQueueOperation)
  174. pPrintQueueOperation->Release();
  175. if (pFileServiceOperation)
  176. pFileServiceOperation->Release();
  177. return hr;
  178. }
  179. //
  180. // Exec function definitions
  181. //
  182. int
  183. ExecGet(char *szProgName, char *szAction, int argc, char * argv[])
  184. {
  185. HRESULT hr;
  186. LPWSTR pszContainer = NULL;
  187. LPWSTR pszClass = NULL;
  188. LPWSTR pszName = NULL;
  189. switch (argc) {
  190. case 1:
  191. ALLOC_UNICODE_WITH_BAIL_ON_NULL(pszName, argv[0]);
  192. hr = GetObject(pszName);
  193. break;
  194. case 2:
  195. ALLOC_UNICODE_WITH_BAIL_ON_NULL(pszContainer, argv[0]);
  196. ALLOC_UNICODE_WITH_BAIL_ON_NULL(pszName, argv[1]);
  197. hr = GetRelativeObject(pszContainer, pszClass, pszName);
  198. break;
  199. case 3:
  200. ALLOC_UNICODE_WITH_BAIL_ON_NULL(pszContainer, argv[0]);
  201. ALLOC_UNICODE_WITH_BAIL_ON_NULL(pszClass, argv[1]);
  202. ALLOC_UNICODE_WITH_BAIL_ON_NULL(pszName, argv[2]);
  203. hr = GetRelativeObject(pszContainer, pszClass, pszName);
  204. break;
  205. default:
  206. PrintUsage(szProgName, szAction,
  207. "\n\t[ <ADsPath of Object> |"
  208. "\n\t <Container> <Object> |"
  209. "\n\t <Container> <Class> <Object> ]");
  210. return(1);
  211. }
  212. error:
  213. FreeUnicodeString(pszContainer);
  214. FreeUnicodeString(pszClass);
  215. FreeUnicodeString(pszName);
  216. if (FAILED(hr))
  217. return(1);
  218. return(0) ;
  219. }