WCHAR转CHAR

void
	TestWideCharToChar()
{
	WCHAR	wchIn[MAX_PATH]	= {0};
	CHAR	chOut[MAX_PATH]	= {0};
	LPSTR	lpANSIBuf		= NULL;
	int		nANSIBufLen		= 0;


	__try
	{
		CopyMemory(wchIn, L"我我我我我我", wcslen(L"我我我我我我") * sizeof(WCHAR));

		nANSIBufLen = WideCharToMultiByte(
			CP_ACP,
			0,
			wchIn,
			-1,
			chOut,
			0,
			NULL,
			NULL		
			);
		if (!nANSIBufLen)
		{
			printf("[TestWideCharToChar] : WideCharToMultiByte failed. (%d) \n",
				GetLastError());

			__leave;
		}

		lpANSIBuf = (LPSTR)calloc(1, nANSIBufLen);
		if (!lpANSIBuf)
		{
			printf("[TestWideCharToChar] : calloc failed. (%d) \n",
				GetLastError());

			__leave;
		}

		nANSIBufLen = WideCharToMultiByte(
			CP_ACP,
			0,
			wchIn,
			-1,
			lpANSIBuf,
			nANSIBufLen,
			NULL,
			NULL		
			);
		if (!nANSIBufLen)
		{
			printf("[TestWideCharToChar] : WideCharToMultiByte failed. (%d) \n",
				GetLastError());

			__leave;
		}

		printf("[TestWideCharToChar] : %s \n", lpANSIBuf);
	}
	__finally
	{
		if (lpANSIBuf)
			free(lpANSIBuf);
	}

	return;
}

你可能感兴趣的:(WCHAR转CHAR)