📄 subject_26635.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> ULONG cCharacters;<BR> DWORD dwError;<BR><BR> // If input is null then just return the same.<BR> if (NULL == pszA)<BR> {<BR> *ppszW = NULL;<BR> return NOERROR;<BR> }<BR><BR> // Determine number of wide characters to be allocated for the<BR> // Unicode string.<BR> cCharacters = strlen(pszA)+1;<BR><BR> // Use of the OLE allocator is required if the resultant Unicode<BR> // string will be passed to another COM component and if that<BR> // component will free it. Otherwise you can use your own allocator.<BR> *ppszW = (LPOLESTR) CoTaskMemAlloc(cCharacters*2);<BR> if (NULL == *ppszW)<BR> return E_OUTOFMEMORY;<BR><BR> // Covert to Unicode.<BR> if (0 == MultiByteToWideChar(CP_ACP, 0, pszA, cCharacters,<BR> *ppszW, cCharacters))<BR> {<BR> dwError = GetLastError();<BR> CoTaskMemFree(*ppszW);<BR> *ppszW = NULL;<BR> return HRESULT_FROM_WIN32(dwError);<BR> }<BR><BR> 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> ULONG cbAnsi, cCharacters;<BR> DWORD dwError;<BR><BR> // If input is null then just return the same.<BR> if (pszW == NULL)<BR> {<BR> *ppszA = NULL;<BR> return NOERROR;<BR> }<BR><BR> cCharacters = wcslen(pszW)+1;<BR> // Determine number of bytes to be allocated for ANSI string. An<BR> // ANSI string can have at most 2 bytes per character (for Double<BR> // Byte Character Strings.)<BR> cbAnsi = cCharacters*2;<BR><BR> // Use of the OLE allocator is not required because the resultant<BR> // ANSI string will never be passed to another COM component. You<BR> // can use your own allocator.<BR> *ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);<BR> if (NULL == *ppszA)<BR> return E_OUTOFMEMORY;<BR><BR> // Convert to ANSI.<BR> if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,<BR> cbAnsi, NULL, NULL))<BR> {<BR> dwError = GetLastError();<BR> CoTaskMemFree(*ppszA);<BR> *ppszA = NULL;<BR> return HRESULT_FROM_WIN32(dwError);<BR> }<BR><BR> 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 + -