⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 subject_26635.htm

📁 一些关于vc的问答
💻 HTM
字号:
<p>
序号:26635 发表者:janelinkey 发表日期:2003-01-03 11:47:59
<br>主题:ATL编写COM时如何传递带不规则字符,乱码,或空字符的字符串
<br>内容:用BSTR,转换宏会把空字符后的字符去掉。有什么办法?我的组件必须和VB通讯。
<br><a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p>
<hr size=1>
<blockquote><p>
<font color=red>答案被接受</font><br>回复者:_cpper 回复日期:2003-01-03 11:54:22
<br>内容:<BR>HRESULT __fastcall AnsiToUnicode(LPCSTR pszA, LPOLESTR* ppszW)<BR>{<BR>&nbsp;&nbsp; ULONG cCharacters;<BR>&nbsp;&nbsp; DWORD dwError;<BR><BR>&nbsp;&nbsp; // If input is null then just return the same.<BR>&nbsp;&nbsp; if (NULL == pszA)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*ppszW = NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return NOERROR;<BR>&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp; // Determine number of wide characters to be allocated for the<BR>&nbsp;&nbsp; // Unicode string.<BR>&nbsp;&nbsp; cCharacters =&nbsp;&nbsp;strlen(pszA)+1;<BR><BR>&nbsp;&nbsp; // Use of the OLE allocator is required if the resultant Unicode<BR>&nbsp;&nbsp; // string will be passed to another COM component and if that<BR>&nbsp;&nbsp; // component will free it. Otherwise you can use your own allocator.<BR>&nbsp;&nbsp; *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);<BR>&nbsp;&nbsp; if (NULL == *ppszW)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return E_OUTOFMEMORY;<BR><BR>&nbsp;&nbsp; // Covert to Unicode.<BR>&nbsp;&nbsp; if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*ppszW, cCharacters))<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwError = GetLastError();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CoTaskMemFree(*ppszW);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*ppszW = NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HRESULT_FROM_WIN32(dwError);<BR>&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp; return NOERROR;<BR>}<BR><BR><BR>/*<BR> * UnicodeToAnsi converts the Unicode string pszW to an ANSI string<BR> * and returns the ANSI string through ppszA. Space for the<BR> * the converted string is allocated by UnicodeToAnsi.<BR> */<BR>HRESULT __fastcall UnicodeToAnsi(LPCOLESTR pszW, LPSTR* ppszA)<BR>{<BR>&nbsp;&nbsp; ULONG cbAnsi, cCharacters;<BR>&nbsp;&nbsp; DWORD dwError;<BR><BR>&nbsp;&nbsp; // If input is null then just return the same.<BR>&nbsp;&nbsp; if (pszW == NULL)<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; *ppszA = NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return NOERROR;<BR>&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp; cCharacters = wcslen(pszW)+1;<BR>&nbsp;&nbsp; // Determine number of bytes to be allocated for ANSI string. An<BR>&nbsp;&nbsp; // ANSI string can have at most 2 bytes per character (for Double<BR>&nbsp;&nbsp; // Byte Character Strings.)<BR>&nbsp;&nbsp; cbAnsi = cCharacters*2;<BR><BR>&nbsp;&nbsp; // Use of the OLE allocator is not required because the resultant<BR>&nbsp;&nbsp; // ANSI string will never be passed to another COM component. You<BR>&nbsp;&nbsp; // can use your own allocator.<BR>&nbsp;&nbsp; *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);<BR>&nbsp;&nbsp; if (NULL == *ppszA)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return E_OUTOFMEMORY;<BR><BR>&nbsp;&nbsp; // Convert to ANSI.<BR>&nbsp;&nbsp; if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cbAnsi, NULL, NULL))<BR>&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dwError = GetLastError();<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CoTaskMemFree(*ppszA);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*ppszA = NULL;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return HRESULT_FROM_WIN32(dwError);<BR>&nbsp;&nbsp; }<BR><BR>&nbsp;&nbsp; return NOERROR;<BR>}<BR> 
<br>
<a href="javascript:history.go(-1)">返回上页</a><br><a href=http://www.copathway.com/cndevforum/>访问论坛</a></p></blockquote>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -