home *** CD-ROM | disk | FTP | other *** search
/ Supercompiler 1997 / SUPERCOMPILER97.iso / MS_VC.50 / VC / MFC / SRC / OLECNVRT.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-30  |  6.2 KB  |  182 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1997 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #ifdef AFX_INIT_SEG
  14. #pragma code_seg(AFX_INIT_SEG)
  15. #endif
  16.  
  17. #ifdef _DEBUG
  18. #undef THIS_FILE
  19. static char THIS_FILE[] = __FILE__;
  20. #endif
  21.  
  22. #define new DEBUG_NEW
  23.  
  24. /////////////////////////////////////////////////////////////////////////////
  25. // OLE UNICODE conversion support
  26.  
  27. void AFXAPI AfxBSTR2CString(CString* pStr, BSTR bstr)
  28. {
  29.     ASSERT(pStr != NULL);
  30.     int nLen = SysStringLen(bstr);
  31. #if defined(_UNICODE) || defined(OLE2ANSI)
  32.     LPTSTR lpsz = pStr->GetBufferSetLength(nLen);
  33.     ASSERT(lpsz != NULL);
  34.     memcpy(lpsz, bstr, nLen*sizeof(TCHAR));
  35. #else
  36.     int nBytes = WideCharToMultiByte(CP_ACP, 0, bstr, nLen, NULL, NULL, NULL,
  37.         NULL);
  38.     LPSTR lpsz = pStr->GetBufferSetLength(nBytes);
  39.     ASSERT(lpsz != NULL);
  40.     WideCharToMultiByte(CP_ACP, 0, bstr, nLen, lpsz, nBytes, NULL, NULL);
  41. #endif
  42. }
  43.  
  44. #if !defined(_UNICODE) && !defined(OLE2ANSI)
  45. // this function creates a BSTR but it actually has an ANSI string inside
  46. BSTR AFXAPI AfxBSTR2ABSTR(BSTR bstrW)
  47. {
  48.     int nLen = SysStringLen(bstrW); //not including NULL
  49.     int nBytes = WideCharToMultiByte(CP_ACP, 0, bstrW, nLen,
  50.         NULL, NULL, NULL, NULL); //number of bytes not including NULL
  51.     BSTR bstrA = SysAllocStringByteLen(NULL, nBytes); // allocates nBytes
  52.     VERIFY(WideCharToMultiByte(CP_ACP, 0, bstrW, nLen, (LPSTR)bstrA, nBytes, NULL,
  53.         NULL) == nBytes);
  54.     return bstrA;
  55. }
  56.  
  57. LPWSTR AFXAPI AfxTaskStringA2W(LPCSTR lpa)
  58. {
  59.     LPWSTR lpw = AfxAllocTaskWideString(lpa);
  60.     CoTaskMemFree((void*)lpa);
  61.     return lpw;
  62. }
  63.  
  64. LPSTR AFXAPI AfxTaskStringW2A(LPCWSTR lpw)
  65. {
  66.     LPSTR lpa = AfxAllocTaskAnsiString(lpw);
  67.     CoTaskMemFree((void*)lpw);
  68.     return lpa;
  69. }
  70.  
  71. LPDEVMODEW AFXAPI AfxDevModeA2W(LPDEVMODEW lpDevModeW, LPDEVMODEA lpDevModeA)
  72. {
  73.     if (lpDevModeA == NULL)
  74.         return NULL;
  75.     ASSERT(lpDevModeW != NULL);
  76.     AfxA2WHelper(lpDevModeW->dmDeviceName, (LPCSTR)lpDevModeA->dmDeviceName, 32*sizeof(WCHAR));
  77.     memcpy(&lpDevModeW->dmSpecVersion, &lpDevModeA->dmSpecVersion,
  78.         offsetof(DEVMODEW, dmFormName) - offsetof(DEVMODEW, dmSpecVersion));
  79.     AfxA2WHelper(lpDevModeW->dmFormName, (LPCSTR)lpDevModeA->dmFormName, 32*sizeof(WCHAR));
  80.     memcpy(&lpDevModeW->dmLogPixels, &lpDevModeA->dmLogPixels,
  81.         sizeof(DEVMODEW) - offsetof(DEVMODEW, dmLogPixels));
  82.     if (lpDevModeA->dmDriverExtra != 0)
  83.         memcpy(lpDevModeW+1, lpDevModeA+1, lpDevModeA->dmDriverExtra);
  84.     lpDevModeW->dmSize = sizeof(DEVMODEW);
  85.     return lpDevModeW;
  86. }
  87.  
  88. LPDEVMODEA AFXAPI AfxDevModeW2A(LPDEVMODEA lpDevModeA, LPDEVMODEW lpDevModeW)
  89. {
  90.     if (lpDevModeW == NULL)
  91.         return NULL;
  92.     ASSERT(lpDevModeA != NULL);
  93.     AfxW2AHelper((LPSTR)lpDevModeA->dmDeviceName, lpDevModeW->dmDeviceName, 32*sizeof(char));
  94.     memcpy(&lpDevModeA->dmSpecVersion, &lpDevModeW->dmSpecVersion,
  95.         offsetof(DEVMODEA, dmFormName) - offsetof(DEVMODEA, dmSpecVersion));
  96.     AfxW2AHelper((LPSTR)lpDevModeA->dmFormName, lpDevModeW->dmFormName, 32*sizeof(char));
  97.     memcpy(&lpDevModeA->dmLogPixels, &lpDevModeW->dmLogPixels,
  98.         sizeof(DEVMODEA) - offsetof(DEVMODEA, dmLogPixels));
  99.     if (lpDevModeW->dmDriverExtra != 0)
  100.         memcpy(lpDevModeA+1, lpDevModeW+1, lpDevModeW->dmDriverExtra);
  101.     lpDevModeA->dmSize = sizeof(DEVMODEA);
  102.     return lpDevModeA;
  103. }
  104.  
  105. LPTEXTMETRICW AFXAPI AfxTextMetricA2W(LPTEXTMETRICW lptmW, LPTEXTMETRICA lptmA)
  106. {
  107.     if (lptmA == NULL)
  108.         return NULL;
  109.     ASSERT(lptmW != NULL);
  110.     memcpy(lptmW, lptmA, sizeof(LONG) * 11);
  111.     memcpy(&lptmW->tmItalic, &lptmA->tmItalic, sizeof(BYTE) * 5);
  112.     MultiByteToWideChar(CP_ACP, 0, (LPCSTR)&lptmA->tmFirstChar, 1, &lptmW->tmFirstChar, 1);
  113.     MultiByteToWideChar(CP_ACP, 0, (LPCSTR)&lptmA->tmLastChar, 1, &lptmW->tmLastChar, 1);
  114.     MultiByteToWideChar(CP_ACP, 0, (LPCSTR)&lptmA->tmDefaultChar, 1, &lptmW->tmDefaultChar, 1);
  115.     MultiByteToWideChar(CP_ACP, 0, (LPCSTR)&lptmA->tmBreakChar, 1, &lptmW->tmBreakChar, 1);
  116.     return lptmW;
  117. }
  118.  
  119. LPTEXTMETRICA AFXAPI AfxTextMetricW2A(LPTEXTMETRICA lptmA, LPTEXTMETRICW lptmW)
  120. {
  121.     if (lptmW == NULL)
  122.         return NULL;
  123.     ASSERT(lptmA != NULL);
  124.     memcpy(lptmA, lptmW, sizeof(LONG) * 11);
  125.     memcpy(&lptmA->tmItalic, &lptmW->tmItalic, sizeof(BYTE) * 5);
  126.     WideCharToMultiByte(CP_ACP, 0, &lptmW->tmFirstChar, 1, (LPSTR)&lptmA->tmFirstChar, 1, NULL, NULL);
  127.     WideCharToMultiByte(CP_ACP, 0, &lptmW->tmLastChar, 1, (LPSTR)&lptmA->tmLastChar, 1, NULL, NULL);
  128.     WideCharToMultiByte(CP_ACP, 0, &lptmW->tmDefaultChar, 1, (LPSTR)&lptmA->tmDefaultChar, 1, NULL, NULL);
  129.     WideCharToMultiByte(CP_ACP, 0, &lptmW->tmBreakChar, 1, (LPSTR)&lptmA->tmBreakChar, 1, NULL, NULL);
  130.     return lptmA;
  131. }
  132. #endif
  133.  
  134. /////////////////////////////////////////////////////////////////////////////
  135. // OLE task memory allocation support
  136.  
  137. #ifndef _MAC
  138. LPWSTR AFXAPI AfxAllocTaskWideString(LPCWSTR lpszString)
  139. {
  140.     if (lpszString == NULL)
  141.         return NULL;
  142.     UINT nSize = (wcslen(lpszString)+1) * sizeof(WCHAR);
  143.     LPWSTR lpszResult = (LPWSTR)CoTaskMemAlloc(nSize);
  144.     if (lpszResult != NULL)
  145.         memcpy(lpszResult, lpszString, nSize);
  146.     return lpszResult;
  147. }
  148.  
  149. LPWSTR AFXAPI AfxAllocTaskWideString(LPCSTR lpszString)
  150. {
  151.     if (lpszString == NULL)
  152.         return NULL;
  153.     UINT nLen = strlen(lpszString)+1;
  154.     LPWSTR lpszResult = (LPWSTR)CoTaskMemAlloc(nLen*sizeof(WCHAR));
  155.     if (lpszResult != NULL)
  156.         VERIFY(MultiByteToWideChar(CP_ACP, 0, lpszString, -1, lpszResult, nLen));
  157.     return lpszResult;
  158. }
  159.  
  160. LPSTR AFXAPI AfxAllocTaskAnsiString(LPCWSTR lpszString)
  161. {
  162.     if (lpszString == NULL)
  163.         return NULL;
  164.     UINT nBytes = (wcslen(lpszString)+1)*2;
  165.     LPSTR lpszResult = (LPSTR)CoTaskMemAlloc(nBytes);
  166.     if (lpszResult != NULL)
  167.         VERIFY(WideCharToMultiByte(CP_ACP, 0, lpszString, -1, lpszResult, nBytes, NULL, NULL));
  168.     return lpszResult;
  169. }
  170. #endif
  171.  
  172. LPSTR AFXAPI AfxAllocTaskAnsiString(LPCSTR lpszString)
  173. {
  174.     if (lpszString == NULL)
  175.         return NULL;
  176.     UINT nSize = strlen(lpszString)+1;
  177.     LPSTR lpszResult = (LPSTR)CoTaskMemAlloc(nSize);
  178.     if (lpszResult != NULL)
  179.         memcpy(lpszResult, lpszString, nSize);
  180.     return lpszResult;
  181. }
  182.