从 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

相关文章

java Long 与long之间的转换流程

《javaLong与long之间的转换流程》Long类提供了一些方法,用于在long和其他数据类型(如String)之间进行转换,本文将详细介绍如何在Java中实现Long和long之间的转换,感... 目录概述流程步骤1:将long转换为Long对象步骤2:将Longhttp://www.cppcns.c

在Java中将XLS转换为XLSX的实现方案

《在Java中将XLS转换为XLSX的实现方案》在本文中,我们将探讨传统ExcelXLS格式与现代XLSX格式的结构差异,并为Java开发者提供转换方案,通过了解底层原理、性能优势及实用工具,您将掌握... 目录为什么升级XLS到XLSX值得投入?实际转换过程解析推荐技术方案对比Apache POI实现编程

Python使用FFmpeg实现高效音频格式转换工具

《Python使用FFmpeg实现高效音频格式转换工具》在数字音频处理领域,音频格式转换是一项基础但至关重要的功能,本文主要为大家介绍了Python如何使用FFmpeg实现强大功能的图形化音频转换工具... 目录概述功能详解软件效果展示主界面布局转换过程截图完成提示开发步骤详解1. 环境准备2. 项目功能结

使用Python实现网页表格转换为markdown

《使用Python实现网页表格转换为markdown》在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,本文将使用Python编写一个网页表格转Markdown工具,需... 在日常工作中,我们经常需要从网页上复制表格数据,并将其转换成Markdown格式,以便在文档、邮件或

Python将字符串转换为小写字母的几种常用方法

《Python将字符串转换为小写字母的几种常用方法》:本文主要介绍Python中将字符串大写字母转小写的四种方法:lower()方法简洁高效,手动ASCII转换灵活可控,str.translate... 目录一、使用内置方法 lower()(最简单)二、手动遍历 + ASCII 码转换三、使用 str.tr

Java如何将文件内容转换为MD5哈希值

《Java如何将文件内容转换为MD5哈希值》:本文主要介绍Java如何将文件内容转换为MD5哈希值的实现方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Java文件内容转换为MD5哈希值一个完整的Java示例代码代码解释注意事项总结Java文件内容转换为MD5

使用Java将实体类转换为JSON并输出到控制台的完整过程

《使用Java将实体类转换为JSON并输出到控制台的完整过程》在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用JSON格式,用Java将实体类转换为J... 在软件开发的过程中,Java是一种广泛使用的编程语言,而在众多应用中,数据的传输和存储经常需要使用j

Java实现视频格式转换的完整指南

《Java实现视频格式转换的完整指南》在Java中实现视频格式的转换,通常需要借助第三方工具或库,因为视频的编解码操作复杂且性能需求较高,以下是实现视频格式转换的常用方法和步骤,需要的朋友可以参考下... 目录核心思路方法一:通过调用 FFmpeg 命令步骤示例代码说明优点方法二:使用 Jaffree(FF

C语言中的常见进制转换详解(从二进制到十六进制)

《C语言中的常见进制转换详解(从二进制到十六进制)》进制转换是计算机编程中的一个常见任务,特别是在处理低级别的数据操作时,C语言作为一门底层编程语言,在进制转换方面提供了灵活的操作方式,今天,我们将深... 目录1、进制基础2、C语言中的进制转换2.1 从十进制转换为其他进制十进制转二进制十进制转八进制十进

Pandas进行周期与时间戳转换的方法

《Pandas进行周期与时间戳转换的方法》本教程将深入讲解如何在pandas中使用to_period()和to_timestamp()方法,完成时间戳与周期之间的转换,并结合实际应用场景展示这些方法的... 目录to_period() 时间戳转周期基本操作应用示例to_timestamp() 周期转时间戳基