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.

276 lines
5.9 KiB

  1. /*++
  2. Copyright (C) Microsoft Corporation, 1996 - 1998
  3. All rights reserved.
  4. Module Name:
  5. query.hxx
  6. Abstract:
  7. Printer DS query
  8. Author:
  9. Steve Kiraly (SteveKi) 09-Dec-1996
  10. Revision History:
  11. --*/
  12. #include "precomp.hxx"
  13. #pragma hdrstop
  14. #include "dsinterf.hxx"
  15. #include "query.hxx"
  16. #include "persist.hxx"
  17. TQuery::
  18. TQuery(
  19. IN HWND hDlg
  20. ) : _bValid( FALSE ),
  21. _hDlg( hDlg ),
  22. _cItems( 0 ),
  23. _pICommonQuery( NULL ),
  24. _pItems( NULL )
  25. {
  26. //
  27. // We will be using OLE.
  28. //
  29. CoInitialize( NULL );
  30. //
  31. // Validate the ds interface object.
  32. //
  33. if( !VALID_OBJ( _Ds ) )
  34. {
  35. return;
  36. }
  37. //
  38. // Get a pointer to the common query interface.
  39. //
  40. HRESULT hr = CoCreateInstance( CLSID_CommonQuery,
  41. 0,
  42. CLSCTX_INPROC_SERVER,
  43. IID_ICommonQuery,
  44. (VOID**)&_pICommonQuery );
  45. if( FAILED( hr ) )
  46. {
  47. return;
  48. }
  49. //
  50. // Read the default scope from the policy key. It does not constitue a
  51. // failure if the key was not found.
  52. //
  53. TPersist PersistPolicy( gszAddPrinterWizardPolicy, TPersist::kOpen|TPersist::kRead );
  54. if( VALID_OBJ( PersistPolicy ) )
  55. {
  56. (VOID)PersistPolicy.bRead( gszAPWDefaultSearchScope, _strDefaultScope );
  57. }
  58. _bValid = TRUE;
  59. }
  60. TQuery::
  61. ~TQuery(
  62. VOID
  63. )
  64. {
  65. //
  66. // Release the qurey item.
  67. //
  68. vReleaseItems();
  69. //
  70. // Release the querey interface.
  71. //
  72. if( _pICommonQuery )
  73. {
  74. _pICommonQuery->Release();
  75. }
  76. //
  77. // Balance the CoInitalize call.
  78. //
  79. CoUninitialize();
  80. }
  81. BOOL
  82. TQuery::
  83. bValid(
  84. VOID
  85. )
  86. {
  87. return _bValid;
  88. }
  89. VOID
  90. TQuery::
  91. vReleaseItems(
  92. VOID
  93. )
  94. {
  95. //
  96. // Release the qurey item.
  97. //
  98. _cItems = 0;
  99. delete [] _pItems;
  100. }
  101. BOOL
  102. TQuery::
  103. bSetDefaultScope(
  104. IN LPCTSTR pszDefaultScope
  105. )
  106. {
  107. return _strDefaultScope.bUpdate( pszDefaultScope);
  108. }
  109. BOOL
  110. TQuery::
  111. bDoQuery(
  112. VOID
  113. )
  114. {
  115. static UINT g_cfDsObjectNames;
  116. TStatusB bStatus;
  117. bStatus DBGNOCHK = FALSE;
  118. DSQUERYINITPARAMS dqip = {0};
  119. OPENQUERYWINDOW oqw = {0};
  120. LPDSOBJECTNAMES pDsObjects = NULL;
  121. IDataObject *pDataObject = NULL;
  122. FORMATETC fmte = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
  123. STGMEDIUM medium = { TYMED_NULL, NULL, NULL };
  124. //
  125. // Build the querey parameters.
  126. //
  127. dqip.cbStruct = sizeof(dqip);
  128. dqip.dwFlags = 0;
  129. dqip.pDefaultScope = ( _strDefaultScope.bEmpty() == TRUE ) ? NULL : (LPTSTR)(LPCTSTR)_strDefaultScope;
  130. DBGMSG( DBG_TRACE, ( "TQuery::bDoQuery - Default Search Scope: " TSTR "\n", (LPCTSTR)_strDefaultScope ) );
  131. //
  132. // Build the open querey window struct.
  133. //
  134. oqw.cbStruct = sizeof( oqw );
  135. oqw.dwFlags = OQWF_OKCANCEL|OQWF_DEFAULTFORM|OQWF_SINGLESELECT|OQWF_REMOVEFORMS;
  136. oqw.clsidHandler = CLSID_DsQuery;
  137. oqw.pHandlerParameters = &dqip;
  138. oqw.clsidDefaultForm = CLSID_DsFindPrinter;
  139. //
  140. // Open the query window.
  141. //
  142. HRESULT hr = _pICommonQuery->OpenQueryWindow( _hDlg, &oqw, &pDataObject);
  143. //
  144. // Release any stale items.
  145. //
  146. vReleaseItems();
  147. //
  148. // If that worked and we received a IDataObjec The DSQUERY
  149. // code exposes the CF_DSOBJECTNAMES
  150. // which gives us the full ADsPath and objectClass.
  151. //
  152. if( SUCCEEDED(hr) && pDataObject )
  153. {
  154. if ( !g_cfDsObjectNames )
  155. {
  156. g_cfDsObjectNames = RegisterClipboardFormat(CFSTR_DSOBJECTNAMES);
  157. }
  158. fmte.cfFormat = static_cast<USHORT>( g_cfDsObjectNames );
  159. if( SUCCEEDED(pDataObject->GetData(&fmte, &medium)) )
  160. {
  161. pDsObjects = (LPDSOBJECTNAMES)medium.hGlobal;
  162. //
  163. // If selected items were returned then save the selected items in
  164. // an array of items.
  165. //
  166. if( pDsObjects->cItems )
  167. {
  168. //
  169. // Allocate the new items.
  170. //
  171. _cItems = pDsObjects->cItems;
  172. _pItems = new TQuery::TItem[_cItems];
  173. if( _pItems )
  174. {
  175. for( UINT i = 0 ; i < pDsObjects->cItems ; i++ )
  176. {
  177. bStatus DBGCHK = _pItems[i].strName().bUpdate( ByteOffset( pDsObjects, pDsObjects->aObjects[i].offsetName ) );
  178. bStatus DBGCHK = _pItems[i].strClass().bUpdate( ByteOffset( pDsObjects, pDsObjects->aObjects[i].offsetClass ) );
  179. DBGMSG( DBG_TRACE, ( " Name " TSTR " Class " TSTR "\n", (LPCTSTR)_pItems[i].strName(), (LPCTSTR)_pItems[i].strClass() ) );
  180. }
  181. }
  182. }
  183. ReleaseStgMedium(&medium);
  184. }
  185. pDataObject->Release();
  186. }
  187. return bStatus;
  188. }
  189. BOOL
  190. TQuery::
  191. bPrinterName(
  192. IN TString &strPrinterName,
  193. IN const TString &strDsName
  194. )
  195. {
  196. IADs *pDsObject = NULL;
  197. HRESULT hr = E_FAIL;
  198. VARIANT Variant;
  199. TStatusB bStatus;
  200. bStatus DBGNOCHK = FALSE;
  201. #ifdef UNICODE
  202. VariantClear( &Variant );
  203. hr = _Ds.ADsGetObject( const_cast<LPTSTR>( static_cast<LPCTSTR>( strDsName ) ), IID_IADs, (LPVOID*)&pDsObject);
  204. if( SUCCEEDED(hr))
  205. {
  206. hr = pDsObject->Get( TEXT( "uNCName" ), &Variant );
  207. if( SUCCEEDED(hr))
  208. {
  209. if ( Variant.vt == VT_BSTR && Variant.bstrVal && Variant.bstrVal[0] )
  210. {
  211. bStatus DBGCHK = strPrinterName.bUpdate( Variant.bstrVal );
  212. }
  213. VariantClear( &Variant );
  214. }
  215. pDsObject->Release();
  216. }
  217. #endif
  218. if( FAILED(hr) )
  219. {
  220. SetLastError( ERROR_INVALID_PRINTER_NAME );
  221. }
  222. return bStatus;
  223. }