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.

206 lines
5.0 KiB

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