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.

296 lines
13 KiB

  1. /********************************************************************
  2. Copyright (c) 1999 Microsoft Corporation
  3. Module Name:
  4. PCH_PrinterDriver.CPP
  5. Abstract:
  6. WBEM provider class implementation for PCH_PrinterDriver class
  7. Revision History:
  8. Ghim-Sim Chua (gschua) 04/27/99
  9. - Created
  10. Kalyani Narlanka (kalyanin) 05/11/99
  11. - Added Code to get all the properties for this class
  12. ********************************************************************/
  13. #include "pchealth.h"
  14. #include "PCH_PrinterDriver.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. // tracing stuff
  17. #ifdef THIS_FILE
  18. #undef THIS_FILE
  19. #endif
  20. static char __szTraceSourceFile[] = __FILE__;
  21. #define THIS_FILE __szTraceSourceFile
  22. #define TRACE_ID DCID_PRINTERDRIVER
  23. CPCH_PrinterDriver MyPCH_PrinterDriverSet (PROVIDER_NAME_PCH_PRINTERDRIVER, PCH_NAMESPACE) ;
  24. // Property names
  25. //===============
  26. const static WCHAR* pTimeStamp = L"TimeStamp" ;
  27. const static WCHAR* pChange = L"Change" ;
  28. const static WCHAR* pDate = L"Date" ;
  29. const static WCHAR* pFilename = L"Filename" ;
  30. const static WCHAR* pManufacturer = L"Manufacturer" ;
  31. const static WCHAR* pName = L"Name" ;
  32. const static WCHAR* pSize = L"Size" ;
  33. const static WCHAR* pVersion = L"Version" ;
  34. const static WCHAR* pPath = L"Path" ;
  35. /*****************************************************************************
  36. *
  37. * FUNCTION : CPCH_PrinterDriver::EnumerateInstances
  38. *
  39. * DESCRIPTION : Returns all the instances of this class.
  40. *
  41. * INPUTS : A pointer to the MethodContext for communication with WinMgmt.
  42. * A long that contains the flags described in
  43. * IWbemServices::CreateInstanceEnumAsync. Note that the following
  44. * flags are handled by (and filtered out by) WinMgmt:
  45. * WBEM_FLAG_DEEP
  46. * WBEM_FLAG_SHALLOW
  47. * WBEM_FLAG_RETURN_IMMEDIATELY
  48. * WBEM_FLAG_FORWARD_ONLY
  49. * WBEM_FLAG_BIDIRECTIONAL
  50. *
  51. * RETURNS : WBEM_S_NO_ERROR if successful
  52. *
  53. * COMMENTS : TO DO: All instances on the machine should be returned here.
  54. * If there are no instances, return WBEM_S_NO_ERROR.
  55. * It is not an error to have no instances.
  56. *
  57. *****************************************************************************/
  58. HRESULT CPCH_PrinterDriver::EnumerateInstances(
  59. MethodContext* pMethodContext,
  60. long lFlags
  61. )
  62. {
  63. TraceFunctEnter("CPCH_PrinterDriver::EnumerateInstances");
  64. // Begin Declarations
  65. HRESULT hRes = WBEM_S_NO_ERROR;
  66. // Query String
  67. CComBSTR bstrPrinterQuery = L"Select DeviceID, PortName FROM win32_printer";
  68. // Instances
  69. CComPtr<IEnumWbemClassObject> pPrinterEnumInst;
  70. CInstance *pPCHPrinterDriverInstance;
  71. // SystemTime
  72. SYSTEMTIME stUTCTime;
  73. // Objects
  74. IWbemClassObjectPtr pPrinterObj;
  75. IWbemClassObjectPtr pFileObj;
  76. // Unsigned Longs....
  77. ULONG ulPrinterRetVal = 0;
  78. ULONG uiReturn = 0;
  79. // File Status structure
  80. struct _stat filestat;
  81. // Strings
  82. CComBSTR bstrPrinterDriverWithPath;
  83. CComBSTR bstrPrinterDriver;
  84. CComBSTR bstrProperty;
  85. CComBSTR bstrDeviceID = L"DeviceID";
  86. LPCWSTR lpctstrPortName = L"PortName";
  87. LPCWSTR lpctstrFileSize = L"FileSize";
  88. LPCWSTR lpctstrLastModified = L"LastModified";
  89. LPCWSTR lpctstrManufacturer = L"Manufacturer";
  90. LPCWSTR lpctstrVersion = L"Version";
  91. LPCTSTR lpctstrComma = _T(",");
  92. LPCTSTR lpctstrDrvExtension = _T(".drv");
  93. LPCTSTR lpctstrDevices = _T("Devices");
  94. LPCWSTR lpctstrDeviceID = L"DeviceID";
  95. TCHAR tchDeviceID[MAX_PATH];
  96. TCHAR tchBuffer[MAX_PATH];
  97. TCHAR *ptchToken;
  98. CComVariant varValue;
  99. CComVariant varSnapshot = "Snapshot";
  100. BOOL fDriverFound;
  101. BOOL fCommit = FALSE;
  102. // Get the date and time to update the TimeStamp Field
  103. GetSystemTime(&stUTCTime);
  104. // Execute the query to get DeviceID, PORTName FROM Win32_Printer
  105. // Class.
  106. // pPrinterEnumInst contains a pointer to the list of instances returned.
  107. tchDeviceID[0] = 0;
  108. hRes = ExecWQLQuery(&pPrinterEnumInst, bstrPrinterQuery);
  109. if (FAILED(hRes))
  110. {
  111. // Cannot get any properties.
  112. goto END;
  113. }
  114. // Query Succeeded!
  115. // Enumerate the instances from pPrinterEnumInstance
  116. // Get the next instance into pPrinterObj object.
  117. while(WBEM_S_NO_ERROR == pPrinterEnumInst->Next(WBEM_INFINITE, 1, &pPrinterObj, &ulPrinterRetVal))
  118. {
  119. // Create a new instance of PCH_PrinterDriver Class based on the passed-in MethodContext
  120. CInstancePtr pPCHPrinterDriverInstance(CreateNewInstance(pMethodContext),false);
  121. // Created a New Instance of PCH_PrinterDriver Successfully.
  122. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  123. // TIME STAMP //
  124. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  125. hRes = pPCHPrinterDriverInstance->SetDateTime(pTimeStamp, WBEMTime(stUTCTime));
  126. if (FAILED(hRes))
  127. {
  128. // Could not Set the Time Stamp
  129. // Continue anyway
  130. ErrorTrace(TRACE_ID, "SetDateTime on Timestamp Field failed.");
  131. }
  132. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  133. // CHANGE //
  134. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  135. hRes = pPCHPrinterDriverInstance->SetVariant(pChange, varSnapshot);
  136. if (FAILED(hRes))
  137. {
  138. // Could not Set the Change Property
  139. // Continue anyway
  140. ErrorTrace(TRACE_ID, "SetCHString on Change Field failed.");
  141. }
  142. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  143. // NAME //
  144. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  145. hRes = CopyProperty(pPrinterObj, lpctstrDeviceID, pPCHPrinterDriverInstance, pName);
  146. if(SUCCEEDED(hRes))
  147. {
  148. fCommit = TRUE;
  149. }
  150. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  151. // PATH //
  152. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  153. CopyProperty(pPrinterObj, lpctstrPortName, pPCHPrinterDriverInstance, pPath);
  154. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  155. // FILENAME //
  156. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  157. // Get the File Name i.e.driver from the INI file.
  158. // Use the DeviceID of win32_printer class to get the PCH_Printer.FileName
  159. // Get the device ID and use it to pick up the driver from win.ini File
  160. hRes = pPrinterObj->Get(bstrDeviceID, 0, &varValue, NULL, NULL);
  161. if(SUCCEEDED(hRes))
  162. {
  163. // Got the DeviceID
  164. // Now call GetProfileString to get the Driver
  165. USES_CONVERSION;
  166. _tcscpy(tchDeviceID,W2T(varValue.bstrVal));
  167. if (GetProfileString(lpctstrDevices, tchDeviceID, "\0", tchBuffer, MAX_PATH) > 1)
  168. {
  169. // tchBuffer contains a string of two tokens, first the driver, second the PathName
  170. // Get the driver
  171. ptchToken = _tcstok(tchBuffer,lpctstrComma);
  172. if(ptchToken != NULL)
  173. {
  174. // Got the Driver Name
  175. bstrPrinterDriver = ptchToken;
  176. varValue = ptchToken;
  177. // Use this to set the FileName
  178. hRes = pPCHPrinterDriverInstance->SetVariant(pFilename, varValue);
  179. if (FAILED(hRes))
  180. {
  181. // Could not Set the FileName Property
  182. // Continue anyway
  183. ErrorTrace(TRACE_ID, "Set Variant on Change Field failed.");
  184. }
  185. // Now get the properties of the File
  186. // Concatenate ".drv" to get the driver's actual Name
  187. bstrPrinterDriver.Append(lpctstrDrvExtension);
  188. // Get the Complete Path of the File
  189. fDriverFound = getCompletePath(bstrPrinterDriver, bstrPrinterDriverWithPath);
  190. if (fDriverFound)
  191. {
  192. // Got the complete Path Call GetCIMDataFile Function to get
  193. // properties of this file.
  194. if (SUCCEEDED(GetCIMDataFile(bstrPrinterDriverWithPath, &pFileObj)))
  195. {
  196. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  197. // VERSION //
  198. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  199. CopyProperty(pFileObj, lpctstrVersion, pPCHPrinterDriverInstance, pVersion);
  200. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  201. // FILESIZE //
  202. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  203. CopyProperty(pFileObj, lpctstrFileSize, pPCHPrinterDriverInstance, pSize);
  204. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  205. // DATE //
  206. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  207. CopyProperty(pFileObj, lpctstrLastModified, pPCHPrinterDriverInstance, pDate);
  208. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  209. // MANUFACTURER //
  210. /////////////////////////////////////////////////////////////////////////////////////////////////////////////
  211. CopyProperty(pFileObj, lpctstrManufacturer, pPCHPrinterDriverInstance, pManufacturer);
  212. } //end of SUCCEEDED...
  213. } // end of if fDriverFound
  214. } // end of if (ptchToken != NULL)
  215. } // end of GetProfileString...
  216. }// end of got the DeviceID
  217. // All the properties are set. Commit the instance
  218. hRes = pPCHPrinterDriverInstance->Commit();
  219. if(FAILED(hRes))
  220. {
  221. // Could not Commit the instance
  222. ErrorTrace(TRACE_ID, "Could not commit the instance");
  223. }
  224. } // end of While
  225. END: TraceFunctLeave();
  226. return hRes;
  227. }