1
0

ns_wc.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. {
  22. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  23. {
  24. lpWideCharStr[cchWideChar-1]=0;
  25. return cchWideChar;
  26. }
  27. else
  28. return 0;
  29. }
  30. lpWideCharStr[converted]=0;
  31. return converted+1;
  32. }
  33. __inline int WideCharToMultiByteSZ(
  34. UINT CodePage, // code page
  35. DWORD dwFlags, // performance and mapping flags
  36. LPCWSTR lpWideCharStr, // wide-character string
  37. int cchWideChar, // number of chars in string
  38. LPSTR lpMultiByteStr, // buffer for new string
  39. int cbMultiByte, // size of buffer
  40. LPCSTR lpDefaultChar, // default for unmappable chars
  41. LPBOOL lpUsedDefaultChar // set when default char used
  42. )
  43. {
  44. int converted=0;
  45. if (cbMultiByte == 0)
  46. return WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte, lpDefaultChar, lpUsedDefaultChar);
  47. converted= WideCharToMultiByte(CodePage, dwFlags, lpWideCharStr, cchWideChar, lpMultiByteStr, cbMultiByte-1, lpDefaultChar, lpUsedDefaultChar);
  48. if (!converted)
  49. {
  50. if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
  51. {
  52. lpMultiByteStr[cbMultiByte-1]=0;
  53. return cbMultiByte;
  54. }
  55. else
  56. return 0;
  57. }
  58. lpMultiByteStr[converted]=0;
  59. return converted+1;
  60. }
  61. #ifdef __cplusplus
  62. }
  63. #endif