从 ANSI 到 Unicode Unicode 到 ANSI 用于 OLE 如何转换

2024-01-06 02:18
文章标签 用于 转换 unicode ansi ole

本文主要是介绍从 ANSI 到 Unicode Unicode 到 ANSI 用于 OLE 如何转换,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

概要

<script type=text/javascript>loadTOCNode(1, 'summary');</script>
所有字符串传递到和接收从 32 位 OLE API 和接口方法使用 Unicode。 这需要使用 ANSI 字符串要传递给 OLE 它们之前它们转换为 Unicode 和将 Unicode 字符串从 OLE 接收到 ANSI 应用程序。 本文演示如何进行这些转换。

更多信息

<script type=text/javascript>loadTOCNode(1, 'moreinformation');</script>
WindowsNT 实现 Unicode (或宽字符) 和 ANSI 版本的 Win 32 函数, 接受字符串参数。 但是 Windows 95 没有实现 Unicode 版本的大多数 Win 32 函数, 接受字符串参数。 而它实现只 ANSI 版本的这些函数。

主要例外到此规则是 32 位 OLE。 32 位 OLE API 和接口方法在 WindowsNT 和 Windows 95 独占使用 Unicode。 或者在 WindowsNT 或 Windows 95 没有实现这些函数 ANSI 版本。

这意味着, 需要在 Windows 95 和 WindowsNT 运行 32 位应用程序必须使用 ANSI 版本的非 OLE Win 32 函数并必须将 ANSI 字符串转换为 Unicode 之前将它们传递给 OLE。

WindowsNT 上只运行 32 位 Unicode 应用程序需要使用任何 ANSI / Unicode 转换函数。

Win 32 提供要 ANSI 到 Unicode 字符串和 Unicode 字符串转换为 ANSI MultiByteToWideChar 和 WideCharToMultiByte。 本文提供 AnsiToUnicode 和 UnicodeToAnsi, 使用这些功能对于 ANSI / Unicode 转换。
  1. /*
  2.  * AnsiToUnicode converts the ANSI string pszA to a Unicode string
  3.  * and returns the Unicode string through ppszW. Space for the
  4.  * the converted string is allocated by AnsiToUnicode.
  5.  */ 
  6. HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)
  7. {
  8.     ULONG cCharacters;
  9.     DWORD dwError;
  10.     // If input is null then just return the same.
  11.     if (NULL == pszA)
  12.     {
  13.         *ppszW = NULL;
  14.         return NOERROR;
  15.     }
  16.     // Determine number of wide characters to be allocated for the
  17.     // Unicode string.
  18.     cCharacters =  strlen(pszA)+1;
  19.     // Use of the OLE allocator is required if the resultant Unicode
  20.     // string will be passed to another COM component and if that
  21.     // component will free it. Otherwise you can use your own allocator.
  22.     *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);
  23.     if (NULL == *ppszW)
  24.         return E_OUTOFMEMORY;
  25.     // Covert to Unicode.
  26.     if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,
  27.                   *ppszW, cCharacters))
  28.     {
  29.         dwError = GetLastError();
  30.         CoTaskMemFree(*ppszW);
  31.         *ppszW = NULL;
  32.         return HRESULT_FROM_WIN32(dwError);
  33.     }
  34.     return NOERROR;
  35. /*
  36.  * UnicodeToAnsi converts the Unicode string pszW to an ANSI string
  37.  * and returns the ANSI string through ppszA. Space for the
  38.  * the converted string is allocated by UnicodeToAnsi.
  39.  */ 
  40. HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)
  41. {
  42.     ULONG cbAnsi, cCharacters;
  43.     DWORD dwError;
  44.     // If input is null then just return the same.
  45.     if (pszW == NULL)
  46.     {
  47.         *ppszA = NULL;
  48.         return NOERROR;
  49.     }
  50.     cCharacters = wcslen(pszW)+1;
  51.     // Determine number of bytes to be allocated for ANSI string. An
  52.     // ANSI string can have at most 2 bytes per character (for Double
  53.     // Byte Character Strings.)
  54.     cbAnsi = cCharacters*2;
  55.     // Use of the OLE allocator is not required because the resultant
  56.     // ANSI  string will never be passed to another COM component. You
  57.     // can use your own allocator.
  58.     *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
  59.     if (NULL == *ppszA)
  60.         return E_OUTOFMEMORY;
  61.     // Convert to ANSI.
  62.     if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
  63.                   cbAnsi, NULL, NULL))
  64.     {
  65.         dwError = GetLastError();
  66.         CoTaskMemFree(*ppszA);
  67.         *ppszA = NULL;
  68.         return HRESULT_FROM_WIN32(dwError);
  69.     }
  70.     return NOERROR;
  71. }
这些函数的示例使用如下所示。 CoTaskMemFree 用于释放转换字符串如果 CoTaskMemAlloc 用于分配字符串。 因为该组件负责释放字符串转换字符串必须不能释放如果它是通过一个 out 参数返回到其他 OLE 组件。 LPOLESTR 是指针指向 Unicode 字符串。
  1. // The following code gets an ANSI filename that is specified by the
  2. // user in the OpenFile common dialog. This file name is converted into
  3. // a Unicode string and is passed to the OLE API CreateFileMoniker. The
  4. // Unicode string is then freed.
  5. OPENFILENAME ofn;
  6. LPOLESTR pszFileNameW;
  7. LPMONIKER pmk;
  8. :
  9. // Get file name from OpenFile Common Dialog. The ANSI file name will
  10. // be placed in ofn.lpstrFile
  11. GetOpenFileName(&ofn);
  12. :
  13. AnsiToUnicode(ofn.lpstrFile, &pszFileNameW);
  14. CreateFileMoniker(pszFileNameW, &pmk);
  15. CoTaskMemFree(pszFileNameW);
  16. // The following code implements IOleInPlaceFrame::SetStatusText.
  17. // The lpszStatusText string, that is received from another OLE
  18. // component, uses Unicode. The string is converted to ANSI before it is
  19. // passed to the ANSI version of SetWindowText. Windows 95 supports only
  20. // the ANSI version of SetWindowText.
  21. COleInPlaceFrame::SetStatusText(LPCOLESTR pszStatusTextW)
  22. {
  23.     LPSTR pszStatusTextA;
  24.     UnicodeToAnsi(pszStatusTextW, &pszStatusTextA);
  25.     SetWindowText(m_hwndStatus, pszStatusTextA);
  26.     CoTaskMemFree(pszStatusTextA);
  27. }
注意 : AnsiToUnicode 和 UnicodeToAnsi 有关分配器用于分配转换字符串中注释。 CoTaskMemAlloc (OLE 分配程序) 都需要使用仅如果结果字符串将传递到其他 OLE 组件并且该组件可释放字符串。 这意味着该字符串作为参数中传递给 OLE 接口方法需要使用 OLE 分配器。 必须使用 OLE 分配器分配字符串作为参数中出 - 传递或通过 out 参数或参数中出 - 返回。

通过使用 OLESTR 宏字符串常量可转换为 Unicode 在编译时。 例如:
  1. CreateFileMoniker(OLESTR("c://boo//har.doc"), &pmk);
Microsoft 基础类 (MFC) 其中附带 VisualC++4.0 编译器源代码中可以找到其他属于 ANSI / Unicode 转换例程。 MFC Technote 59 中描述这些例程: ' 使用 MFC MBCS/Unicode 转换宏 '。 定义这些宏 OLE2T T2OLE、 OLE2CT、 T2COLE、 A2W、 W2A、 A2CW、 W2CA 和 USES_CONVERSION 位于 /msdev/mfc/include/afxpriv.h。 同时参阅 AfxA2WHelper 和 AfxW2AHelper MFC /msdev/mfc/src 和 OLE2T T2OLE、 OLE2CT 和 T2COLE MFC /msdev/mfc/src 中源代码中使用中源代码中。 这些函数允许要取决于是否作了 _ UNICODE 预处理器定义编译或者对于 Unicode 或 ANSI 代码。 例如, 上例中 CreateFileMoniker 调用如下进行与 MFC 宏:
  1. USES_CONVERSION;
  2. GetOpenFileName(&ofn);
  3. CreateFileMoniker(T2OLE(ofn.lpstrFile), &pmk);
如果定义 _ UNICODE, T2OLE 定义如下:
  1. inline LPOLESTR T2OLE(LPTSTR lp) { return lp; }
如果未定义 _ UNICODE, T2OLE 定义如下:
  1. #define T2OLE(lpa) A2W(lpa)
T2OLE 中 T 指示未定义 _ UNICODE 时类型被转换为 OLE 字符串 (Unicode 字符串) 是 Unicode 字符串当定义 _ UNICODE 和 ANSI 字符串。 未定义 _ UNICODE 时同样 LPTSTR 定义作为指向 Unicode 字符串当定义 _ UNICODE 和 ANSI 字符串指向。 T2OLE 定义 _ UNICODE 时不执行任何转换 (LPTSTR = = LPOLESTR)。 当未定义 Unicode, A2W 调用。 A2W 将 ANSI 字符串转换为 Unicode 如下:
  1. #define A2W(lpa) (/ 
  2.         ((LPCSTR)lpa == NULL) ? NULL : (/ 
  3.             _convert = (strlen(lpa)+1),/ 
  4.             AfxA2WHelper((LPWSTR) alloca(_convert*2), lpa, _convert)/ 
  5.         )/ 
  6. )
AfxA2WHelper 使用 MultiByteToWideChar 来进行转换。

MFC 转换宏用于 _ alloca 从用于转换字符串程序堆栈分配空间。 过程调用完成后自动释放空间。 OLE 要求 OLE 分配器能够用于所有字符串 (数据) 将由一个组件分配和释放由另。 这意味着 out 参数传递字符串并中出参数 - 的 OLE 接口必须与 OLE 分配器分配。 因为调用方负责释放它们内参数需要不能分配与 OLE 分配器。 大多数 Linking / Embedding OLE 接口和 API 传递字符串作为参数中。 因此 MFC 转换宏可用于大多数情况。 对于中出参数或者对于因为它们执行未分配空间使用 OLE 分配器返回通过外参数值不能使用 MFC 转换宏。 AnsiToUnicode 和 UnicodeToAnsi 可用于这些情况。

尚未 Unicode/MBCS ANSI 转换例程其他组可以找到 OLE 在 Microsoft Systems 日记本, 上 DonBox 的列中 8月 1995, Vol. 10 否。 8, Page 86。 DonBox 定义用它将返回 Unicode/MBCS 转换 ANSI 字符串转换运算符 C++ 类。 对象转超出范围时自动释放分配空间。 此类修改和使用 OLE 分配器分配以不释放通过中出或 out 参数传递字符串分配空间。

遵循之一 String16, 类, 从其中将 ANSI 字符串转换为 Unicode, DonBox 的列。 其他类, String8, 是类似于这台用于 Unicode 转换为 ANSI 从上例 CreateFileMoniker 调用如下进行与此类:
  1. GetOpenFileName(&ofn);
  2. CreateFileMoniker(String16(ofn.lpstrFile), &pmk);
在上述代码, String16 的实例被创建。 的类构造函数会将 ANSI 字符串转换为 Unicode。 语言实现将调用转换运算符, 运算符 const wchar _ *, 转换到的 CreateFileMoniker 的第一个参数的类型参数。 转换运算符将返回 Unicode 字符串传递到 CreateFileMoniker。 对象将销毁处于转超出范围。
  1. // String16  
  2. // Shim class that converts both 8-bit (foreign) and
  3. // 16-bit (native) strings to 16-bit wideness
  4. class String16 {
  5. public:
  6. // native and foreign constructors
  7.     String16(const char *p8);
  8.     String16(const wchar_t *p16);
  9. // non-virtual destructor (this class is concrete)
  10.   ~String16(void);
  11. // native conversion operator
  12.   operator const wchar_t * (voidconst;
  13. private:
  14. // native wideness string
  15.     wchar_t *m_sz;
  16. // is foreign??
  17.     BOOL m_bIsForeign;
  18. // protect against assignment!
  19.   String16(const String16&);
  20.     String16& operator=(const String16&);
  21. };
  22. // native constructor is a pass-through
  23. inline String16::String16(const wchar_t *p16)
  24. : m_sz((wchar_t *)p16), m_bIsForeign(FALSE)
  25. {
  26. }
  27. // simply give out the native wideness string
  28. inline String16::operator const wchar_t * (voidconst
  29. {
  30.   return m_sz;
  31. }
  32. // foreign constructor requires allocation of a native
  33. // string and conversion
  34. inline String16::String16(const char *p8)
  35. : m_bIsForeign(TRUE)
  36. {
  37. // calculate string length
  38.   size_t len = strlen(p8);
  39. // calculate required buffer size (some characters may
  40. // already occupy 16-bits under DBCS)
  41.   size_t size = mbstowcs(0, p8, len) + 1;
  42. // alloc native string and convert
  43.   if (m_sz = new wchar_t[size])
  44.     mbstowcs(m_sz, p8, size);
  45. }
  46. // delete native string only if synthesized in foreign constructor
  47. inline String16::~String16(void) {
  48.   if (m_bIsForeign)
  49.     delete[] m_sz;
  50. }
这篇文章中的信息适用于:
Microsoft OLE 4.0 当用于
  Microsoft Windows NT 3.51 Service Pack 5
  Microsoft Windows NT 4.0
  Microsoft Windows 95

 

这篇关于从 ANSI 到 Unicode Unicode 到 ANSI 用于 OLE 如何转换的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/574911

相关文章

python实现svg图片转换为png和gif

《python实现svg图片转换为png和gif》这篇文章主要为大家详细介绍了python如何实现将svg图片格式转换为png和gif,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录python实现svg图片转换为png和gifpython实现图片格式之间的相互转换延展:基于Py

C#实现将Excel表格转换为图片(JPG/ PNG)

《C#实现将Excel表格转换为图片(JPG/PNG)》Excel表格可能会因为不同设备或字体缺失等问题,导致格式错乱或数据显示异常,转换为图片后,能确保数据的排版等保持一致,下面我们看看如何使用C... 目录通过C# 转换Excel工作表到图片通过C# 转换指定单元格区域到图片知识扩展C# 将 Excel

C++使用printf语句实现进制转换的示例代码

《C++使用printf语句实现进制转换的示例代码》在C语言中,printf函数可以直接实现部分进制转换功能,通过格式说明符(formatspecifier)快速输出不同进制的数值,下面给大家分享C+... 目录一、printf 原生支持的进制转换1. 十进制、八进制、十六进制转换2. 显示进制前缀3. 指

使用Python开发一个带EPUB转换功能的Markdown编辑器

《使用Python开发一个带EPUB转换功能的Markdown编辑器》Markdown因其简单易用和强大的格式支持,成为了写作者、开发者及内容创作者的首选格式,本文将通过Python开发一个Markd... 目录应用概览代码结构与核心组件1. 初始化与布局 (__init__)2. 工具栏 (setup_t

Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码

《Java中Date、LocalDate、LocalDateTime、LocalTime、时间戳之间的相互转换代码》:本文主要介绍Java中日期时间转换的多种方法,包括将Date转换为LocalD... 目录一、Date转LocalDateTime二、Date转LocalDate三、LocalDateTim

Python实现AVIF图片与其他图片格式间的批量转换

《Python实现AVIF图片与其他图片格式间的批量转换》这篇文章主要为大家详细介绍了如何使用Pillow库实现AVIF与其他格式的相互转换,即将AVIF转换为常见的格式,比如JPG或PNG,需要的小... 目录环境配置1.将单个 AVIF 图片转换为 JPG 和 PNG2.批量转换目录下所有 AVIF 图

详解如何通过Python批量转换图片为PDF

《详解如何通过Python批量转换图片为PDF》:本文主要介绍如何基于Python+Tkinter开发的图片批量转PDF工具,可以支持批量添加图片,拖拽等操作,感兴趣的小伙伴可以参考一下... 目录1. 概述2. 功能亮点2.1 主要功能2.2 界面设计3. 使用指南3.1 运行环境3.2 使用步骤4. 核

Java实现时间与字符串互相转换详解

《Java实现时间与字符串互相转换详解》这篇文章主要为大家详细介绍了Java中实现时间与字符串互相转换的相关方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一、日期格式化为字符串(一)使用预定义格式(二)自定义格式二、字符串解析为日期(一)解析ISO格式字符串(二)解析自定义

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre

python+opencv处理颜色之将目标颜色转换实例代码

《python+opencv处理颜色之将目标颜色转换实例代码》OpenCV是一个的跨平台计算机视觉库,可以运行在Linux、Windows和MacOS操作系统上,:本文主要介绍python+ope... 目录下面是代码+ 效果 + 解释转HSV: 关于颜色总是要转HSV的掩膜再标注总结 目标:将红色的部分滤