common.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #include "./common.h"
  2. #include "./wasabi.h"
  3. #include <strsafe.h>
  4. LPWSTR Plugin_MallocString(size_t cchLen)
  5. {
  6. return (LPWSTR)calloc(cchLen, sizeof(WCHAR));
  7. }
  8. void Plugin_FreeString(LPWSTR pszString)
  9. {
  10. if (NULL != pszString)
  11. {
  12. free(pszString);
  13. }
  14. }
  15. LPWSTR Plugin_ReAllocString(LPWSTR pszString, size_t cchLen)
  16. {
  17. return (LPWSTR)realloc(pszString, sizeof(WCHAR) * cchLen);
  18. }
  19. LPWSTR Plugin_CopyString(LPCWSTR pszSource)
  20. {
  21. if (NULL == pszSource)
  22. return NULL;
  23. INT cchSource = lstrlenW(pszSource) + 1;
  24. LPWSTR copy = Plugin_MallocString(cchSource);
  25. if (NULL != copy)
  26. {
  27. CopyMemory(copy, pszSource, sizeof(WCHAR) * cchSource);
  28. }
  29. return copy;
  30. }
  31. LPSTR Plugin_MallocAnsiString(size_t cchLen)
  32. {
  33. return (LPSTR)calloc(cchLen, sizeof(CHAR));
  34. }
  35. LPSTR Plugin_CopyAnsiString(LPCSTR pszSource)
  36. {
  37. if (NULL == pszSource)
  38. return NULL;
  39. INT cchSource = lstrlenA(pszSource) + 1;
  40. LPSTR copy = Plugin_MallocAnsiString(cchSource);
  41. if (NULL != copy)
  42. {
  43. CopyMemory(copy, pszSource, sizeof(CHAR) * cchSource);
  44. }
  45. return copy;
  46. }
  47. void Plugin_FreeAnsiString(LPSTR pszString)
  48. {
  49. Plugin_FreeString((LPWSTR)pszString);
  50. }
  51. LPSTR Plugin_WideCharToMultiByte(UINT codePage, DWORD dwFlags, LPCWSTR lpWideCharStr, INT cchWideChar, LPCSTR lpDefaultChar, LPBOOL lpUsedDefaultChar)
  52. {
  53. INT cchBuffer = WideCharToMultiByte(codePage, dwFlags, lpWideCharStr, cchWideChar, NULL, 0, lpDefaultChar, lpUsedDefaultChar);
  54. if (0 == cchBuffer) return NULL;
  55. LPSTR buffer = Plugin_MallocAnsiString(cchBuffer);
  56. if (NULL == buffer) return NULL;
  57. if (0 == WideCharToMultiByte(codePage, dwFlags, lpWideCharStr, cchWideChar, buffer, cchBuffer, lpDefaultChar, lpUsedDefaultChar))
  58. {
  59. Plugin_FreeAnsiString(buffer);
  60. return NULL;
  61. }
  62. return buffer;
  63. }
  64. LPWSTR Plugin_MultiByteToWideChar(UINT codePage, DWORD dwFlags, LPCSTR lpMultiByteStr, INT cbMultiByte)
  65. {
  66. if (NULL == lpMultiByteStr) return NULL;
  67. INT cchBuffer = MultiByteToWideChar(codePage, dwFlags, lpMultiByteStr, cbMultiByte, NULL, 0);
  68. if (NULL == cchBuffer) return NULL;
  69. if (cbMultiByte > 0) cchBuffer++;
  70. LPWSTR buffer = Plugin_MallocString(cchBuffer);
  71. if (NULL == buffer) return NULL;
  72. if (0 == MultiByteToWideChar(codePage, dwFlags, lpMultiByteStr, cbMultiByte, buffer, cchBuffer))
  73. {
  74. Plugin_FreeString(buffer);
  75. return NULL;
  76. }
  77. if (cbMultiByte > 0)
  78. {
  79. buffer[cchBuffer - 1] = L'\0';
  80. }
  81. return buffer;
  82. }
  83. LPWSTR Plugin_DuplicateResString(LPCWSTR pszResource)
  84. {
  85. return (IS_INTRESOURCE(pszResource)) ?
  86. (LPWSTR)pszResource :
  87. Plugin_CopyString(pszResource);
  88. }
  89. void Plugin_FreeResString(LPWSTR pszResource)
  90. {
  91. if (!IS_INTRESOURCE(pszResource))
  92. Plugin_FreeString(pszResource);
  93. }
  94. HRESULT Plugin_CopyResString(LPWSTR pszBuffer, INT cchBufferMax, LPCWSTR pszString)
  95. {
  96. if (NULL == pszBuffer)
  97. return E_INVALIDARG;
  98. HRESULT hr = S_OK;
  99. if (NULL == pszString)
  100. {
  101. pszBuffer[0] = L'\0';
  102. }
  103. else if (IS_INTRESOURCE(pszString))
  104. {
  105. if (NULL == WASABI_API_LNG)
  106. hr = E_FAIL;
  107. else
  108. WASABI_API_LNGSTRINGW_BUF((INT)(INT_PTR)pszString, pszBuffer, cchBufferMax);
  109. }
  110. else
  111. {
  112. hr = StringCchCopy(pszBuffer, cchBufferMax, pszString);
  113. }
  114. return hr;
  115. }
  116. void Plugin_SafeRelease(IUnknown *pUnk)
  117. {
  118. if (NULL != pUnk)
  119. pUnk->Release();
  120. }