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.

481 lines
16 KiB

  1. /******************************************************************
  2. PrintSys.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 "PrintSys.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. CPrintSys MyPrintSysSet (PROVIDER_NAME_PRINTSYS, PCH_NAMESPACE) ;
  22. /////////////////////////////////////////////////////////////////////////////
  23. // Property names
  24. const static WCHAR *c_wszGenDrv = L"GenDrv";
  25. const static WCHAR *c_wszName = L"Name";
  26. const static WCHAR *c_wszPath = L"Path";
  27. const static WCHAR *c_wszUniDrv = L"UniDrv";
  28. const static WCHAR *c_wszUsePrintMgrSpooling = L"UsePrintMgrSpooling";
  29. //////////////////////////////////////////////////////////////////////////////
  30. // construction / destruction
  31. // ***************************************************************************
  32. CPrintSys::CPrintSys (LPCWSTR lpwszName, LPCWSTR lpwszNameSpace ) :
  33. Provider(lpwszName, lpwszNameSpace)
  34. {
  35. m_pParamOut = NULL;
  36. m_pCurrent = NULL;
  37. m_pParamIn = NULL;
  38. m_lFlags = 0;
  39. }
  40. // ***************************************************************************
  41. CPrintSys::~CPrintSys()
  42. {
  43. }
  44. //////////////////////////////////////////////////////////////////////////////
  45. // exposed methods
  46. // *****************************************************************************
  47. HRESULT CPrintSys::EnumerateInstances(MethodContext *pMethodContext, long lFlags)
  48. {
  49. TraceFunctEnter("CPrintSys::EnumerateInstances");
  50. HRESULT hr = WBEM_S_NO_ERROR;
  51. // Create a new instance of PCH_Printer Class based on the passed-in MethodContext
  52. CInstancePtr pPrintSysInst(CreateNewInstance(pMethodContext), false);
  53. hr = this->GetObject(pPrintSysInst, 0);
  54. TraceFunctLeave();
  55. return hr;
  56. }
  57. // *****************************************************************************
  58. HRESULT CPrintSys::ExecMethod (const CInstance& Instance,
  59. const BSTR bstrMethodName,
  60. CInstance *pInParams, CInstance *pOutParams,
  61. long lFlags)
  62. {
  63. return WBEM_E_PROVIDER_NOT_CAPABLE;
  64. }
  65. // *****************************************************************************
  66. HRESULT CPrintSys::GetObject(CInstance* pInstance, long lFlags)
  67. {
  68. TraceFunctEnter("CPrintSys::GetObject");
  69. HRESULT hr = WBEM_S_NO_ERROR;
  70. // Objects
  71. IWbemClassObjectPtr pFileObj = NULL;
  72. // Variants
  73. CComVariant varValue;
  74. CComVariant varNotAvail = L"Not Available";
  75. // Strings
  76. CComBSTR bstrDriverWithPath;
  77. CComBSTR bstrDetails;
  78. CComBSTR bstrVersion = L"Version";
  79. CComBSTR bstrFileSize = L"FileSize";
  80. CComBSTR bstrModifiedDate = L"LastModified";
  81. LPCTSTR lpctstrUniDriver = _T("unidrv.dll");
  82. LPCTSTR lpctstrGenDriver = _T("gendrv.dll");
  83. LPCTSTR lpctstrWindows = _T("Windows");
  84. LPCTSTR lpctstrDevice = _T("Device");
  85. LPCTSTR lpctstrNoUniDrv = _T("(unidrv.dll) = NotInstalled");
  86. LPCTSTR lpctstrNoGenDrv = _T("(gendrv.dll) = NotInstalled");
  87. LPCTSTR lpctstrPrintersHive = _T("System\\CurrentControlSet\\Control\\Print\\Printers");
  88. LPCTSTR lpctstrYes = _T("yes");
  89. LPCTSTR lpctstrAttributes = _T("Attributes");
  90. LPCTSTR lpctstrSpooler = _T("Spooler");
  91. TCHAR tchBuffer[MAX_PATH + 1];
  92. TCHAR tchPrinterKeyName[MAX_PATH + 1];
  93. TCHAR *ptchToken;
  94. // Booleans
  95. BOOL fDriverFound = FALSE;
  96. BOOL fAttribFound = FALSE;
  97. // DWORDs
  98. DWORD dwSize;
  99. DWORD dwIndex;
  100. DWORD dwType;
  101. // Return Values;
  102. ULONG ulPrinterAttribs;
  103. LONG lRegRetVal;
  104. struct tm tm;
  105. WBEMTime wbemtime;
  106. HKEY hkeyPrinter = NULL;
  107. HKEY hkeyPrinters = NULL;
  108. FILETIME ft;
  109. // *** Set the properties associated with the default printer
  110. // In "win.ini" file under "Windows" section "Device" represents the default printer
  111. if(GetProfileString(lpctstrWindows, lpctstrDevice, "\0", tchBuffer, MAX_PATH) > 1)
  112. {
  113. // The Above GetProfileString returns "printerName", "PrinterDriver"
  114. // and "PrinterPath" seperated by commas. Ignore "PrinterDriver"
  115. // and use the other two to set the properties.
  116. ptchToken = _tcstok(tchBuffer, _T(","));
  117. if(ptchToken != NULL)
  118. {
  119. // ** name (token 1)
  120. varValue = ptchToken;
  121. if (pInstance->SetVariant(c_wszName, varValue) == FALSE)
  122. ErrorTrace(TRACE_ID, "SetVariant on Name failed.");
  123. // ** path (token 3)
  124. ptchToken = _tcstok(NULL, _T(","));
  125. if(ptchToken != NULL)
  126. {
  127. // gotta skip the 2nd token cuz it's the printer dirver & we
  128. // don't give a rat's patoshki about it at this moment...
  129. ptchToken = _tcstok(NULL, _T(","));
  130. if(ptchToken != NULL)
  131. {
  132. varValue = ptchToken;
  133. if (pInstance->SetVariant(c_wszPath, varValue) == FALSE)
  134. ErrorTrace(TRACE_ID, "Set Variant on Path failed.");
  135. }
  136. }
  137. }
  138. }
  139. // couldn't fetch the properties of the default printer, so shove in some
  140. // default values...
  141. else
  142. {
  143. // set Name to "Not Available"
  144. if (pInstance->SetVariant(c_wszName, varNotAvail) == FALSE)
  145. ErrorTrace(TRACE_ID, "Se Variant on Name failed.");
  146. // set Path to "Not Available"
  147. if (pInstance->SetVariant(c_wszPath, varValue) == FALSE)
  148. ErrorTrace(TRACE_ID, "Set Variant on Path failed.");
  149. }
  150. // *** Set the properties associated with using print manager spooling
  151. // First try to get the Spooling information from the registry which is
  152. // available if there are any installed printers
  153. // HKLM\system\CCS\Control\Print\Printers
  154. lRegRetVal = RegOpenKeyEx(HKEY_LOCAL_MACHINE, lpctstrPrintersHive, 0, KEY_READ, &hkeyPrinters);
  155. if(lRegRetVal == ERROR_SUCCESS)
  156. {
  157. // Enumerate the keys under this hive.
  158. ZeroMemory(&ft, sizeof(ft));
  159. dwIndex = 0;
  160. dwSize = MAX_PATH;
  161. lRegRetVal = RegEnumKeyEx(hkeyPrinters, dwIndex, tchPrinterKeyName, &dwSize, NULL, NULL, NULL, &ft);
  162. if(lRegRetVal == ERROR_SUCCESS)
  163. {
  164. // There is atleast one printer installed.
  165. lRegRetVal = RegOpenKeyEx(hkeyPrinters, tchPrinterKeyName, 0, KEY_READ, &hkeyPrinter);
  166. if(lRegRetVal == ERROR_SUCCESS)
  167. {
  168. // Opened the first printer key
  169. // Query for , regname "Attributes"
  170. dwSize = sizeof(DWORD);
  171. lRegRetVal = RegQueryValueEx(hkeyPrinter, lpctstrAttributes, NULL, &dwType, (LPBYTE)&ulPrinterAttribs, &dwSize);
  172. if(lRegRetVal == ERROR_SUCCESS)
  173. {
  174. // if the PRINTER_ATTRIBUTE_DIRECT bit in ulPrinterAttribs
  175. // is set, then we have spooling...
  176. if((ulPrinterAttribs & PRINTER_ATTRIBUTE_DIRECT) != 0)
  177. varValue = VARIANT_FALSE;
  178. else
  179. varValue = VARIANT_TRUE;
  180. if (ulPrinterAttribs > 0)
  181. fAttribFound = TRUE;
  182. }
  183. }
  184. }
  185. }
  186. if (hkeyPrinter != NULL)
  187. {
  188. RegCloseKey(hkeyPrinter);
  189. hkeyPrinter = NULL;
  190. }
  191. if (hkeyPrinters != NULL)
  192. {
  193. RegCloseKey(hkeyPrinters);
  194. hkeyPrinters = NULL;
  195. }
  196. if(fAttribFound == FALSE)
  197. {
  198. // If not get the "spooler" key value from the win.ini file. If the entry is not present default to "yes".
  199. if(GetProfileString(lpctstrWindows, lpctstrSpooler, _T("yes"), tchBuffer, MAX_PATH) > 1)
  200. {
  201. // if it's yes, then we have spooling...
  202. if(_tcsicmp(tchBuffer, lpctstrYes) == 0)
  203. varValue = VARIANT_TRUE;
  204. else
  205. varValue = VARIANT_FALSE;
  206. }
  207. }
  208. // Set the Spooling Property.
  209. if (pInstance->SetVariant(c_wszUsePrintMgrSpooling, varValue) == FALSE)
  210. ErrorTrace(TRACE_ID, "SetVariant on usePrintManagerSpooling failed.");
  211. // *** Set the properties associated with using print manager spooling
  212. // Get the complete path for unidrv.dll
  213. fDriverFound = getCompletePath(lpctstrUniDriver, bstrDriverWithPath);
  214. if(fDriverFound)
  215. {
  216. // Need to use GetCIMDataFile to get the unidriver properties
  217. if (SUCCEEDED(GetCIMDataFile(bstrDriverWithPath, &pFileObj)))
  218. {
  219. bstrDetails.Empty();
  220. // Get the Version & append it to the value string...
  221. varValue.Clear();
  222. hr = pFileObj->Get(bstrVersion, 0, &varValue, NULL, NULL);
  223. if(SUCCEEDED(hr))
  224. {
  225. if(varValue.vt == VT_BSTR)
  226. {
  227. bstrDetails.Append(varValue.bstrVal);
  228. bstrDetails.Append(_T(" "));
  229. }
  230. }
  231. // Get the FileSize & append it to the value string...
  232. varValue.Clear();
  233. hr = pFileObj->Get(bstrFileSize, 0, &varValue, NULL, NULL);
  234. if(SUCCEEDED(hr))
  235. {
  236. if(varValue.vt == VT_BSTR)
  237. {
  238. bstrDetails.Append(varValue.bstrVal);
  239. bstrDetails.Append(_T(" "));
  240. }
  241. }
  242. // Get the date & time & append them to the value string...
  243. varValue.Clear();
  244. hr = pFileObj->Get(bstrModifiedDate, 0, &varValue, NULL, NULL);
  245. if(SUCCEEDED(hr))
  246. {
  247. if(varValue.vt == VT_BSTR)
  248. {
  249. WCHAR *pwsz;
  250. // there is a slight problem when WMI returns to us a
  251. // time that has a '*' in it. The WBEMTime class plain
  252. // refuses to deal with it. So change '*'s to '0's...
  253. for (pwsz = varValue.bstrVal; *pwsz != L'\0'; pwsz++)
  254. {
  255. if (*pwsz == L'*')
  256. *pwsz = L'0';
  257. }
  258. wbemtime = varValue.bstrVal;
  259. if(wbemtime.GetStructtm(&tm))
  260. {
  261. varValue = asctime(&tm);
  262. bstrDetails.Append(varValue.bstrVal);
  263. }
  264. }
  265. }
  266. // set the value
  267. varValue.vt = VT_BSTR;
  268. varValue.bstrVal = bstrDetails.Detach();
  269. }
  270. // Since I don't seem to have unindrv.dll installed, I can't verify
  271. // this, but going by the other WMI providers, GetObject doesn't seem
  272. // to like it when u release objects fetched by it. So, I am not
  273. // going to release it.
  274. if (pFileObj != NULL)
  275. {
  276. // pFileObj->Release();
  277. pFileObj = NULL;
  278. }
  279. }
  280. // the unidriver dll isn't present. Use a default value
  281. else
  282. {
  283. varValue.Clear();
  284. varValue = lpctstrNoUniDrv;
  285. }
  286. // set the property
  287. if (pInstance->SetVariant(c_wszUniDrv, varValue) == FALSE)
  288. ErrorTrace(TRACE_ID, "SetVariant on UniDriver failed.");
  289. // *** Set the properties associated with using print manager spooling
  290. // Get the complete path for gendrv.dll
  291. bstrDriverWithPath.Empty();
  292. fDriverFound = getCompletePath(lpctstrGenDriver, bstrDriverWithPath);
  293. if(fDriverFound)
  294. {
  295. bstrDetails.Empty();
  296. // Need to use GetCIMDataFile to get the gen driver properties
  297. if(SUCCEEDED(GetCIMDataFile(bstrDriverWithPath, &pFileObj)))
  298. {
  299. // Get the Version & append it to the value string...
  300. varValue.Clear();
  301. hr = pFileObj->Get(bstrVersion, 0, &varValue, NULL, NULL);
  302. if(SUCCEEDED(hr))
  303. {
  304. if(varValue.vt == VT_BSTR)
  305. {
  306. bstrDetails.Append(varValue.bstrVal);
  307. bstrDetails.Append(_T(" "));
  308. }
  309. }
  310. // Get the FileSize & append it to the value string...
  311. varValue.Clear();
  312. hr = pFileObj->Get(bstrFileSize, 0, &varValue, NULL, NULL);
  313. if(SUCCEEDED(hr))
  314. {
  315. if(varValue.vt == VT_BSTR)
  316. {
  317. bstrDetails.Append(varValue.bstrVal);
  318. bstrDetails.Append(_T(" "));
  319. }
  320. }
  321. // Get the date & time & append them to the value string...
  322. varValue.Clear();
  323. hr = pFileObj->Get(bstrModifiedDate, 0, &varValue, NULL, NULL);
  324. if(SUCCEEDED(hr))
  325. {
  326. if(varValue.vt == VT_BSTR)
  327. {
  328. WCHAR *pwsz;
  329. // there is a slight problem when WMI returns to us a
  330. // time that has a '*' in it. The WBEMTime class plain
  331. // refuses to deal with it. So change '*'s to '0's...
  332. for (pwsz = varValue.bstrVal; *pwsz != L'\0'; pwsz++)
  333. {
  334. if (*pwsz == L'*')
  335. *pwsz = L'0';
  336. }
  337. wbemtime = varValue.bstrVal;
  338. if(wbemtime.GetStructtm(&tm))
  339. {
  340. varValue = asctime(&tm);
  341. bstrDetails.Append(varValue.bstrVal);
  342. }
  343. }
  344. }
  345. // set the value
  346. varValue.vt = VT_BSTR;
  347. varValue.bstrVal = bstrDetails.Detach();
  348. }
  349. // Since I don't seem to have gendrv.dll installed, I can't verify
  350. // this, but going by the other WMI providers, GetObject doesn't seem
  351. // to like it when u release objects fetched by it. So, I am not
  352. // going to release it.
  353. if (pFileObj != NULL)
  354. {
  355. // pFileObj->Release();
  356. pFileObj = NULL;
  357. }
  358. }
  359. // the gen driver dll is not present...
  360. else
  361. {
  362. varValue.Clear();
  363. varValue = lpctstrNoGenDrv;
  364. }
  365. if (pInstance->SetVariant(c_wszGenDrv, varValue) == FALSE)
  366. ErrorTrace(TRACE_ID, "SetVariant on GenDrv failed.");
  367. // WOOHOO!! We can commit now
  368. hr = pInstance->Commit();
  369. if(FAILED(hr))
  370. ErrorTrace(TRACE_ID, "Error on commiting!");
  371. TraceFunctLeave();
  372. return hr;
  373. }
  374. // *****************************************************************************
  375. HRESULT CPrintSys::ExecQuery(MethodContext *pMethodContext,
  376. CFrameworkQuery& Query, long lFlags)
  377. {
  378. return WBEM_E_PROVIDER_NOT_CAPABLE;
  379. }
  380. // *****************************************************************************
  381. HRESULT CPrintSys::PutInstance(const CInstance& Instance, long lFlags)
  382. {
  383. return WBEM_E_PROVIDER_NOT_CAPABLE;
  384. }
  385. // *****************************************************************************
  386. HRESULT CPrintSys::DeleteInstance(const CInstance& Instance, long lFlags)
  387. {
  388. return WBEM_E_PROVIDER_NOT_CAPABLE;
  389. }