Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
1.8 KiB

  1. #include "ctlspriv.h"
  2. #include <limits.h>
  3. /*
  4. * Creates a buffer for a unicode string, and then copies the ANSI text
  5. * into it (converting it to unicode in the process)
  6. *
  7. * The returned pointer should be freed with LocalFree after use.
  8. */
  9. LPWSTR ProduceWFromA( UINT uiCodePage, LPCSTR psz ) {
  10. LPWSTR pszW;
  11. int cch;
  12. if (psz == NULL || psz == LPSTR_TEXTCALLBACKA)
  13. return (LPWSTR)psz;
  14. // The old code would call lstrlen and lstrcpy which would fault internal to the
  15. // api, this should do about the same...
  16. if (IsBadReadPtr(psz,1))
  17. return NULL; // For now lets try not setting a string...
  18. cch = MultiByteToWideChar(uiCodePage, 0, psz, -1, NULL, 0);
  19. if (cch == 0)
  20. cch = 1;
  21. pszW = LocalAlloc( LMEM_FIXED, cch * sizeof(WCHAR) );
  22. if (pszW != NULL ) {
  23. if (MultiByteToWideChar( uiCodePage, MB_PRECOMPOSED, psz, -1, pszW,
  24. cch ) == FALSE) {
  25. LocalFree(pszW);
  26. pszW = NULL;
  27. }
  28. }
  29. return pszW;
  30. }
  31. /*
  32. * Creates a buffer for a unicode string, and then copies the ANSI text
  33. * into it (converting it to unicode in the process)
  34. *
  35. * The returned pointer should be freed with LocalFree after use.
  36. */
  37. LPSTR ProduceAFromW( UINT uiCodePage, LPCWSTR psz ) {
  38. LPSTR pszA;
  39. int cch;
  40. if (psz == NULL || psz == LPSTR_TEXTCALLBACKW)
  41. return (LPSTR)psz;
  42. cch = WideCharToMultiByte(uiCodePage, 0, psz, -1, NULL, 0, NULL, NULL);
  43. if (cch == 0)
  44. cch = 1;
  45. pszA = LocalAlloc( LMEM_FIXED, cch * sizeof(char) );
  46. if (pszA != NULL ) {
  47. if (WideCharToMultiByte(uiCodePage, 0, psz, -1, pszA, cch, NULL, NULL) ==
  48. FALSE) {
  49. LocalFree(pszA);
  50. pszA = NULL;
  51. }
  52. }
  53. return pszA;
  54. }