ns_wc.h 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. #pragma once
  2. #include <windows.h>
  3. #ifdef __cplusplus
  4. extern "C"
  5. {
  6. #endif
  7. __inline int MultiByteToWideCharSZ(
  8. UINT CodePage, // code page
  9. DWORD dwFlags, // character-type options
  10. LPCSTR lpMultiByteStr, // string to map
  11. int cbMultiByte, // number of bytes in string
  12. LPWSTR lpWideCharStr, // wide-character buffer
  13. int cchWideChar // size of buffer
  14. )
  15. {
  16. int converted=0;
  17. if (cchWideChar == 0)
  18. return MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar);
  19. converted = MultiByteToWideChar(CodePage, dwFlags, lpMultiByteStr, cbMultiByte, lpWideCharStr, cchWideChar-1);
  20. if (!converted)
  21. return 0;
  22. lpWideCharStr[converted]=0;
  23. return converted+1;
  24. }
  25. __inline int WideCharToMultiByteSZ(
  26. UINT CodePage, // code page
  27. DWORD dwFlags, // performance and mapping flags
  28. LPCWSTR lpWideCharStr, // wide-character string
  29. int cchWideChar, // number of chars in string
  30. LPSTR lpMultiByteStr, // buffer for new string
  31. int cbMultiByte, // size of buffer
  32. LPCSTR lpDefaultChar, // default for unmappable chars
  33. LPBOOL lpUsedDefaultChar // set when default char used
  34. )
  35. {
  36. int converted=0;
  37. if (cbMultiByte == 0)
  38. return WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar);
  39. converted= WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte-1, lpDefaultChar, lpUsedDefaultChar);
  40. if (!converted)
  41. return converted;
  42. lpMultiByteStr[converted]=0;
  43. return converted+1;
  44. }
  45. #ifdef __cplusplus
  46. }
  47. #endif