请教关于字符集转换的问题
#define _UNICODE
#include <stdlib.h>
#include <string.h>
#include <tchar.h>
#include <objbase.h>
int main()
{
LPCOLESTR pszW;
LPSTR* ppszA= NULL;
ULONG cbAnsi, cCharacters;
DWORD dwError;
pszW = _T("Astring");
cCharacters = wcslen(pszW)+1;
cbAnsi = cCharacters*2;
*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
if (NULL == *ppszA)
return E_OUTOFMEMORY;
// Convert to ANSI.
if (0 == WideCharToMultiByte(CP_ACP, 0, pszW, cCharacters, *ppszA,
cbAnsi, NULL, NULL))
{
dwError = GetLastError();
CoTaskMemFree(*ppszA);
*ppszA = NULL;
return HRESULT_FROM_WIN32(dwError);
}
return NOERROR;
}
此程序段完成把宽字节转换成多字节。
在执行*ppszA = (LPSTR) CoTaskMemAlloc(cbAnsi);
出现unhandled exception in str.exe 0xC0000005:Access Violation.
请大家帮我看看错在哪里了,先谢了。