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.

206 lines
4.8 KiB

  1. // prturl.cpp : Implementation of Cprturl
  2. #include "stdafx.h"
  3. #include "oleprn.h"
  4. #include "prturl.h"
  5. #include "printer.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. // Cprturl
  8. #ifndef WIN9X
  9. // generate proper HRESULT from Win32 last error
  10. inline HRESULT HRESULTFromWIN32()
  11. {
  12. DWORD dw = GetLastError();
  13. if (ERROR_SUCCESS == dw) return E_FAIL;
  14. return HRESULT_FROM_WIN32(dw);
  15. }
  16. HRESULT Cprturl::PrivateGetSupportValue (LPTSTR pValueName, BSTR * pVal)
  17. {
  18. // The max length of the link is 255 as defined in winnt.adm
  19. #define MAX_LINK_LEN 256
  20. static TCHAR szPrinterPath[] = TEXT ("Software\\Policies\\Microsoft\\Windows NT\\Printers");
  21. HKEY hPrinterKey = NULL;
  22. TCHAR szBuffer[MAX_LINK_LEN] = {0};
  23. BOOL bRet = FALSE;
  24. DWORD dwSize = sizeof (szBuffer);
  25. DWORD dwType;
  26. if (ERROR_SUCCESS == RegOpenKeyEx (HKEY_LOCAL_MACHINE,
  27. szPrinterPath,
  28. 0,
  29. KEY_QUERY_VALUE,
  30. &hPrinterKey)) {
  31. if ((ERROR_SUCCESS == RegQueryValueEx (hPrinterKey,
  32. pValueName,
  33. 0,
  34. &dwType,
  35. (LPBYTE) szBuffer,
  36. &dwSize))
  37. && dwType == REG_SZ) {
  38. bRet = TRUE;
  39. }
  40. RegCloseKey (hPrinterKey);
  41. }
  42. if (!bRet) {
  43. szBuffer[0] = 0;
  44. }
  45. if (*pVal = SysAllocString (szBuffer))
  46. return S_OK;
  47. else
  48. return Error(IDS_OUT_OF_MEMORY, IID_Iprturl, E_OUTOFMEMORY);
  49. }
  50. STDMETHODIMP Cprturl::get_SupportLinkName(BSTR * pVal)
  51. {
  52. return PrivateGetSupportValue (TEXT ("SupportLinkName"), pVal);
  53. }
  54. STDMETHODIMP Cprturl::get_SupportLink(BSTR * pVal)
  55. {
  56. return PrivateGetSupportValue (TEXT ("SupportLink"), pVal);
  57. }
  58. STDMETHODIMP Cprturl::put_PrinterName(BSTR newVal)
  59. {
  60. HRESULT hr = S_OK;
  61. do
  62. {
  63. if (!newVal || 0 == newVal[0])
  64. {
  65. //
  66. // The printer name can't ne NULL or empty string.
  67. //
  68. hr = E_INVALIDARG;
  69. break;
  70. }
  71. CPrinter printer;
  72. if (!printer.Open(newVal))
  73. {
  74. //
  75. // Failed to open the printer. This is fatal.
  76. //
  77. hr = HRESULTFromWIN32();
  78. break;
  79. }
  80. LPTSTR pszOemName = NULL;
  81. LPTSTR pszOemUrl = printer.GetOemUrl(pszOemName);
  82. LPTSTR pszWebUrl = printer.GetPrinterWebUrl();
  83. CAutoPtrBSTR spbstrPrinterWebURL = SysAllocString(pszWebUrl);
  84. CAutoPtrBSTR spbstrPrinterOemURL = SysAllocString(pszOemUrl);
  85. CAutoPtrBSTR spbstrPrinterOemName = SysAllocString(pszOemName);
  86. if ((pszWebUrl && !spbstrPrinterWebURL) ||
  87. (pszOemUrl && !spbstrPrinterOemURL) ||
  88. (pszOemName && !spbstrPrinterOemName))
  89. {
  90. hr = E_OUTOFMEMORY;
  91. break;
  92. }
  93. //
  94. // If we are here then everything has succeeded.
  95. // Remember the new strings.
  96. //
  97. m_spbstrPrinterWebURL = spbstrPrinterWebURL.Detach();
  98. m_spbstrPrinterOemURL = spbstrPrinterOemURL.Detach();
  99. m_spbstrPrinterOemName = spbstrPrinterOemName.Detach();
  100. hr = S_OK;
  101. }
  102. while(false);
  103. return hr;
  104. }
  105. STDMETHODIMP Cprturl::get_PrinterWebURL(BSTR *pVal)
  106. {
  107. HRESULT hr = S_OK;
  108. do
  109. {
  110. if (!pVal)
  111. {
  112. hr = E_INVALIDARG;
  113. break;
  114. }
  115. if (!m_spbstrPrinterWebURL)
  116. {
  117. hr = E_UNEXPECTED;
  118. break;
  119. }
  120. *pVal = SysAllocString(m_spbstrPrinterWebURL);
  121. }
  122. while(false);
  123. return hr;
  124. }
  125. STDMETHODIMP Cprturl::get_PrinterOemURL(BSTR *pVal)
  126. {
  127. HRESULT hr = S_OK;
  128. do
  129. {
  130. if (!pVal)
  131. {
  132. hr = E_INVALIDARG;
  133. break;
  134. }
  135. if (!m_spbstrPrinterOemURL)
  136. {
  137. hr = E_UNEXPECTED;
  138. break;
  139. }
  140. *pVal = SysAllocString(m_spbstrPrinterOemURL);
  141. }
  142. while(false);
  143. return hr;
  144. }
  145. STDMETHODIMP Cprturl::get_PrinterOemName(BSTR *pVal)
  146. {
  147. HRESULT hr = S_OK;
  148. do
  149. {
  150. if (!pVal)
  151. {
  152. hr = E_INVALIDARG;
  153. break;
  154. }
  155. if (!m_spbstrPrinterOemName)
  156. {
  157. hr = E_UNEXPECTED;
  158. break;
  159. }
  160. *pVal = SysAllocString(m_spbstrPrinterOemName);
  161. }
  162. while(false);
  163. return hr;
  164. }
  165. #endif
  166. STDMETHODIMP Cprturl::get_ClientInfo(long *lpdwInfo)
  167. {
  168. if (lpdwInfo == NULL)
  169. return E_POINTER;
  170. *lpdwInfo = (long)webCreateOSInfo();
  171. return S_OK;
  172. }