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.

280 lines
6.8 KiB

  1. // OleCvt.cpp : Implementation of COleCvt
  2. #include "stdafx.h"
  3. #include <strsafe.h>
  4. #include "oleprn.h"
  5. #include "OleCvt.h"
  6. /////////////////////////////////////////////////////////////////////////////
  7. // COleCvt
  8. STDMETHODIMP COleCvt::InterfaceSupportsErrorInfo(REFIID riid)
  9. {
  10. static const IID* arr[] =
  11. {
  12. &IID_IOleCvt,
  13. };
  14. for (int i=0;i<sizeof(arr)/sizeof(arr[0]);i++)
  15. {
  16. if (InlineIsEqualGUID(*arr[i],riid))
  17. return S_OK;
  18. }
  19. return S_FALSE;
  20. }
  21. STDMETHODIMP COleCvt::OnStartPage (IUnknown* pUnk)
  22. {
  23. if(!pUnk)
  24. return E_POINTER;
  25. if (m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER)
  26. return E_ACCESSDENIED;
  27. CComPtr<IScriptingContext> spContext;
  28. HRESULT hr;
  29. // Get the IScriptingContext Interface
  30. hr = pUnk->QueryInterface(IID_IScriptingContext, (void **)&spContext);
  31. if(FAILED(hr))
  32. return hr;
  33. // Get Request Object Pointer
  34. hr = spContext->get_Request(&m_piRequest);
  35. if(FAILED(hr))
  36. {
  37. spContext.Release();
  38. return hr;
  39. }
  40. // Get Response Object Pointer
  41. hr = spContext->get_Response(&m_piResponse);
  42. if(FAILED(hr))
  43. {
  44. m_piRequest.Release();
  45. return hr;
  46. }
  47. // Get Server Object Pointer
  48. hr = spContext->get_Server(&m_piServer);
  49. if(FAILED(hr))
  50. {
  51. m_piRequest.Release();
  52. m_piResponse.Release();
  53. return hr;
  54. }
  55. // Get Session Object Pointer
  56. hr = spContext->get_Session(&m_piSession);
  57. if(FAILED(hr))
  58. {
  59. m_piRequest.Release();
  60. m_piResponse.Release();
  61. m_piServer.Release();
  62. return hr;
  63. }
  64. // Get Application Object Pointer
  65. hr = spContext->get_Application(&m_piApplication);
  66. if(FAILED(hr))
  67. {
  68. m_piRequest.Release();
  69. m_piResponse.Release();
  70. m_piServer.Release();
  71. m_piSession.Release();
  72. return hr;
  73. }
  74. m_bOnStartPageCalled = TRUE;
  75. return S_OK;
  76. }
  77. STDMETHODIMP COleCvt::OnEndPage ()
  78. {
  79. if (m_dwSafety & INTERFACESAFE_FOR_UNTRUSTED_CALLER)
  80. return E_ACCESSDENIED;
  81. m_bOnStartPageCalled = FALSE;
  82. // Release all interfaces
  83. m_piRequest.Release();
  84. m_piResponse.Release();
  85. m_piServer.Release();
  86. m_piSession.Release();
  87. m_piApplication.Release();
  88. return S_OK;
  89. }
  90. HRESULT COleCvt::SetOleCvtScriptingError(DWORD dwError)
  91. {
  92. return (SetScriptingError(CLSID_OleCvt, IID_IOleCvt, dwError));
  93. }
  94. STDMETHODIMP COleCvt::get_ToUtf8(BSTR bstrUnicode, BSTR * pVal)
  95. {
  96. LPSTR pszUtf8 = NULL;
  97. LPWSTR pwszUnicodeStr = NULL;
  98. DWORD rc;
  99. HRESULT hr = S_OK;
  100. if (!pVal)
  101. return E_POINTER;
  102. *pVal = 0;
  103. // Convert the input unicode string to utf8
  104. if (! (rc = WideCharToMultiByte( CP_UTF8, 0, bstrUnicode, -1, NULL, 0, NULL, NULL ))) {
  105. goto Cleanup;
  106. }
  107. // Allocate the string
  108. if (! (pszUtf8 = (LPSTR) LocalAlloc (LPTR, rc)))
  109. goto Cleanup;
  110. if (! (rc = WideCharToMultiByte( CP_UTF8, 0, bstrUnicode, -1, pszUtf8, rc, NULL, NULL )))
  111. goto Cleanup;
  112. // Convert the string to unicode as if it is ANSI (preserve the UTF8 conversion)
  113. if (! (rc = MultiByteToWideChar( 1252, 0, pszUtf8, -1, NULL, 0)))
  114. goto Cleanup;
  115. // Allocate the string
  116. if (! (pwszUnicodeStr = (LPWSTR) LocalAlloc (LPTR, sizeof (WCHAR) * rc)))
  117. goto Cleanup;
  118. if (! (rc = MultiByteToWideChar( 1252, 0, pszUtf8, -1, pwszUnicodeStr, rc)))
  119. goto Cleanup;
  120. if (!(*pVal = SysAllocString (pwszUnicodeStr)))
  121. goto Cleanup;
  122. SetLastError (ERROR_SUCCESS);
  123. Cleanup:
  124. if (GetLastError () != ERROR_SUCCESS)
  125. hr = SetOleCvtScriptingError (GetLastError ());
  126. if (pszUtf8)
  127. LocalFree (pszUtf8);
  128. if (pwszUnicodeStr)
  129. LocalFree (pwszUnicodeStr);
  130. return hr;
  131. }
  132. STDMETHODIMP COleCvt::get_EncodeUnicodeName(BSTR bstrSrcName, BSTR * pDstName)
  133. {
  134. LPTSTR pszEncodedName = NULL;
  135. DWORD dwSize = 0;
  136. HRESULT hr = S_OK;
  137. if (!bstrSrcName || !pDstName)
  138. return E_POINTER;
  139. EncodePrinterName (bstrSrcName, NULL, &dwSize);
  140. if (! (pszEncodedName = (LPTSTR) LocalAlloc (LPTR, sizeof (TCHAR) * dwSize)))
  141. goto Cleanup;
  142. if (!EncodePrinterName (bstrSrcName, pszEncodedName, &dwSize))
  143. goto Cleanup;
  144. if (!(*pDstName = SysAllocString (pszEncodedName)))
  145. goto Cleanup;
  146. SetLastError (ERROR_SUCCESS);
  147. Cleanup:
  148. if (GetLastError () != ERROR_SUCCESS)
  149. hr = SetOleCvtScriptingError (GetLastError ());
  150. if (pszEncodedName) {
  151. LocalFree (pszEncodedName);
  152. }
  153. return hr;
  154. }
  155. STDMETHODIMP COleCvt::get_DecodeUnicodeName(BSTR bstrSrcName, BSTR * pDstName)
  156. {
  157. LPTSTR pszDecodedName = NULL;
  158. DWORD dwSize = 0;
  159. HRESULT hr = S_OK;
  160. if (!bstrSrcName || !pDstName)
  161. return E_POINTER;
  162. DecodePrinterName (bstrSrcName, NULL, &dwSize);
  163. if (! (pszDecodedName = (LPTSTR) LocalAlloc (LPTR, sizeof (TCHAR) * dwSize)))
  164. goto Cleanup;
  165. if (!DecodePrinterName (bstrSrcName, pszDecodedName, &dwSize))
  166. goto Cleanup;
  167. if (!(*pDstName = SysAllocString (pszDecodedName)))
  168. goto Cleanup;
  169. SetLastError (ERROR_SUCCESS);
  170. Cleanup:
  171. if (GetLastError () != ERROR_SUCCESS)
  172. hr = SetOleCvtScriptingError (GetLastError ());
  173. if (pszDecodedName) {
  174. LocalFree (pszDecodedName);
  175. }
  176. return hr;
  177. }
  178. // This function is to take the input string bstrString as if it wrer ANSI
  179. // and then convert it back to unicode using whatever codepage specifed.
  180. STDMETHODIMP COleCvt::get_ToUnicode(BSTR bstrString, long lCodePage, BSTR * pVal)
  181. {
  182. LPSTR pszStr = NULL;
  183. LPWSTR pwszUnicodeStr = NULL;
  184. DWORD rc;
  185. HRESULT hr = S_OK;
  186. if (!pVal)
  187. return E_POINTER;
  188. *pVal = 0;
  189. // Convert the input unicode string to utf8
  190. if (! (rc = WideCharToMultiByte(CP_ACP, 0, bstrString, -1, NULL, 0, NULL, NULL ))) {
  191. goto Cleanup;
  192. }
  193. // Allocate the string
  194. if (! (pszStr = (LPSTR) LocalAlloc (LPTR, rc)))
  195. goto Cleanup;
  196. if (! (rc = WideCharToMultiByte( CP_ACP, 0, bstrString, -1, pszStr, rc, NULL, NULL )))
  197. goto Cleanup;
  198. // Convert the string to unicode as if it is ANSI (preserve the UTF8 conversion)
  199. if (! (rc = MultiByteToWideChar( lCodePage, 0, pszStr, -1, NULL, 0)))
  200. goto Cleanup;
  201. // Allocate the string
  202. if (! (pwszUnicodeStr = (LPWSTR) LocalAlloc (LPTR, sizeof (WCHAR) * rc)))
  203. goto Cleanup;
  204. if (! (rc = MultiByteToWideChar( lCodePage, 0, pszStr, -1, pwszUnicodeStr, rc)))
  205. goto Cleanup;
  206. if (!(*pVal = SysAllocString (pwszUnicodeStr)))
  207. goto Cleanup;
  208. SetLastError (ERROR_SUCCESS);
  209. Cleanup:
  210. if (GetLastError () != ERROR_SUCCESS)
  211. hr = SetOleCvtScriptingError (GetLastError ());
  212. if (pszStr)
  213. LocalFree (pszStr);
  214. if (pwszUnicodeStr)
  215. LocalFree (pwszUnicodeStr);
  216. return hr;
  217. }