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.

882 lines
24 KiB

  1. // Printer.cpp: implementation of the CPrinter class.
  2. //
  3. //////////////////////////////////////////////////////////////////////
  4. #include "stdafx.h"
  5. #include <strsafe.h>
  6. #include "Printer.h"
  7. #include "wininet.h"
  8. static const DWORD BIT_IGNORED_JOB = (JOB_STATUS_PAUSED |
  9. JOB_STATUS_PRINTED |
  10. JOB_STATUS_DELETING |
  11. JOB_STATUS_OFFLINE |
  12. JOB_STATUS_SPOOLING);
  13. static const DWORD BIT_ERROR_JOB = (JOB_STATUS_ERROR | JOB_STATUS_PAPEROUT);
  14. static const DWORD CHAR_PER_PAGE = 4800;
  15. static const DWORD PAGEPERJOB = 1;
  16. static const DWORD BYTEPERJOB = 2;
  17. //////////////////////////////////////////////////////////////////////
  18. // Construction/Destruction
  19. //////////////////////////////////////////////////////////////////////
  20. CPrinter::CPrinter():
  21. m_hPrinter(NULL),
  22. m_pInfo2(NULL),
  23. m_pInfo4(NULL),
  24. m_pDriverInfo6(NULL),
  25. m_bCalcJobETA(NULL),
  26. m_pszUrlBuffer(NULL),
  27. m_pszOemUrl (NULL),
  28. m_pszManufacturer (NULL)
  29. {
  30. }
  31. CPrinter::~CPrinter()
  32. {
  33. if (m_pInfo2) {
  34. LocalFree (m_pInfo2);
  35. }
  36. if (m_pInfo4) {
  37. LocalFree (m_pInfo4);
  38. }
  39. if (m_pDriverInfo6) {
  40. LocalFree (m_pDriverInfo6);
  41. }
  42. if (m_hPrinter) {
  43. ClosePrinter (m_hPrinter);
  44. }
  45. if (m_pszUrlBuffer) {
  46. LocalFree (m_pszUrlBuffer);
  47. }
  48. if (m_pszOemUrl) {
  49. LocalFree (m_pszOemUrl);
  50. }
  51. if (m_pszManufacturer) {
  52. LocalFree (m_pszManufacturer);
  53. }
  54. }
  55. BOOL CPrinter::Open(LPTSTR pPrinterName, LPHANDLE phPrinter)
  56. {
  57. PRINTER_DEFAULTS pd = {NULL, NULL, PRINTER_ACCESS_USE};
  58. if (m_hPrinter != NULL)
  59. return FALSE;
  60. if (! (OpenPrinter(pPrinterName, &m_hPrinter, &pd)))
  61. return FALSE;
  62. if (phPrinter) {
  63. *phPrinter = m_hPrinter;
  64. }
  65. return TRUE;
  66. }
  67. // This is a compiler safe way of checking that an unsigned integer is
  68. // negative and returning zero if it is or the integer if it is not
  69. // Note: Tval can be any size but must be unsigned
  70. template<class T> inline T ZeroIfNegative(T Tval) {
  71. if (Tval & (T(1) << (sizeof(Tval) * 8 - 1))) return 0;
  72. else return Tval;
  73. }
  74. BOOL CPrinter::CalJobEta()
  75. {
  76. DWORD dwPrintRate = DWERROR;
  77. PJOB_INFO_2 pJobInfo = NULL;
  78. PJOB_INFO_2 pJob;
  79. DWORD dwNumJobs;
  80. DWORD dwNeeded = 0;
  81. DWORD i;
  82. float fFactor = 1;
  83. float fTotal = 0;
  84. DWORD dwNumJobsReqested;
  85. BOOL bRet = FALSE;
  86. const DWORD cdwLimit = 256;
  87. if (! AllocGetPrinterInfo2())
  88. return NULL;
  89. if (m_pInfo2->cJobs > 0) {
  90. if (m_pInfo2->cJobs > cdwLimit) {
  91. fFactor = (float) m_pInfo2->cJobs / cdwLimit;
  92. dwNumJobsReqested = cdwLimit;
  93. }
  94. else
  95. dwNumJobsReqested = m_pInfo2->cJobs;
  96. EnumJobs (m_hPrinter, 0, dwNumJobsReqested, 2,
  97. NULL, 0, &dwNeeded, &dwNumJobs);
  98. if ((GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
  99. (NULL == (pJobInfo = (PJOB_INFO_2)LocalAlloc(LPTR, dwNeeded))) ||
  100. (!EnumJobs (m_hPrinter, 0, dwNumJobsReqested, 2, (LPBYTE) pJobInfo, dwNeeded,
  101. &dwNeeded, &dwNumJobs))) {
  102. goto Cleanup;
  103. }
  104. // Get the average Job size
  105. // Find out if we can use page as the unit
  106. for (pJob = pJobInfo, i = 0; i < dwNumJobs; i++, pJob++) {
  107. if (pJob->Status & BIT_IGNORED_JOB)
  108. continue;
  109. if (pJob->Size > 0 && pJob->TotalPages == 0)
  110. break;
  111. }
  112. m_dwPendingJobCount = 0;
  113. if (i == dwNumJobs) {
  114. // All the jobs have the page information. Use page as the unit
  115. m_dwAvgJobSizeUnit = PAGEPERJOB;
  116. for (pJob = pJobInfo, i = 0; i < dwNumJobs; i++, pJob++) {
  117. if (pJob->Status & BIT_IGNORED_JOB)
  118. continue;
  119. m_dwPendingJobCount++;
  120. fTotal += pJob->TotalPages;
  121. }
  122. }
  123. else {
  124. // Use byte as the unit
  125. m_dwAvgJobSizeUnit = BYTEPERJOB;
  126. for (pJob = pJobInfo, i = 0; i < dwNumJobs; i++, pJob++) {
  127. if (pJob->Status & BIT_IGNORED_JOB)
  128. continue;
  129. m_dwPendingJobCount++;
  130. fTotal += ZeroIfNegative(pJob->Size);
  131. }
  132. }
  133. // Calculate the averate job size
  134. if (m_dwPendingJobCount) {
  135. m_dwAvgJobSize = DWORD ((fTotal) / (float) m_dwPendingJobCount);
  136. dwPrintRate = GetPPM();
  137. if (dwPrintRate != DWERROR)
  138. m_dwJobCompletionMinute = (DWORD) (fFactor * GetWaitingMinutes (dwPrintRate, pJobInfo, dwNumJobs));
  139. }
  140. else {
  141. m_dwAvgJobSize = 0;
  142. m_dwJobCompletionMinute = 0;
  143. }
  144. m_dwPendingJobCount = (DWORD) (m_dwPendingJobCount * fFactor);
  145. }
  146. else {
  147. m_dwPendingJobCount = 0;
  148. m_dwAvgJobSize = 0;
  149. m_dwJobCompletionMinute = 0;
  150. m_dwAvgJobSizeUnit = PAGEPERJOB;
  151. }
  152. m_bCalcJobETA = TRUE;
  153. // Set the last error to ERROR_INVALID_DATA if the printer rate is not available for the current printer
  154. // or if the printer status is not suitable to display the summary information
  155. //
  156. if (dwPrintRate == DWERROR ||
  157. m_pInfo2->Status & ( PRINTER_STATUS_PAUSED |
  158. PRINTER_STATUS_ERROR |
  159. PRINTER_STATUS_PENDING_DELETION |
  160. PRINTER_STATUS_PAPER_JAM |
  161. PRINTER_STATUS_PAPER_OUT |
  162. PRINTER_STATUS_OFFLINE )) {
  163. SetLastError (ERROR_INVALID_DATA);
  164. m_dwJobCompletionMinute = DWERROR;
  165. bRet = FALSE;
  166. }
  167. else
  168. bRet = TRUE;
  169. Cleanup:
  170. if (pJobInfo)
  171. LocalFree(pJobInfo);
  172. return bRet;
  173. }
  174. DWORD CPrinter::GetWaitingMinutes(DWORD dwPPM, PJOB_INFO_2 pJobInfo, DWORD dwNumJob)
  175. {
  176. DWORD dwWaitingTime = 0;
  177. DWORD dwTotalPages = 0;
  178. if (dwNumJob == 0)
  179. return 0;
  180. if (dwPPM == 0)
  181. return DWERROR;
  182. for (DWORD i = 0; i < dwNumJob; i++, pJobInfo++) {
  183. if (pJobInfo->Status & BIT_IGNORED_JOB)
  184. continue;
  185. if (pJobInfo->Status & BIT_ERROR_JOB)
  186. return DWERROR;
  187. if (pJobInfo->TotalPages > 0) {
  188. dwTotalPages += pJobInfo->TotalPages;
  189. }
  190. else {
  191. if (pJobInfo->Size) {
  192. dwTotalPages += 1 + ZeroIfNegative(pJobInfo->Size) / CHAR_PER_PAGE;
  193. }
  194. }
  195. }
  196. if (dwTotalPages)
  197. dwWaitingTime = 1 + dwTotalPages / dwPPM;
  198. return dwWaitingTime;
  199. }
  200. DWORD CPrinter::GetPPM()
  201. {
  202. DWORD dwPrintRate;
  203. DWORD dwPrintRateUnit;
  204. // Get PrintRate
  205. dwPrintRate = MyDeviceCapabilities(m_pInfo2->pPrinterName,
  206. NULL,
  207. DC_PRINTRATE,
  208. NULL,
  209. NULL);
  210. if (dwPrintRate == DWERROR ) {
  211. return dwPrintRate;
  212. }
  213. dwPrintRateUnit = MyDeviceCapabilities(m_pInfo2->pPrinterName,
  214. NULL,
  215. DC_PRINTRATEUNIT,
  216. NULL,
  217. NULL);
  218. switch (dwPrintRateUnit) {
  219. case PRINTRATEUNIT_PPM: // PagesPerMinute
  220. break;
  221. case PRINTRATEUNIT_CPS: // CharactersPerSecond
  222. dwPrintRate = CPS2PPM (dwPrintRate);
  223. break;
  224. case PRINTRATEUNIT_LPM: // LinesPerMinute
  225. dwPrintRate = LPM2PPM (dwPrintRate);
  226. break;
  227. case PRINTRATEUNIT_IPM: // InchesPerMinute
  228. dwPrintRate = DWERROR;
  229. break;
  230. default: // Unknown
  231. dwPrintRate = DWERROR;
  232. break;
  233. }
  234. return dwPrintRate ;
  235. }
  236. BOOL CPrinter::AllocGetPrinterInfo2()
  237. {
  238. DWORD dwNeeded = 0;
  239. PPRINTER_INFO_2 pPrinterInfo = NULL;
  240. LPTSTR pszTmp;
  241. if (m_hPrinter == NULL) {
  242. SetLastError (ERROR_INVALID_HANDLE);
  243. return FALSE;
  244. }
  245. // Get a PRINTER_INFO_2 structure filled up
  246. if (GetPrinter(m_hPrinter, 2, NULL, 0, &dwNeeded) ||
  247. (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
  248. (NULL == (pPrinterInfo = (PPRINTER_INFO_2)LocalAlloc(LPTR, dwNeeded))) ||
  249. (!GetPrinter(m_hPrinter, 2, (byte *)pPrinterInfo, dwNeeded, &dwNeeded))) {
  250. if (pPrinterInfo)
  251. LocalFree(pPrinterInfo);
  252. if (! GetLastError())
  253. SetLastError (ERROR_INVALID_DATA);
  254. return FALSE;
  255. }
  256. // Mark the offline status if the attribute says offline
  257. if (pPrinterInfo->Attributes & PRINTER_ATTRIBUTE_WORK_OFFLINE) {
  258. pPrinterInfo->Status |= PRINTER_STATUS_OFFLINE;
  259. }
  260. // Extract the first port for the case of pooled printing. i.e. we don't support pooled printing.
  261. if ( pPrinterInfo->pPortName) {
  262. if( pszTmp = _tcschr( pPrinterInfo->pPortName, TEXT(',')))
  263. *pszTmp = TEXT('\0');
  264. }
  265. if (m_pInfo2) {
  266. LocalFree (m_pInfo2);
  267. }
  268. m_pInfo2 = pPrinterInfo;
  269. return TRUE;
  270. }
  271. BOOL CPrinter::AllocGetPrinterInfo4()
  272. {
  273. DWORD dwNeeded = 0;
  274. PPRINTER_INFO_4 pPrinterInfo = NULL;
  275. if (m_hPrinter == NULL) {
  276. SetLastError (ERROR_INVALID_HANDLE);
  277. return FALSE;
  278. }
  279. // Get a PRINTER_INFO_4 structure filled up
  280. if (GetPrinter(m_hPrinter, 4, NULL, 0, &dwNeeded) ||
  281. (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
  282. (NULL == (pPrinterInfo = (PPRINTER_INFO_4)LocalAlloc(LPTR, dwNeeded))) ||
  283. (!GetPrinter(m_hPrinter, 4, (byte *)pPrinterInfo, dwNeeded, &dwNeeded))) {
  284. if (pPrinterInfo)
  285. LocalFree(pPrinterInfo);
  286. if (! (GetLastError()))
  287. SetLastError (ERROR_INVALID_DATA);
  288. return FALSE;
  289. }
  290. if (m_pInfo4) {
  291. LocalFree (m_pInfo4);
  292. }
  293. m_pInfo4 = pPrinterInfo;
  294. return TRUE;
  295. }
  296. BOOL CPrinter::AllocGetDriverInfo6()
  297. {
  298. DWORD dwNeeded = 0;
  299. PDRIVER_INFO_6 pDriverInfo = NULL;
  300. if (m_hPrinter == NULL) {
  301. SetLastError (ERROR_INVALID_HANDLE);
  302. return FALSE;
  303. }
  304. // Get a DRIVER_INFO_6 structure filled up
  305. if (GetPrinterDriver(m_hPrinter, NULL, 6, NULL, 0, &dwNeeded) ||
  306. (GetLastError() != ERROR_INSUFFICIENT_BUFFER) ||
  307. (NULL == (pDriverInfo = (PDRIVER_INFO_6)LocalAlloc(LPTR, dwNeeded))) ||
  308. (!GetPrinterDriver(m_hPrinter, NULL, 6, (LPBYTE)pDriverInfo, dwNeeded, &dwNeeded))) {
  309. if (pDriverInfo)
  310. LocalFree(pDriverInfo);
  311. if (! (GetLastError()))
  312. SetLastError (ERROR_INVALID_DATA);
  313. return FALSE;
  314. }
  315. if (m_pDriverInfo6) {
  316. LocalFree (m_pDriverInfo6);
  317. }
  318. m_pDriverInfo6 = pDriverInfo;
  319. return TRUE;
  320. }
  321. PPRINTER_INFO_2 CPrinter::GetPrinterInfo2()
  322. {
  323. if (m_pInfo2 == NULL) {
  324. if (! AllocGetPrinterInfo2())
  325. return NULL;
  326. }
  327. return m_pInfo2;
  328. }
  329. PDRIVER_INFO_6 CPrinter::GetDriverInfo6()
  330. {
  331. if (m_pDriverInfo6 == NULL) {
  332. if (! AllocGetDriverInfo6()) {
  333. return NULL;
  334. }
  335. }
  336. return m_pDriverInfo6;
  337. }
  338. DWORD CPrinter::GetWaitingTime()
  339. {
  340. if (!m_bCalcJobETA) {
  341. SetLastError (ERROR_INVALID_DATA);
  342. return FALSE;
  343. }
  344. return m_dwJobCompletionMinute;
  345. }
  346. BOOL CPrinter::GetJobEtaData (DWORD & dwWaitingTime, DWORD &dwJobCount, DWORD &dwJobSize, DWORD &dwJob)
  347. {
  348. if (!m_bCalcJobETA) {
  349. SetLastError (ERROR_INVALID_DATA);
  350. return FALSE;
  351. }
  352. dwJobCount = m_dwPendingJobCount;
  353. dwJobSize = m_dwAvgJobSize;
  354. dwWaitingTime = m_dwJobCompletionMinute;
  355. dwJob = m_dwAvgJobSizeUnit;
  356. return TRUE;
  357. }
  358. LPTSTR CPrinter::GetPrinterWebUrl(void)
  359. {
  360. static const TCHAR c_szHttp[] = TEXT("http://");
  361. static const TCHAR c_szHttps[] = TEXT("https://");
  362. BOOL bReturn = FALSE;
  363. DWORD dwLastError = ERROR_SUCCESS;
  364. DWORD dwLen = 0;
  365. TCHAR szBuffer[MAX_COMPUTERNAME_LENGTH+1];
  366. DWORD dwBufferSize;
  367. DWORD dwAttributes;
  368. LPCTSTR pszServerName;
  369. LPTSTR pszShareName;
  370. LPTSTR pszSplServerName;
  371. LPTSTR pszSplPrinterName;
  372. LPTSTR pszSplShareName;
  373. //
  374. // Get printer info 4 to fetch the attribute.
  375. //
  376. bReturn = AllocGetPrinterInfo4();
  377. if (!bReturn) {
  378. if (GetLastError () == ERROR_INVALID_LEVEL ) {
  379. //
  380. // Most likely it is a remote printers folder, no support for level4, so
  381. // we have to use level 2 instead.
  382. //
  383. if (! AllocGetPrinterInfo2())
  384. goto Cleanup;
  385. }
  386. else {
  387. //
  388. // The call fails with other reasons
  389. //
  390. goto Cleanup;
  391. }
  392. }
  393. else{
  394. if (m_pInfo4->Attributes & PRINTER_ATTRIBUTE_LOCAL) {
  395. // Check if the local flag is on. If so, try to get printer info2 for more information
  396. if (! AllocGetPrinterInfo2())
  397. goto Cleanup;
  398. }
  399. }
  400. //
  401. // Assume failure
  402. //
  403. bReturn = FALSE;
  404. if (m_pInfo2) {
  405. dwAttributes = m_pInfo2->Attributes;
  406. pszSplServerName = m_pInfo2->pServerName;
  407. pszSplPrinterName = m_pInfo2->pPrinterName;
  408. pszSplShareName = m_pInfo2->pShareName;
  409. }
  410. else
  411. {
  412. dwAttributes = m_pInfo4->Attributes;
  413. pszSplServerName = m_pInfo4->pServerName;
  414. pszSplPrinterName = m_pInfo4->pPrinterName;
  415. pszSplShareName = NULL;
  416. }
  417. //
  418. // Check if it is a printer connected to an http port
  419. // then the port name is the url.
  420. //
  421. if( m_pInfo2 )
  422. {
  423. if( m_pInfo2->pPortName )
  424. {
  425. //
  426. // Compare the port name prefex to see if it is an HTTP port.
  427. //
  428. if( !_tcsnicmp( m_pInfo2->pPortName, c_szHttp, _tcslen( c_szHttp ) ) ||
  429. !_tcsnicmp( m_pInfo2->pPortName, c_szHttps, _tcslen( c_szHttps ) ) )
  430. {
  431. //
  432. // We always use portname as the URL
  433. //
  434. dwLen = 1 + lstrlen( m_pInfo2->pPortName );
  435. if (! (m_pszUrlBuffer = (LPTSTR) LocalAlloc (LPTR, dwLen * sizeof (TCHAR))))
  436. {
  437. goto Cleanup;
  438. }
  439. StringCchCopy( m_pszUrlBuffer, dwLen, m_pInfo2->pPortName );
  440. bReturn = TRUE;
  441. goto Cleanup;
  442. }
  443. }
  444. }
  445. //
  446. // If it is an unshared printer, return false
  447. //
  448. if ( !(dwAttributes & PRINTER_ATTRIBUTE_SHARED) )
  449. {
  450. goto Cleanup;
  451. }
  452. //
  453. // Check if it is a connection or a local printer or a masq printer
  454. // which is not connected over http, then build the url
  455. // from the \\server name\share name.
  456. //
  457. if( !pszSplServerName )
  458. {
  459. dwBufferSize = COUNTOF( szBuffer );
  460. if( !GetComputerName( szBuffer, &dwBufferSize ) )
  461. {
  462. goto Cleanup;
  463. }
  464. pszServerName = szBuffer;
  465. }
  466. //
  467. // Server name was provided then set our pointer to just
  468. // after the two leading wacks.
  469. //
  470. else
  471. {
  472. if( pszSplServerName[0] == TEXT('\\') &&
  473. pszSplServerName[1] == TEXT('\\') )
  474. {
  475. pszServerName = pszSplServerName + 2;
  476. }
  477. else
  478. {
  479. goto Cleanup;
  480. }
  481. }
  482. if ( !IsWebServerInstalled(pszSplServerName) ) {
  483. dwLastError = ERROR_NO_BROWSER_SERVERS_FOUND;
  484. goto Cleanup;
  485. }
  486. //
  487. // Build the URL - http://server/printers/ipp_0004.asp?printer=ShareName
  488. //
  489. if (pszSplShareName)
  490. {
  491. pszShareName = pszSplShareName;
  492. }
  493. else {
  494. //
  495. // Parse the sharename/printername from the printer name
  496. //
  497. if(pszSplPrinterName) {
  498. if (pszSplPrinterName[0] == TEXT ('\\') && pszSplPrinterName[1] == TEXT ('\\') )
  499. {
  500. pszShareName = _tcschr (pszSplPrinterName + 2, TEXT ('\\'));
  501. pszShareName++;
  502. }
  503. else
  504. pszShareName = pszSplPrinterName;
  505. }
  506. else
  507. {
  508. goto Cleanup;
  509. }
  510. }
  511. GetWebUIUrl (pszServerName, pszShareName, NULL, &dwLen);
  512. if (GetLastError () != ERROR_INSUFFICIENT_BUFFER )
  513. {
  514. goto Cleanup;
  515. }
  516. if (! (m_pszUrlBuffer = (LPTSTR) LocalAlloc (LPTR, dwLen * sizeof (TCHAR))))
  517. {
  518. goto Cleanup;
  519. }
  520. if (!GetWebUIUrl (pszServerName, pszShareName, m_pszUrlBuffer, &dwLen))
  521. {
  522. goto Cleanup;
  523. }
  524. //
  525. // Indicate success.
  526. //
  527. bReturn = TRUE;
  528. //
  529. // Clean any opened or allocated resources.
  530. //
  531. Cleanup:
  532. //
  533. // If this routine failed then set the last error.
  534. //
  535. if( !bReturn )
  536. {
  537. //
  538. // If the last error was not set then a called routine may
  539. // have set the last error. We don't want to clear the
  540. // last error.
  541. //
  542. if( dwLastError != ERROR_SUCCESS )
  543. {
  544. SetLastError( dwLastError );
  545. }
  546. }
  547. return m_pszUrlBuffer;
  548. }
  549. BOOL CPrinter::GetDriverData(
  550. DriverData dwDriverData,
  551. LPTSTR &pszData)
  552. {
  553. static const ULONG_PTR ulpOffset[LastDriverData] = {
  554. // This has the offsets into the DRIVER_INFO_6 structure.....
  555. ULOFFSET( PDRIVER_INFO_6, pszOEMUrl ) ,
  556. ULOFFSET( PDRIVER_INFO_6, pszHardwareID ) ,
  557. ULOFFSET( PDRIVER_INFO_6, pszMfgName)
  558. };
  559. LPTSTR pszDataString = NULL;
  560. BOOL bRet = FALSE;
  561. DWORD dwSize;
  562. ASSERT( (int)dwDriverData >= 0 && (int)dwDriverData < LastDriverData );
  563. if (! GetDriverInfo6() )
  564. goto Cleanup;
  565. pszDataString = *(LPTSTR *)(((ULONG_PTR) m_pDriverInfo6) + ulpOffset[dwDriverData] );
  566. if (pszDataString == NULL || *pszDataString == NULL)
  567. goto Cleanup;
  568. dwSize = sizeof(TCHAR) * (lstrlen( pszDataString ) + 1);
  569. if (! (pszData = (LPTSTR)LocalAlloc(LPTR, dwSize)))
  570. goto Cleanup;
  571. //
  572. // Use byte-size there.
  573. //
  574. StringCbCopy( pszData, dwSize, pszDataString );
  575. bRet = TRUE;
  576. Cleanup:
  577. return bRet;
  578. }
  579. BOOL CPrinter::ParseUrlPattern(
  580. LPTSTR pSrc,
  581. LPTSTR pDest,
  582. DWORD &dwDestLen)
  583. {
  584. const dwMaxMacroLen = 255;
  585. enum {
  586. NORMALTEXT, STARTMACRO
  587. } URL_PATTERN_STATE;
  588. BOOL bRet = FALSE;
  589. DWORD dwLen = 0;
  590. DWORD dwMacroLen = 0;
  591. DWORD dwAvailbleSize;
  592. DWORD dwState = NORMALTEXT;
  593. int i;
  594. TCHAR ch;
  595. TCHAR szMacroName [dwMaxMacroLen + 1];
  596. LPTSTR pMacroValue = NULL;
  597. LPTSTR pszMacroList[] = {
  598. TEXT ("MODEL"),
  599. TEXT ("HARDWAREID"),
  600. };
  601. while (ch = *pSrc++) {
  602. switch (dwState) {
  603. case NORMALTEXT:
  604. if (ch == TEXT ('%')) {
  605. // Start a macro
  606. dwState = STARTMACRO;
  607. dwMacroLen = 0;
  608. szMacroName[0] = 0;
  609. }
  610. else {
  611. if (dwLen >= dwDestLen) {
  612. dwLen ++;
  613. }
  614. else {
  615. pDest[dwLen++] = ch;
  616. }
  617. }
  618. break;
  619. case STARTMACRO:
  620. if (ch == TEXT ('%')) {
  621. szMacroName[dwMacroLen] = 0;
  622. // Replace Macro
  623. for (int i = 0; i < sizeof (pszMacroList) / sizeof (pszMacroList[0]); i++) {
  624. if (!lstrcmpi (szMacroName, pszMacroList[i])) {
  625. pMacroValue = 0;
  626. switch (i) {
  627. case 0:
  628. AssignString (pMacroValue, m_pInfo2->pDriverName);
  629. break;
  630. case 1:
  631. GetDriverData (HardwareID , pMacroValue);
  632. break;
  633. default:
  634. break;
  635. }
  636. if (pMacroValue) {
  637. if (dwDestLen > dwLen)
  638. dwAvailbleSize = dwDestLen - dwLen;
  639. else
  640. dwAvailbleSize = 0;
  641. TCHAR szPlaceHolder[1];
  642. DWORD dwBufSize = sizeof (szPlaceHolder) / sizeof (TCHAR);
  643. if (!InternetCanonicalizeUrl (pMacroValue, szPlaceHolder, &dwBufSize, 0)) {
  644. if (dwBufSize < dwAvailbleSize ) {
  645. if (!InternetCanonicalizeUrl (pMacroValue, pDest + dwLen, &dwBufSize, 0)) {
  646. LocalFree (pMacroValue);
  647. return bRet;
  648. }
  649. else {
  650. dwLen = lstrlen (pDest);
  651. }
  652. }
  653. else {
  654. dwLen += dwBufSize;
  655. }
  656. }
  657. LocalFree (pMacroValue);
  658. }
  659. break;
  660. }
  661. }
  662. dwState = NORMALTEXT;
  663. }
  664. else {
  665. if (dwMacroLen < dwMaxMacroLen) {
  666. szMacroName[dwMacroLen ++] = ch;
  667. }
  668. }
  669. break;
  670. }
  671. }
  672. if (dwState == STARTMACRO) {
  673. SetLastError ( ERROR_INVALID_DATA );
  674. }
  675. else {
  676. if (dwLen >= dwDestLen) {
  677. SetLastError (ERROR_INSUFFICIENT_BUFFER);
  678. dwDestLen = dwLen + 1;
  679. }
  680. else {
  681. pDest[dwLen] = 0;
  682. bRet = TRUE;
  683. }
  684. }
  685. return bRet;
  686. }
  687. LPTSTR CPrinter::GetOemUrl(
  688. LPTSTR & pszManufacturer)
  689. {
  690. LPTSTR pszOemUrlPattern = NULL;
  691. DWORD dwLen = 0;
  692. LPTSTR pszUrl = NULL;
  693. if (!GetPrinterInfo2 () )
  694. goto Cleanup;
  695. if (GetDriverData (OEMUrlPattern, pszOemUrlPattern)) {
  696. if (! ParseUrlPattern (pszOemUrlPattern, NULL, dwLen)
  697. && GetLastError() == ERROR_INSUFFICIENT_BUFFER) {
  698. m_pszOemUrl = (LPTSTR) LocalAlloc (LPTR, sizeof (TCHAR) * dwLen);
  699. if (m_pszOemUrl) {
  700. if (!ParseUrlPattern (pszOemUrlPattern, m_pszOemUrl, dwLen))
  701. goto Cleanup;
  702. }
  703. else
  704. goto Cleanup;
  705. }
  706. }
  707. if (GetDriverData (Manufacturer, m_pszManufacturer)) {
  708. pszManufacturer = m_pszManufacturer;
  709. pszUrl = m_pszOemUrl;
  710. }
  711. Cleanup:
  712. if (pszOemUrlPattern) {
  713. LocalFree ( pszOemUrlPattern);
  714. }
  715. return pszUrl;
  716. }