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.

656 lines
22 KiB

  1. // This is a part of the Active Template Library.
  2. // Copyright (C) 1996-1997 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. #ifndef _INC_MALLOC
  16. #include <malloc.h>
  17. #endif // _INC_MALLOC
  18. #pragma pack(push,8)
  19. namespace ATL
  20. {
  21. namespace _ATL_SAFE_ALLOCA_IMPL
  22. {
  23. // Following code is to avoid alloca causing a stack overflow.
  24. // It is intended for use from the _ATL_SAFE_ALLOCA macros
  25. // or Conversion macros.
  26. __declspec(selectany) DWORD _Atlosplatform = 0;
  27. inline BOOL _AtlGetVersionEx()
  28. {
  29. OSVERSIONINFO osi;
  30. memset(&osi, 0, sizeof(OSVERSIONINFO));
  31. osi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  32. GetVersionEx(&osi);
  33. _Atlosplatform = osi.dwPlatformId;
  34. return TRUE;
  35. }
  36. // From VC7 CRT sources.
  37. #define _ATL_MIN_STACK_REQ_WIN9X 0x11000
  38. #define _ATL_MIN_STACK_REQ_WINNT 0x2000
  39. /***
  40. * void _resetstkoflw(void) - Recovers from Stack Overflow
  41. *
  42. * Purpose:
  43. * Sets the guard page to its position before the stack overflow.
  44. *
  45. * Exit:
  46. * Returns nonzero on success, zero on failure
  47. *
  48. *******************************************************************************/
  49. inline int _Atlresetstkoflw(void)
  50. {
  51. static BOOL bTemp = _AtlGetVersionEx();
  52. LPBYTE pStack, pGuard, pStackBase, pMaxGuard, pMinGuard;
  53. MEMORY_BASIC_INFORMATION mbi;
  54. SYSTEM_INFO si;
  55. DWORD PageSize;
  56. DWORD flNewProtect;
  57. DWORD flOldProtect;
  58. // Use _alloca() to get the current stack pointer
  59. pStack = (LPBYTE)_alloca(1);
  60. // Find the base of the stack.
  61. if (VirtualQuery(pStack, &mbi, sizeof mbi) == 0)
  62. return 0;
  63. pStackBase = (LPBYTE)mbi.AllocationBase;
  64. // Find the page just below where the stack pointer currently points.
  65. // This is the highest potential guard page.
  66. GetSystemInfo(&si);
  67. PageSize = si.dwPageSize;
  68. pMaxGuard = (LPBYTE) (((DWORD_PTR)pStack & ~(DWORD_PTR)(PageSize - 1))
  69. - PageSize);
  70. // If the potential guard page is too close to the start of the stack
  71. // region, abandon the reset effort for lack of space. Win9x has a
  72. // larger reserved stack requirement.
  73. pMinGuard = pStackBase + ((_Atlosplatform == VER_PLATFORM_WIN32_WINDOWS)
  74. ? _ATL_MIN_STACK_REQ_WIN9X
  75. : _ATL_MIN_STACK_REQ_WINNT);
  76. if (pMaxGuard < pMinGuard)
  77. return 0;
  78. // On a non-Win9x system, do nothing if a guard page is already present,
  79. // else set up the guard page to the bottom of the committed range.
  80. // For Win9x, just set guard page below the current stack page.
  81. if (_Atlosplatform != VER_PLATFORM_WIN32_WINDOWS) {
  82. // Find first block of committed memory in the stack region
  83. pGuard = pStackBase;
  84. do {
  85. if (VirtualQuery(pGuard, &mbi, sizeof mbi) == 0)
  86. return 0;
  87. pGuard = pGuard + mbi.RegionSize;
  88. } while ((mbi.State & MEM_COMMIT) == 0);
  89. pGuard = (LPBYTE)mbi.BaseAddress;
  90. // If first committed block is already marked as a guard page,
  91. // there is nothing that needs to be done, so return success.
  92. if (mbi.Protect & PAGE_GUARD)
  93. return 1;
  94. // Fail if the first committed block is above the highest potential
  95. // guard page. Should never happen.
  96. if (pMaxGuard < pGuard)
  97. return 0;
  98. VirtualAlloc(pGuard, PageSize, MEM_COMMIT, PAGE_READWRITE);
  99. }
  100. else {
  101. pGuard = pMaxGuard;
  102. }
  103. // Enable the new guard page.
  104. flNewProtect = _Atlosplatform == VER_PLATFORM_WIN32_WINDOWS
  105. ? PAGE_NOACCESS
  106. : PAGE_READWRITE | PAGE_GUARD;
  107. return VirtualProtect(pGuard, PageSize, flNewProtect, &flOldProtect);
  108. }
  109. // Verifies if sufficient space is available on the stack.
  110. inline bool _AtlVerifyStackAvailable(SIZE_T Size)
  111. {
  112. bool bStackAvailable = true;
  113. __try
  114. {
  115. PVOID p = _alloca(Size);
  116. p;
  117. }
  118. __except ((EXCEPTION_STACK_OVERFLOW == GetExceptionCode()) ?
  119. EXCEPTION_EXECUTE_HANDLER :
  120. EXCEPTION_CONTINUE_SEARCH)
  121. {
  122. bStackAvailable = false;
  123. _Atlresetstkoflw();
  124. }
  125. return bStackAvailable;
  126. }
  127. // Helper Classes to manage heap buffers for _ATL_SAFE_ALLOCA
  128. // Default allocator used by ATL
  129. class _CCRTAllocator
  130. {
  131. public :
  132. static void * Allocate(SIZE_T nRequestedSize)
  133. {
  134. return malloc(nRequestedSize);
  135. }
  136. static void Free(void* p)
  137. {
  138. free(p);
  139. }
  140. };
  141. template < class Allocator>
  142. class CAtlSafeAllocBufferManager
  143. {
  144. private :
  145. struct CAtlSafeAllocBufferNode
  146. {
  147. CAtlSafeAllocBufferNode* m_pNext;
  148. void* GetData()
  149. {
  150. return (this + 1);
  151. }
  152. };
  153. CAtlSafeAllocBufferNode* m_pHead;
  154. public :
  155. CAtlSafeAllocBufferManager() : m_pHead(NULL) {};
  156. void* Allocate(SIZE_T nRequestedSize)
  157. {
  158. CAtlSafeAllocBufferNode *p = (CAtlSafeAllocBufferNode*)Allocator::Allocate(nRequestedSize + sizeof(CAtlSafeAllocBufferNode));
  159. if (p == NULL)
  160. return NULL;
  161. // Add buffer to the list
  162. p->m_pNext = m_pHead;
  163. m_pHead = p;
  164. return p->GetData();
  165. }
  166. ~CAtlSafeAllocBufferManager()
  167. {
  168. // Walk the list and free the buffers
  169. while (m_pHead != NULL)
  170. {
  171. CAtlSafeAllocBufferNode* p = m_pHead;
  172. m_pHead = m_pHead->m_pNext;
  173. Allocator::Free(p);
  174. }
  175. }
  176. };
  177. // Use one of the following macros before using _ATL_SAFE_ALLOCA
  178. // EX version allows specifying a different heap allocator
  179. #define USES_ATL_SAFE_ALLOCA_EX(x) ATL::_ATL_SAFE_ALLOCA_IMPL::CAtlSafeAllocBufferManager<x> _AtlSafeAllocaManager
  180. #ifndef USES_ATL_SAFE_ALLOCA
  181. #define USES_ATL_SAFE_ALLOCA USES_ATL_SAFE_ALLOCA_EX(ATL::_ATL_SAFE_ALLOCA_IMPL::_CCRTAllocator)
  182. #endif
  183. // nRequestedSize - requested size in bytes
  184. // nThreshold - size in bytes beyond which memory is allocated from the heap.
  185. // Defining _ATL_SAFE_ALLOCA_ALWAYS_ALLOCATE_THRESHOLD_SIZE always allocates the size specified
  186. // for threshold if the stack space is available irrespective of requested size.
  187. // This available for testing purposes. It will help determine the max stack usage due to _alloca's
  188. #ifdef _ATL_SAFE_ALLOCA_ALWAYS_ALLOCATE_THRESHOLD_SIZE
  189. #define _ATL_SAFE_ALLOCA(nRequestedSize, nThreshold) \
  190. (((nRequestedSize) <= (nThreshold) && ATL::_ATL_SAFE_ALLOCA_IMPL::_AtlVerifyStackAvailable(nThreshold) ) ? \
  191. _alloca(nThreshold) : \
  192. ((ATL::_ATL_SAFE_ALLOCA_IMPL::_AtlVerifyStackAvailable(nThreshold)) ? _alloca(nThreshold) : 0), \
  193. _AtlSafeAllocaManager.Allocate(nRequestedSize))
  194. #else
  195. #define _ATL_SAFE_ALLOCA(nRequestedSize, nThreshold) \
  196. (((nRequestedSize) <= (nThreshold) && ATL::_ATL_SAFE_ALLOCA_IMPL::_AtlVerifyStackAvailable(nRequestedSize) ) ? \
  197. _alloca(nRequestedSize) : \
  198. _AtlSafeAllocaManager.Allocate(nRequestedSize))
  199. #endif
  200. // Use 1024 bytes as the default threshold in ATL
  201. #ifndef _ATL_SAFE_ALLOCA_DEF_THRESHOLD
  202. #define _ATL_SAFE_ALLOCA_DEF_THRESHOLD 1024
  203. #endif
  204. } // namespace _ATL_SAFE_ALLOCA_IMPL
  205. } // namespace ATL
  206. // Make sure MFC's afxconv.h hasn't already been loaded to do this
  207. #ifndef USES_CONVERSION
  208. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  209. #ifndef _DEBUG
  210. #define USES_CONVERSION int _convert; _convert
  211. #else
  212. #define USES_CONVERSION int _convert = 0
  213. #endif
  214. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  215. #ifndef _DEBUG
  216. #define USES_CONVERSION_EX int _convert_ex; _convert_ex; UINT _acp_ex = CP_ACP; _acp_ex; LPCWSTR _lpw_ex; _lpw_ex; LPCSTR _lpa_ex; _lpa_ex; USES_ATL_SAFE_ALLOCA
  217. #else
  218. #define USES_CONVERSION_EX int _convert_ex = 0; _convert_ex; UINT _acp_ex = CP_ACP; _acp_ex; LPCWSTR _lpw_ex = NULL; _lpw_ex; LPCSTR _lpa_ex = NULL; _lpa_ex; USES_ATL_SAFE_ALLOCA
  219. #endif
  220. /////////////////////////////////////////////////////////////////////////////
  221. // Global UNICODE<>ANSI translation helpers
  222. LPWSTR WINAPI AtlA2WHelper(LPWSTR lpw, LPCSTR lpa, int nChars);
  223. LPSTR WINAPI AtlW2AHelper(LPSTR lpa, LPCWSTR lpw, int nChars);
  224. #ifndef ATLA2WHELPER
  225. #define ATLA2WHELPER AtlA2WHelper
  226. #define ATLW2AHELPER AtlW2AHelper
  227. #endif
  228. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  229. #define A2W(lpa) (\
  230. ((LPCSTR)lpa == NULL) ? NULL : (\
  231. _convert = (lstrlenA(lpa)+1),\
  232. ATLA2WHELPER((LPWSTR) alloca(_convert*2), (LPCSTR)lpa, _convert)))
  233. #define W2A(lpw) (\
  234. ((LPCWSTR)lpw == NULL) ? NULL : (\
  235. _convert = (lstrlenW(lpw)+1)*2,\
  236. ATLW2AHELPER((LPSTR) alloca(_convert), lpw, _convert)))
  237. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  238. // The call to _alloca will not cause stack overflow if _AtlVerifyStackAvailable returns TRUE.
  239. #define A2W_EX(lpa, nThreshold) (\
  240. ((_lpa_ex = lpa) == NULL) ? NULL : (\
  241. _convert_ex = (lstrlenA(_lpa_ex)+1),\
  242. ATLA2WHELPER( \
  243. (LPWSTR)_ATL_SAFE_ALLOCA(_convert_ex * sizeof(WCHAR), nThreshold), \
  244. _lpa_ex, \
  245. _convert_ex)))
  246. #define W2A_EX(lpw, nThreshold) (\
  247. ((_lpw_ex = lpw) == NULL) ? NULL : (\
  248. _convert_ex = (lstrlenW(_lpw_ex)+1) * sizeof(WCHAR),\
  249. ATLW2AHELPER( \
  250. (LPSTR)_ATL_SAFE_ALLOCA(_convert_ex, nThreshold), \
  251. _lpw_ex, \
  252. _convert_ex)))
  253. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  254. #define A2CW(lpa) ((LPCWSTR)A2W(lpa))
  255. #define W2CA(lpw) ((LPCSTR)W2A(lpw))
  256. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  257. #define A2CW_EX(lpa, nChar) ((LPCWSTR)A2W_EX(lpa, nChar))
  258. #define W2CA_EX(lpw, nChar) ((LPCSTR)W2A_EX(lpw, nChar))
  259. #if defined(_UNICODE)
  260. // in these cases the default (TCHAR) is the same as OLECHAR
  261. inline int ocslen(LPCOLESTR x) { return lstrlenW(x); }
  262. inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpyW(dest, src); }
  263. inline LPCOLESTR T2COLE_EX(LPCTSTR lp, UINT) { return lp; }
  264. inline LPCTSTR OLE2CT_EX(LPCOLESTR lp, UINT) { return lp; }
  265. inline LPOLESTR T2OLE_EX(LPTSTR lp, UINT) { return lp; }
  266. inline LPTSTR OLE2T_EX(LPOLESTR lp, UINT) { return lp; }
  267. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  268. inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
  269. inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
  270. inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
  271. inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
  272. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  273. #elif defined(OLE2ANSI)
  274. // in these cases the default (TCHAR) is the same as OLECHAR
  275. inline int ocslen(LPCOLESTR x) { return lstrlen(x); }
  276. inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src) { return lstrcpy(dest, src); }
  277. inline LPCOLESTR T2COLE_EX(LPCTSTR lp, UINT) { return lp; }
  278. inline LPCTSTR OLE2CT_EX(LPCOLESTR lp, UINT) { return lp; }
  279. inline LPOLESTR T2OLE_EX(LPTSTR lp, UINT) { return lp; }
  280. inline LPTSTR OLE2T_EX(LPOLESTR lp, UINT) { return lp; }
  281. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  282. inline LPCOLESTR T2COLE(LPCTSTR lp) { return lp; }
  283. inline LPCTSTR OLE2CT(LPCOLESTR lp) { return lp; }
  284. inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
  285. inline LPTSTR OLE2T(LPOLESTR lp) { return lp; }
  286. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  287. #else
  288. inline int ocslen(LPCOLESTR x) { return lstrlenW(x); }
  289. //lstrcpyW doesn't work on Win95, so we do this
  290. inline OLECHAR* ocscpy(LPOLESTR dest, LPCOLESTR src)
  291. {return (LPOLESTR) memcpy(dest, src, (lstrlenW(src)+1)*sizeof(WCHAR));}
  292. //CharNextW doesn't work on Win95 so we use this
  293. #define T2COLE_EX(lpa, nChar) A2CW_EX(lpa, nChar)
  294. #define T2OLE_EX(lpa, nChar) A2W_EX(lpa, nChar)
  295. #define OLE2CT_EX(lpo, nChar) W2CA_EX(lpo, nChar)
  296. #define OLE2T_EX(lpo, nChar) W2A_EX(lpo, nChar)
  297. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  298. #define T2COLE(lpa) A2CW(lpa)
  299. #define T2OLE(lpa) A2W(lpa)
  300. #define OLE2CT(lpo) W2CA(lpo)
  301. #define OLE2T(lpo) W2A(lpo)
  302. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  303. #endif
  304. #ifdef OLE2ANSI
  305. inline LPOLESTR A2OLE_EX(LPSTR lp, UINT) { return lp;}
  306. inline LPSTR OLE2A_EX(LPOLESTR lp, UINT) { return lp;}
  307. #define W2OLE_EX W2A_EX
  308. #define OLE2W_EX A2W_EX
  309. inline LPCOLESTR A2COLE_EX(LPCSTR lp, UINT) { return lp;}
  310. inline LPCSTR OLE2CA_EX(LPCOLESTR lp, UINT) { return lp;}
  311. #define W2COLE_EX W2CA_EX
  312. #define OLE2CW_EX A2CW_EX
  313. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  314. inline LPOLESTR A2OLE(LPSTR lp) { return lp;}
  315. inline LPSTR OLE2A(LPOLESTR lp) { return lp;}
  316. #define W2OLE W2A
  317. #define OLE2W A2W
  318. inline LPCOLESTR A2COLE(LPCSTR lp) { return lp;}
  319. inline LPCSTR OLE2CA(LPCOLESTR lp) { return lp;}
  320. #define W2COLE W2CA
  321. #define OLE2CW A2CW
  322. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  323. #else
  324. inline LPOLESTR W2OLE_EX(LPWSTR lp, UINT) { return lp; }
  325. inline LPWSTR OLE2W_EX(LPOLESTR lp, UINT) { return lp; }
  326. #define A2OLE_EX A2W_EX
  327. #define OLE2A_EX W2A_EX
  328. inline LPCOLESTR W2COLE_EX(LPCWSTR lp, UINT) { return lp; }
  329. inline LPCWSTR OLE2CW_EX(LPCOLESTR lp, UINT) { return lp; }
  330. #define A2COLE_EX A2CW_EX
  331. #define OLE2CA_EX W2CA_EX
  332. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  333. inline LPOLESTR W2OLE(LPWSTR lp) { return lp; }
  334. inline LPWSTR OLE2W(LPOLESTR lp) { return lp; }
  335. #define A2OLE A2W
  336. #define OLE2A W2A
  337. inline LPCOLESTR W2COLE(LPCWSTR lp) { return lp; }
  338. inline LPCWSTR OLE2CW(LPCOLESTR lp) { return lp; }
  339. #define A2COLE A2CW
  340. #define OLE2CA W2CA
  341. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  342. #endif
  343. #ifdef _UNICODE
  344. #define T2A_EX W2A_EX
  345. #define A2T_EX A2W_EX
  346. inline LPWSTR T2W_EX(LPTSTR lp, UINT) { return lp; }
  347. inline LPTSTR W2T_EX(LPWSTR lp, UINT) { return lp; }
  348. #define T2CA_EX W2CA_EX
  349. #define A2CT_EX A2CW_EX
  350. inline LPCWSTR T2CW_EX(LPCTSTR lp, UINT) { return lp; }
  351. inline LPCTSTR W2CT_EX(LPCWSTR lp, UINT) { return lp; }
  352. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  353. #define T2A W2A
  354. #define A2T A2W
  355. inline LPWSTR T2W(LPTSTR lp) { return lp; }
  356. inline LPTSTR W2T(LPWSTR lp) { return lp; }
  357. #define T2CA W2CA
  358. #define A2CT A2CW
  359. inline LPCWSTR T2CW(LPCTSTR lp) { return lp; }
  360. inline LPCTSTR W2CT(LPCWSTR lp) { return lp; }
  361. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  362. #else
  363. #define T2W_EX A2W_EX
  364. #define W2T_EX W2A_EX
  365. inline LPSTR T2A_EX(LPTSTR lp, UINT) { return lp; }
  366. inline LPTSTR A2T_EX(LPSTR lp, UINT) { return lp; }
  367. #define T2CW_EX A2CW_EX
  368. #define W2CT_EX W2CA_EX
  369. inline LPCSTR T2CA_EX(LPCTSTR lp, UINT) { return lp; }
  370. inline LPCTSTR A2CT_EX(LPCSTR lp, UINT) { return lp; }
  371. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  372. #define T2W A2W
  373. #define W2T W2A
  374. inline LPSTR T2A(LPTSTR lp) { return lp; }
  375. inline LPTSTR A2T(LPSTR lp) { return lp; }
  376. #define T2CW A2CW
  377. #define W2CT W2CA
  378. inline LPCSTR T2CA(LPCTSTR lp) { return lp; }
  379. inline LPCTSTR A2CT(LPCSTR lp) { return lp; }
  380. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  381. #endif
  382. inline BSTR A2WBSTR(LPCSTR lp, int nLen = -1)
  383. {
  384. if (lp == NULL || nLen == 0)
  385. return NULL;
  386. USES_CONVERSION_EX;
  387. BSTR str = NULL;
  388. int nConvertedLen = MultiByteToWideChar(_acp_ex, 0, lp,
  389. nLen, NULL, NULL);
  390. int nAllocLen = nConvertedLen;
  391. if (nLen == -1)
  392. nAllocLen -= 1; // Don't allocate terminating '\0'
  393. str = ::SysAllocStringLen(NULL, nAllocLen);
  394. if (str != NULL)
  395. {
  396. int nResult;
  397. nResult = MultiByteToWideChar(_acp_ex, 0, lp, nLen, str, nConvertedLen);
  398. if(nResult != nConvertedLen)
  399. {
  400. SysFreeString(str);
  401. return NULL;
  402. }
  403. }
  404. return str;
  405. }
  406. inline BSTR OLE2BSTR(LPCOLESTR lp) {return ::SysAllocString(lp);}
  407. #if defined(_UNICODE)
  408. // in these cases the default (TCHAR) is the same as OLECHAR
  409. inline BSTR T2BSTR_EX(LPCTSTR lp) {return ::SysAllocString(lp);}
  410. inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
  411. inline BSTR W2BSTR_EX(LPCWSTR lp) {return ::SysAllocString(lp);}
  412. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  413. inline BSTR T2BSTR(LPCTSTR lp) {return T2BSTR_EX(lp); }
  414. inline BSTR A2BSTR(LPCSTR lp) {return A2BSTR_EX(lp); }
  415. inline BSTR W2BSTR(LPCWSTR lp) {return W2BSTR_EX(lp); }
  416. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  417. #elif defined(OLE2ANSI)
  418. // in these cases the default (TCHAR) is the same as OLECHAR
  419. inline BSTR T2BSTR_EX(LPCTSTR lp) {return ::SysAllocString(lp);}
  420. inline BSTR A2BSTR_EX(LPCSTR lp) {return ::SysAllocString(lp);}
  421. inline BSTR W2BSTR_EX(LPCWSTR lp) {USES_CONVERSION_EX; return ::SysAllocString(W2COLE_EX(lp));}
  422. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  423. inline BSTR T2BSTR(LPCTSTR lp) {return T2BSTR_EX(lp); }
  424. inline BSTR A2BSTR(LPCSTR lp) {return A2BSTR_EX(lp); }
  425. inline BSTR W2BSTR(LPCWSTR lp) {USES_CONVERSION; return ::SysAllocString(W2COLE(lp));}
  426. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  427. #else
  428. inline BSTR T2BSTR_EX(LPCTSTR lp) {return A2WBSTR(lp);}
  429. inline BSTR A2BSTR_EX(LPCSTR lp) {return A2WBSTR(lp);}
  430. inline BSTR W2BSTR_EX(LPCWSTR lp) {return ::SysAllocString(lp);}
  431. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  432. inline BSTR T2BSTR(LPCTSTR lp) {return T2BSTR_EX(lp); }
  433. inline BSTR A2BSTR(LPCSTR lp) {return A2BSTR_EX(lp); }
  434. inline BSTR W2BSTR(LPCWSTR lp) {return W2BSTR_EX(lp); }
  435. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  436. #endif
  437. #if defined(_WINGDI_) && !defined(NOGDI)
  438. /////////////////////////////////////////////////////////////////////////////
  439. // Global UNICODE<>ANSI translation helpers
  440. LPDEVMODEW AtlDevModeA2W(LPDEVMODEW lpDevModeW, LPDEVMODEA lpDevModeA);
  441. LPDEVMODEA AtlDevModeW2A(LPDEVMODEA lpDevModeA, LPDEVMODEW lpDevModeW);
  442. LPTEXTMETRICW AtlTextMetricA2W(LPTEXTMETRICW lptmW, LPTEXTMETRICA pltmA);
  443. LPTEXTMETRICA AtlTextMetricW2A(LPTEXTMETRICA lptmA, LPTEXTMETRICW pltmW);
  444. #ifndef ATLDEVMODEA2W
  445. #define ATLDEVMODEA2W AtlDevModeA2W
  446. #define ATLDEVMODEW2A AtlDevModeW2A
  447. #define ATLTEXTMETRICA2W AtlTextMetricA2W
  448. #define ATLTEXTMETRICW2A AtlTextMetricW2A
  449. #endif
  450. // Requires USES_CONVERSION_EX or USES_ATL_SAFE_ALLOCA macro before using the _EX versions of the macros
  451. #define DEVMODEW2A_EX(lpw)\
  452. ((lpw == NULL) ? NULL : ATLDEVMODEW2A((LPDEVMODEA)_ATL_SAFE_ALLOCA(sizeof(DEVMODEA)+lpw->dmDriverExtra, _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lpw))
  453. #define DEVMODEA2W_EX(lpa)\
  454. ((lpa == NULL) ? NULL : ATLDEVMODEA2W((LPDEVMODEW)_ATL_SAFE_ALLOCA(sizeof(DEVMODEW)+lpa->dmDriverExtra, _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lpa))
  455. #define TEXTMETRICW2A_EX(lptmw)\
  456. ((lptmw == NULL) ? NULL : ATLTEXTMETRICW2A((LPTEXTMETRICA)_ATL_SAFE_ALLOCA(sizeof(TEXTMETRICA), _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lptmw))
  457. #define TEXTMETRICA2W_EX(lptma)\
  458. ((lptma == NULL) ? NULL : ATLTEXTMETRICA2W((LPTEXTMETRICW)_ATL_SAFE_ALLOCA(sizeof(TEXTMETRICW), _ATL_SAFE_ALLOCA_DEF_THRESHOLD), lptma))
  459. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  460. #define DEVMODEW2A(lpw)\
  461. ((lpw == NULL) ? NULL : ATLDEVMODEW2A((LPDEVMODEA)alloca(sizeof(DEVMODEA)+lpw->dmDriverExtra), lpw))
  462. #define DEVMODEA2W(lpa)\
  463. ((lpa == NULL) ? NULL : ATLDEVMODEA2W((LPDEVMODEW)alloca(sizeof(DEVMODEW)+lpa->dmDriverExtra), lpa))
  464. #define TEXTMETRICW2A(lptmw)\
  465. ((lptmw == NULL) ? NULL : ATLTEXTMETRICW2A((LPTEXTMETRICA)alloca(sizeof(TEXTMETRICA)), lptmw))
  466. #define TEXTMETRICA2W(lptma)\
  467. ((lptma == NULL) ? NULL : ATLTEXTMETRICA2W((LPTEXTMETRICW)alloca(sizeof(TEXTMETRICW)), lptma))
  468. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  469. #ifdef OLE2ANSI
  470. #define DEVMODEOLE DEVMODEA
  471. #define LPDEVMODEOLE LPDEVMODEA
  472. #define TEXTMETRICOLE TEXTMETRICA
  473. #define LPTEXTMETRICOLE LPTEXTMETRICA
  474. #else
  475. #define DEVMODEOLE DEVMODEW
  476. #define LPDEVMODEOLE LPDEVMODEW
  477. #define TEXTMETRICOLE TEXTMETRICW
  478. #define LPTEXTMETRICOLE LPTEXTMETRICW
  479. #endif
  480. #if defined(_UNICODE)
  481. // in these cases the default (TCHAR) is the same as OLECHAR
  482. inline LPDEVMODEW DEVMODEOLE2T_EX(LPDEVMODEOLE lp) { return lp; }
  483. inline LPDEVMODEOLE DEVMODET2OLE_EX(LPDEVMODEW lp) { return lp; }
  484. inline LPTEXTMETRICW TEXTMETRICOLE2T_EX(LPTEXTMETRICOLE lp) { return lp; }
  485. inline LPTEXTMETRICOLE TEXTMETRICT2OLE_EX(LPTEXTMETRICW lp) { return lp; }
  486. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  487. inline LPDEVMODEW DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
  488. inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODEW lp) { return lp; }
  489. inline LPTEXTMETRICW TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
  490. inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRICW lp) { return lp; }
  491. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  492. #elif defined(OLE2ANSI)
  493. // in these cases the default (TCHAR) is the same as OLECHAR
  494. inline LPDEVMODE DEVMODEOLE2T_EX(LPDEVMODEOLE lp) { return lp; }
  495. inline LPDEVMODEOLE DEVMODET2OLE_EX(LPDEVMODE lp) { return lp; }
  496. inline LPTEXTMETRIC TEXTMETRICOLE2T_EX(LPTEXTMETRICOLE lp) { return lp; }
  497. inline LPTEXTMETRICOLE TEXTMETRICT2OLE_EX(LPTEXTMETRIC lp) { return lp; }
  498. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  499. inline LPDEVMODE DEVMODEOLE2T(LPDEVMODEOLE lp) { return lp; }
  500. inline LPDEVMODEOLE DEVMODET2OLE(LPDEVMODE lp) { return lp; }
  501. inline LPTEXTMETRIC TEXTMETRICOLE2T(LPTEXTMETRICOLE lp) { return lp; }
  502. inline LPTEXTMETRICOLE TEXTMETRICT2OLE(LPTEXTMETRIC lp) { return lp; }
  503. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  504. #else
  505. #define DEVMODEOLE2T_EX(lpo) DEVMODEW2A_EX(lpo)
  506. #define DEVMODET2OLE_EX(lpa) DEVMODEA2W_EX(lpa)
  507. #define TEXTMETRICOLE2T_EX(lptmw) TEXTMETRICW2A_EX(lptmw)
  508. #define TEXTMETRICT2OLE_EX(lptma) TEXTMETRICA2W_EX(lptma)
  509. #ifndef _ATL_EX_CONVERSION_MACROS_ONLY
  510. #define DEVMODEOLE2T(lpo) DEVMODEW2A(lpo)
  511. #define DEVMODET2OLE(lpa) DEVMODEA2W(lpa)
  512. #define TEXTMETRICOLE2T(lptmw) TEXTMETRICW2A(lptmw)
  513. #define TEXTMETRICT2OLE(lptma) TEXTMETRICA2W(lptma)
  514. #endif // _ATL_EX_CONVERSION_MACROS_ONLY
  515. #endif
  516. #endif //_WINGDI_
  517. #else //!USES_CONVERSION
  518. // if USES_CONVERSION already defined (i.e. MFC_VER < 4.21 )
  519. // flip this switch to avoid atlconv.cpp
  520. #define _ATL_NO_CONVERSIONS
  521. #endif //!USES_CONVERSION
  522. // Define these even if MFC already included
  523. #if defined(_UNICODE)
  524. // in these cases the default (TCHAR) is the same as OLECHAR
  525. inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNextW(lp);}
  526. #elif defined(OLE2ANSI)
  527. // in these cases the default (TCHAR) is the same as OLECHAR
  528. inline LPOLESTR CharNextO(LPCOLESTR lp) {return CharNext(lp);}
  529. #else
  530. inline LPOLESTR CharNextO(LPCOLESTR lp) {return (LPOLESTR)(lp+1);}
  531. #endif
  532. #pragma pack(pop)
  533. #endif // __ATLCONV_H__
  534. /////////////////////////////////////////////////////////////////////////////