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.

267 lines
7.9 KiB

  1. /******************************************************************
  2. PrintJob.CPP -- WMI provider class implementation
  3. Generated by Microsoft WMI Code Generation Engine
  4. TO DO: - See individual function headers
  5. - When linking, make sure you link to framedyd.lib &
  6. msvcrtd.lib (debug) or framedyn.lib & msvcrt.lib (retail).
  7. Description:
  8. ******************************************************************/
  9. #include "pchealth.h"
  10. #include "PrintJob.h"
  11. /////////////////////////////////////////////////////////////////////////////
  12. // tracing stuff
  13. #ifdef THIS_FILE
  14. #undef THIS_FILE
  15. #endif
  16. static char __szTraceSourceFile[] = __FILE__;
  17. #define THIS_FILE __szTraceSourceFile
  18. #define TRACE_ID DCID_PRINTERDRIVER
  19. /////////////////////////////////////////////////////////////////////////////
  20. // initialization
  21. CPrintJob MyPrintJobSet (PROVIDER_NAME_PRINTJOB, PCH_NAMESPACE) ;
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Property names
  24. // PCH
  25. const static WCHAR *c_wszName = L"Name";
  26. const static WCHAR *c_wszPagesPrinted = L"PagesPrinted";
  27. const static WCHAR *c_wszSize = L"Size";
  28. const static WCHAR *c_wszStatus = L"Status";
  29. const static WCHAR *c_wszTimeSubmitted = L"TimeSubmitted";
  30. const static WCHAR *c_wszUser = L"User";
  31. const static WCHAR *c_wszDocument = L"Document";
  32. // WMI
  33. const static WCHAR *c_wszNotify = L"Notify";
  34. const static WCHAR *c_wszJobStatus = L"JobStatus";
  35. //////////////////////////////////////////////////////////////////////////////
  36. // construction / destruction
  37. // ***************************************************************************
  38. CPrintJob::CPrintJob(LPCWSTR lpwszName, LPCWSTR lpwszNameSpace) :
  39. Provider(lpwszName, lpwszNameSpace)
  40. {
  41. }
  42. // ***************************************************************************
  43. CPrintJob::~CPrintJob()
  44. {
  45. }
  46. //////////////////////////////////////////////////////////////////////////////
  47. // internal methods
  48. // ****************************************************************************
  49. HRESULT CPrintJob::GetInstanceData(IWbemClassObjectPtr pObj,
  50. CInstance *pInstance)
  51. {
  52. // ** Name
  53. CopyProperty(pObj, c_wszName, pInstance, c_wszName);
  54. // ** Pages Printed
  55. CopyProperty(pObj, c_wszPagesPrinted, pInstance, c_wszPagesPrinted);
  56. // ** Size
  57. CopyProperty(pObj, c_wszSize, pInstance, c_wszSize);
  58. // ** JobStatus
  59. CopyProperty(pObj, c_wszJobStatus, pInstance, c_wszStatus);
  60. // ** TimeSubmitted
  61. CopyProperty(pObj, c_wszTimeSubmitted, pInstance, c_wszTimeSubmitted);
  62. // ** User
  63. CopyProperty(pObj, c_wszNotify, pInstance, c_wszUser);
  64. // ** Document
  65. CopyProperty(pObj, c_wszDocument, pInstance, c_wszDocument);
  66. return NOERROR;
  67. }
  68. //////////////////////////////////////////////////////////////////////////////
  69. // exposed methods
  70. // ***************************************************************************
  71. HRESULT CPrintJob::EnumerateInstances(MethodContext *pMethodContext, long lFlags)
  72. {
  73. TraceFunctEnter("CPCH_PrintJob::EnumerateInstances");
  74. IEnumWbemClassObject *pEnumInst = NULL;
  75. IWbemClassObjectPtr pObj = NULL;
  76. CComBSTR bstrQuery;
  77. HRESULT hr = WBEM_S_NO_ERROR;
  78. ULONG ulRetVal;
  79. TCHAR wszUser[1024];
  80. DWORD cchUser = sizeof(wszUser) / sizeof(TCHAR);
  81. GetUserName(wszUser, &cchUser);
  82. // Execute the query
  83. bstrQuery = L"select Name, document, Notify, Size, JobStatus, TimeSubmitted, PagesPrinted from Win32_printJob";
  84. hr = ExecWQLQuery(&pEnumInst, bstrQuery);
  85. if (FAILED(hr))
  86. goto done;
  87. // enumerate the instances from Win32_PrintJob
  88. while(pEnumInst->Next(WBEM_INFINITE, 1, &pObj, &ulRetVal) == WBEM_S_NO_ERROR)
  89. {
  90. // Create a new instance based on the passed-in MethodContext
  91. CInstancePtr pInstance(CreateNewInstance(pMethodContext), FALSE);
  92. // since the old code didn't care if this failed, neither do I.
  93. hr = GetInstanceData(pObj, pInstance);
  94. // commit instance
  95. hr = pInstance->Commit();
  96. if (FAILED(hr))
  97. ErrorTrace(TRACE_ID, "Error committing instance");
  98. // Ok, so WMI does not follow it's own docs on how GetObject
  99. // works. According to them, we should release this object here. But
  100. // if I try, winmgmt GPFs.
  101. // pObj->Release();
  102. pObj = NULL;
  103. }
  104. done:
  105. if (pEnumInst != NULL)
  106. pEnumInst->Release();
  107. TraceFunctLeave();
  108. return hr;
  109. }
  110. // *****************************************************************************
  111. HRESULT CPrintJob::ExecMethod(const CInstance& Instance,
  112. const BSTR bstrMethodName,
  113. CInstance *pInParams, CInstance *pOutParams,
  114. long lFlags)
  115. {
  116. return (WBEM_E_PROVIDER_NOT_CAPABLE);
  117. }
  118. // *****************************************************************************
  119. HRESULT CPrintJob::GetObject(CInstance* pInstance, long lFlags)
  120. {
  121. TraceFunctEnter("CPrintJob::GetObject");
  122. IWbemClassObjectPtr pObj = NULL;
  123. CComBSTR bstrPath;
  124. HRESULT hr = NOERROR;
  125. VARIANT var;
  126. TCHAR szDefault[MAX_PATH];
  127. TCHAR *pchToken, *szDefName = NULL;
  128. DWORD i;
  129. VariantInit(&var);
  130. if (pInstance == NULL)
  131. {
  132. hr = E_INVALIDARG;
  133. goto done;
  134. }
  135. // get the name of the printer
  136. if (pInstance->GetVariant(c_wszName, var) == FALSE)
  137. {
  138. ErrorTrace(TRACE_ID, "Unable to fetch printer name");
  139. hr = E_FAIL;
  140. goto done;
  141. }
  142. if (V_VT(&var) != VT_BSTR)
  143. {
  144. hr = VariantChangeType(&var, &var, 0, VT_BSTR);
  145. if (FAILED(hr))
  146. {
  147. ErrorTrace(TRACE_ID, "VariantChangeType failed: 0x%08x", hr);
  148. goto done;
  149. }
  150. }
  151. // get the default printer & path
  152. if(GetProfileString(_T("Windows"), _T("Device"), "\0", szDefault, MAX_PATH) > 1)
  153. {
  154. // The Above GetProfileString returns "printerName", "PrinterDriver"
  155. // and "PrinterPath" seperated by commas. Ignore "PrinterDriver"
  156. // and use the other two to set the properties.
  157. pchToken = _tcstok(szDefault, _T(","));
  158. if(pchToken != NULL)
  159. {
  160. // ** default name
  161. szDefName = pchToken;
  162. }
  163. }
  164. // build the path to the object
  165. bstrPath = L"\\\\.\\root\\cimv2:Win32_PrintJob.Name=\"";
  166. bstrPath.Append(V_BSTR(&var));
  167. bstrPath.Append("\"");
  168. // fetch it
  169. hr = GetCIMObj(bstrPath, &pObj, lFlags);
  170. if (FAILED(hr))
  171. goto done;
  172. // populate the CInstance object
  173. hr = GetInstanceData(pObj, pInstance);
  174. if (FAILED(hr))
  175. goto done;
  176. // All the properties are set. Commit the instance
  177. hr = pInstance->Commit();
  178. if(FAILED(hr))
  179. ErrorTrace(TRACE_ID, "Could not commit instance: 0x%08x", hr);
  180. done:
  181. VariantClear(&var);
  182. // Ok, so WMI does not follow it's own docs on how GetObject
  183. // works. According to them, we should release this object here. But
  184. // if I try, winmgmt GPFs.
  185. // if (pObj != NULL)
  186. // pObj->Release();
  187. TraceFunctLeave();
  188. return hr;
  189. }
  190. // *****************************************************************************
  191. HRESULT CPrintJob::ExecQuery(MethodContext *pMethodContext,
  192. CFrameworkQuery& Query, long lFlags)
  193. {
  194. return WBEM_E_PROVIDER_NOT_CAPABLE;
  195. }
  196. // *****************************************************************************
  197. HRESULT CPrintJob::PutInstance(const CInstance& Instance, long lFlags)
  198. {
  199. return WBEM_E_PROVIDER_NOT_CAPABLE;
  200. }
  201. // *****************************************************************************
  202. HRESULT CPrintJob::DeleteInstance(const CInstance& Instance, long lFlags)
  203. {
  204. return WBEM_E_PROVIDER_NOT_CAPABLE;
  205. }