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.

278 lines
6.5 KiB

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