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.

370 lines
13 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Active Template Library Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Active Template Library product.
  10. #ifndef __ATLCONV_H__
  11. #define __ATLCONV_H__
  12. #ifndef __cplusplus
  13. #error ATL requires C++ compilation (use a .cpp suffix)
  14. #endif
  15. #include <atldef.h>
  16. #ifndef _INC_MALLOC
  17. #include <malloc.h>
  18. #endif // _INC_MALLOC
  19. #pragma pack(push,8)
  20. #ifdef _CONVERSION_USES_THREAD_LOCALE
  21. #ifndef _DEBUG
  22. #define USES_CONVERSION int _convert; _convert; UINT _acp = GetACP(); _acp; LPCWSTR _lpw; _lpw; LPCSTR _lpa; _lpa
  23. #else
  24. #define USES_CONVERSION int _convert = 0; _convert; UINT _acp = GetACP(); _acp; LPCWSTR _lpw = NULL; _lpw; LPCSTR _lpa = NULL; _lpa
  25. #endif
  26. #else
  27. #ifndef _DEBUG
  28. #define USES_CONVERSION int _convert; _convert; UINT _acp = CP_ACP; _acp; LPCWSTR _lpw; _lpw; LPCSTR _lpa; _lpa
  29. #else
  30. #define USES_CONVERSION int _convert = 0; _convert; UINT _acp = CP_ACP; _acp; LPCWSTR _lpw = NULL; _lpw; LPCSTR _lpa = NULL; _lpa
  31. #endif
  32. #endif
  33. #ifdef _WINGDI_
  34. ATLAPI_(LPDEVMODEA) AtlDevModeW2A(LPDEVMODEA lpDevModeA, LPDEVMODEW lpDevModeW);
  35. #endif
  36. /////////////////////////////////////////////////////////////////////////////
  37. // Global UNICODE<>ANSI translation helpers
  38. inline LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars, UINT acp)
  39. {
  40. ATLASSERT(lpa != NULL);
  41. ATLASSERT(lpw != NULL);
  42. // verify that no illegal character present
  43. // since lpw was allocated based on the size of lpa
  44. // don't worry about the number of chars
  45. lpw[0] = '\0';
  46. MultiByteToWideChar(acp, 0, lpa, -1, lpw, nChars);
  47. return lpw;
  48. }
  49. inline LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars, UINT acp)
  50. {
  51. ATLASSERT(lpw != NULL);
  52. ATLASSERT(lpa != NULL);
  53. // verify that no illegal character present
  54. // since lpa was allocated based on the size of lpw
  55. // don't worry about the number of chars
  56. lpa[0] = '\0';
  57. WideCharToMultiByte(acp, 0, lpw, -1, lpa, nChars, NULL, NULL);
  58. return lpa;
  59. }
  60. inline LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars)
  61. {
  62. return AtlA2WHelper(lpw, lpa, nChars, CP_ACP);
  63. }
  64. inline LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars)
  65. {
  66. return AtlW2AHelper(lpa, lpw, nChars, CP_ACP);
  67. }
  68. #ifdef _CONVERSION_USES_THREAD_LOCALE
  69. #ifdef ATLA2WHELPER
  70. #undef ATLA2WHELPER
  71. #undef ATLW2AHELPER
  72. #endif
  73. #define ATLA2WHELPER AtlA2WHelper
  74. #define ATLW2AHELPER AtlW2AHelper
  75. #else
  76. #ifndef ATLA2WHELPER
  77. #define ATLA2WHELPER AtlA2WHelper
  78. #define ATLW2AHELPER AtlW2AHelper
  79. #endif
  80. #endif
  81. #ifdef _CONVERSION_USES_THREAD_LOCALE
  82. #define A2W(lpa) (\
  83. ((_lpa = lpa) == NULL) ? NULL : (\
  84. _convert = (lstrlenA(_lpa)+1),\
  85. ATLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert, _acp)))
  86. #else
  87. #define A2W(lpa) (\
  88. ((_lpa = lpa) == NULL) ? NULL : (\
  89. _convert = (lstrlenA(_lpa)+1),\
  90. ATLA2WHELPER((LPWSTR) alloca(_convert*2), _lpa, _convert)))
  91. #endif
  92. #ifdef _CONVERSION_USES_THREAD_LOCALE
  93. #define W2A(lpw) (\
  94. ((_lpw = lpw) == NULL) ? NULL : (\
  95. _convert = (lstrlenW(_lpw)+1)*2,\
  96. ATLW2AHELPER((LPSTR) alloca(_convert), _lpw, _convert, _acp)))
  97. #else
  98. #define W2A(lpw) (\
  99. ((_lpw = lpw) == NULL) ? NULL : (\
  100. _convert = (lstrlenW(_lpw)+1)*2,\
  101. ATLW2AHELPER((LPSTR) alloca(_convert), _lpw, _convert)))
  102. #endif
  103. #define A2CW(lpa) ((LPCWSTR)A2W(lpa))
  104. #define W2CA(lpw) ((LPCSTR)W2A(lpw))
  105. #if defined(_UNICODE)
  106. // in these cases the default (TCHAR) is the same as OLECHAR
  107. inline int ocslen(LPCOLESTR x) { return lstrlenW(x); }
  108. inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpyW(dest, src); }
  109. inline OLECHAR* ocscat(LPOLESTR dest, LPCOLESTR src) { return lstrcatW(dest, src); }
  110. inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
  111. inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
  112. inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
  113. inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
  114. inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNextW(lp);}
  115. #elif defined(OLE2ANSI)
  116. // in these cases the default (TCHAR) is the same as OLECHAR
  117. inline int ocslen(LPCOLESTR x) { return lstrlen(x); }
  118. inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpy(dest, src); }
  119. inline OLECHAR* ocscat(LPOLESTR dest, LPCOLESTR src) { return ocscpy(dest+ocslen(dest), src); }
  120. inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
  121. inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
  122. inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
  123. inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
  124. inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNext(lp);}
  125. #else
  126. inline int ocslen(LPCOLESTR x) { return lstrlenW(x); }
  127. //lstrcpyW doesn't work on Win95, so we do this
  128. inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src)
  129. {return (LPOLESTR) memcpy(dest, src, (lstrlenW(src)+1)*sizeof(WCHAR));}
  130. inline OLECHAR* ocscat(LPOLESTR dest, LPCOLESTR src) { return ocscpy(dest+ocslen(dest), src); }
  131. //CharNextW doesn't work on Win95 so we use this
  132. #define T2COLE(lpa) A2CW(lpa)
  133. #define T2OLE(lpa) A2W(lpa)
  134. #define OLE2CT(lpo) W2CA(lpo)
  135. #define OLE2T(lpo) W2A(lpo)
  136. inline LPOLESTR CharNextO(LPCOLESTR lp) {return (LPOLESTR) ((*lp) ? (lp+1) : lp);}
  137. #endif
  138. #ifdef OLE2ANSI
  139. inline LPOLESTR A2OLE(LPSTR lp) { return lp;}
  140. inline LPSTR OLE2A(LPOLESTR lp) { return lp;}
  141. #define W2OLE W2A
  142. #define OLE2W A2W
  143. inline LPCOLESTR A2COLE(LPCSTR lp) { return lp;}
  144. inline LPCSTR OLE2CA(LPCOLESTR lp) { return lp;}
  145. #define W2COLE W2CA
  146. #define OLE2CW A2CW
  147. #else
  148. inline LPOLESTR W2OLE(LPWSTR lp) { return lp; }
  149. inline LPWSTR OLE2W(LPOLESTR lp) { return lp; }
  150. #define A2OLE A2W
  151. #define OLE2A W2A
  152. inline LPCOLESTR W2COLE(LPCWSTR lp) { return lp; }
  153. inline LPCWSTR OLE2CW(LPCOLESTR lp) { return lp; }
  154. #define A2COLE A2CW
  155. #define OLE2CA W2CA
  156. #endif
  157. #ifdef _UNICODE
  158. #define T2A W2A
  159. #define A2T A2W
  160. inline LPWSTR T2W(LPTSTR lp) { return lp; }
  161. inline LPTSTR W2T(LPWSTR lp) { return lp; }
  162. #define T2CA W2CA
  163. #define A2CT A2CW
  164. inline LPCWSTR T2CW(LPCTSTR lp) { return lp; }
  165. inline LPCTSTR W2CT(LPCWSTR lp) { return lp; }
  166. #else
  167. #define T2W A2W
  168. #define W2T W2A
  169. inline LPSTR T2A(LPTSTR lp) { return lp; }
  170. inline LPTSTR A2T(LPSTR lp) { return lp; }
  171. #define T2CW A2CW
  172. #define W2CT W2CA
  173. inline LPCSTR T2CA(LPCTSTR lp) { return lp; }
  174. inline LPCTSTR A2CT(LPCSTR lp) { return lp; }
  175. #endif
  176. inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
  177. {
  178. if ((lp == NULL) || (nLen == 0))
  179. return NULL;
  180. USES_CONVERSION;
  181. BSTR str = NULL;
  182. int nConvertedLen = MultiByteToWideChar(_acp, 0, lp,
  183. nLen, NULL, NULL)-1;
  184. str = ::SysAllocStringLen(NULL, nConvertedLen);
  185. if (str != NULL)
  186. {
  187. MultiByteToWideChar(_acp, 0, lp, -1,
  188. str, nConvertedLen);
  189. }
  190. return str;
  191. }
  192. inline BSTR OLE2BSTR(LPCOLESTR lp) {return ::SysAllocString(lp);}
  193. #if defined(_UNICODE)
  194. // in these cases the default (TCHAR) is the same as OLECHAR
  195. inline BSTR T2BSTR(LPCTSTR lp) {return ::SysAllocString(lp);}
  196. inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
  197. inline BSTR W2BSTR(LPCWSTR lp) {return ::SysAllocString(lp);}
  198. #elif defined(OLE2ANSI)
  199. // in these cases the default (TCHAR) is the same as OLECHAR
  200. inline BSTR T2BSTR(LPCTSTR lp) {return ::SysAllocString(lp);}
  201. inline BSTR A2BSTR(LPCSTR lp) {return ::SysAllocString(lp);}
  202. inline BSTR W2BSTR(LPCWSTR lp) {USES_CONVERSION; return ::SysAllocString(W2COLE(lp));}
  203. #else
  204. inline BSTR T2BSTR(LPCTSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
  205. inline BSTR A2BSTR(LPCSTR lp) {USES_CONVERSION; return A2WBSTR(lp);}
  206. inline BSTR W2BSTR(LPCWSTR lp) {return ::SysAllocString(lp);}
  207. #endif
  208. #ifdef _WINGDI_
  209. /////////////////////////////////////////////////////////////////////////////
  210. // Global UNICODE<>ANSI translation helpers
  211. inline LPDEVMODEW AtlDevModeA2W(LPDEVMODEW lpDevModeW, LPDEVMODEA lpDevModeA)
  212. {
  213. USES_CONVERSION;
  214. if (lpDevModeA == NULL)
  215. return NULL;
  216. ATLASSERT(lpDevModeW != NULL);
  217. AtlA2WHelper(lpDevModeW->dmDeviceName, (LPCSTR)lpDevModeA->dmDeviceName, 32*sizeof(WCHAR), _acp);
  218. memcpy(&lpDevModeW->dmSpecVersion, &lpDevModeA->dmSpecVersion,
  219. offsetof(DEVMODEW, dmFormName) - offsetof(DEVMODEW, dmSpecVersion));
  220. AtlA2WHelper(lpDevModeW->dmFormName, (LPCSTR)lpDevModeA->dmFormName, 32*sizeof(WCHAR), _acp);
  221. memcpy(&lpDevModeW->dmLogPixels, &lpDevModeA->dmLogPixels,
  222. sizeof(DEVMODEW) - offsetof(DEVMODEW, dmLogPixels));
  223. if (lpDevModeA->dmDriverExtra != 0)
  224. memcpy(lpDevModeW+1, lpDevModeA+1, lpDevModeA->dmDriverExtra);
  225. lpDevModeW->dmSize = sizeof(DEVMODEW);
  226. return lpDevModeW;
  227. }
  228. inline LPTEXTMETRICW AtlTextMetricA2W(LPTEXTMETRICW lptmW, LPTEXTMETRICA lptmA)
  229. {
  230. USES_CONVERSION;
  231. if (lptmA == NULL)
  232. return NULL;
  233. ATLASSERT(lptmW != NULL);
  234. memcpy(lptmW, lptmA, sizeof(LONG) * 11);
  235. memcpy(&lptmW->tmItalic, &lptmA->tmItalic, sizeof(BYTE) * 5);
  236. MultiByteToWideChar(_acp, 0, (LPCSTR)&lptmA->tmFirstChar, 1, &lptmW->tmFirstChar, 1);
  237. MultiByteToWideChar(_acp, 0, (LPCSTR)&lptmA->tmLastChar, 1, &lptmW->tmLastChar, 1);
  238. MultiByteToWideChar(_acp, 0, (LPCSTR)&lptmA->tmDefaultChar, 1, &lptmW->tmDefaultChar, 1);
  239. MultiByteToWideChar(_acp, 0, (LPCSTR)&lptmA->tmBreakChar, 1, &lptmW->tmBreakChar, 1);
  240. return lptmW;
  241. }
  242. inline LPTEXTMETRICA AtlTextMetricW2A(LPTEXTMETRICA lptmA, LPTEXTMETRICW lptmW)
  243. {
  244. USES_CONVERSION;
  245. if (lptmW == NULL)
  246. return NULL;
  247. ATLASSERT(lptmA != NULL);
  248. memcpy(lptmA, lptmW, sizeof(LONG) * 11);
  249. memcpy(&lptmA->tmItalic, &lptmW->tmItalic, sizeof(BYTE) * 5);
  250. WideCharToMultiByte(_acp, 0, &lptmW->tmFirstChar, 1, (LPSTR)&lptmA->tmFirstChar, 1, NULL, NULL);
  251. WideCharToMultiByte(_acp, 0, &lptmW->tmLastChar, 1, (LPSTR)&lptmA->tmLastChar, 1, NULL, NULL);
  252. WideCharToMultiByte(_acp, 0, &lptmW->tmDefaultChar, 1, (LPSTR)&lptmA->tmDefaultChar, 1, NULL, NULL);
  253. WideCharToMultiByte(_acp, 0, &lptmW->tmBreakChar, 1, (LPSTR)&lptmA->tmBreakChar, 1, NULL, NULL);
  254. return lptmA;
  255. }
  256. #ifndef ATLDEVMODEA2W
  257. #define ATLDEVMODEA2W AtlDevModeA2W
  258. #define ATLDEVMODEW2A AtlDevModeW2A
  259. #define ATLTEXTMETRICA2W AtlTextMetricA2W
  260. #define ATLTEXTMETRICW2A AtlTextMetricW2A
  261. #endif
  262. #define DEVMODEW2A(lpw)\
  263. ((lpw == NULL) ? NULL : ATLDEVMODEW2A((LPDEVMODEA)alloca(sizeof(DEVMODEA)+lpw->dmDriverExtra), lpw))
  264. #define DEVMODEA2W(lpa)\
  265. ((lpa == NULL) ? NULL : ATLDEVMODEA2W((LPDEVMODEW)alloca(sizeof(DEVMODEW)+lpa->dmDriverExtra), lpa))
  266. #define TEXTMETRICW2A(lptmw)\
  267. ((lptmw == NULL) ? NULL : ATLTEXTMETRICW2A((LPTEXTMETRICA)alloca(sizeof(TEXTMETRICA)), lptmw))
  268. #define TEXTMETRICA2W(lptma)\
  269. ((lptma == NULL) ? NULL : ATLTEXTMETRICA2W((LPTEXTMETRICW)alloca(sizeof(TEXTMETRICW)), lptma))
  270. #ifdef OLE2ANSI
  271. #define DEVMODEOLE DEVMODEA
  272. #define LPDEVMODEOLE LPDEVMODEA
  273. #define TEXTMETRICOLE TEXTMETRICA
  274. #define LPTEXTMETRICOLE LPTEXTMETRICA
  275. #else
  276. #define DEVMODEOLE DEVMODEW
  277. #define LPDEVMODEOLE LPDEVMODEW
  278. #define TEXTMETRICOLE TEXTMETRICW
  279. #define LPTEXTMETRICOLE LPTEXTMETRICW
  280. #endif
  281. #if defined(_UNICODE)
  282. // in these cases the default (TCHAR) is the same as OLECHAR
  283. inline LPDEVMODEW DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
  284. inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODEW lp) { return lp; }
  285. inline LPTEXTMETRICW TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
  286. inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRICW lp) { return lp; }
  287. #elif defined(OLE2ANSI)
  288. // in these cases the default (TCHAR) is the same as OLECHAR
  289. inline LPDEVMODE DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
  290. inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODE lp) { return lp; }
  291. inline LPTEXTMETRIC TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
  292. inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRIC lp) { return lp; }
  293. #else
  294. #define DEVMODEOLE2T(lpo) DEVMODEW2A(lpo)
  295. #define DEVMODET2OLE(lpa) DEVMODEA2W(lpa)
  296. #define TEXTMETRICOLE2T(lptmw) TEXTMETRICW2A(lptmw)
  297. #define TEXTMETRICT2OLE(lptma) TEXTMETRICA2W(lptma)
  298. #endif
  299. #endif //_WINGDI_
  300. #pragma pack(pop)
  301. #ifndef _ATL_DLL_IMPL
  302. #ifndef _ATL_DLL
  303. #define _ATLCONV_IMPL
  304. #endif
  305. #endif
  306. #endif // __ATLCONV_H__
  307. /////////////////////////////////////////////////////////////////////////////
  308. #ifdef _ATLCONV_IMPL
  309. #ifdef _WINGDI_
  310. ATLINLINE ATLAPI_(LPDEVMODEA) AtlDevModeW2A(LPDEVMODEA lpDevModeA, LPDEVMODEW lpDevModeW)
  311. {
  312. USES_CONVERSION;
  313. if (lpDevModeW == NULL)
  314. return NULL;
  315. ATLASSERT(lpDevModeA != NULL);
  316. AtlW2AHelper((LPSTR)lpDevModeA->dmDeviceName, lpDevModeW->dmDeviceName, 32*sizeof(char), _acp);
  317. memcpy(&lpDevModeA->dmSpecVersion, &lpDevModeW->dmSpecVersion,
  318. offsetof(DEVMODEA, dmFormName) - offsetof(DEVMODEA, dmSpecVersion));
  319. AtlW2AHelper((LPSTR)lpDevModeA->dmFormName, lpDevModeW->dmFormName, 32*sizeof(char), _acp);
  320. memcpy(&lpDevModeA->dmLogPixels, &lpDevModeW->dmLogPixels,
  321. sizeof(DEVMODEA) - offsetof(DEVMODEA, dmLogPixels));
  322. if (lpDevModeW->dmDriverExtra != 0)
  323. memcpy(lpDevModeA+1, lpDevModeW+1, lpDevModeW->dmDriverExtra);
  324. lpDevModeA->dmSize = sizeof(DEVMODEA);
  325. return lpDevModeA;
  326. }
  327. #endif //_WINGDI
  328. //Prevent pulling in second time
  329. #undef _ATLCONV_IMPL
  330. #endif // _ATLCONV_IMPL
  331. #define _ATL_NO_CONVERSIONS